Preheat oven to 350°F. Grease a 9x13 pan and set aside.
Cook onion and butter over medium low until onion is tender. Add thyme and flour and cook for 1-2 minutes.
Turn heat to low and add milk a little bit at a time whisking between each addition. The mixture will thicken but as you continue adding and whisking, it will smooth out. Stir in chicken broth, salt and pepper bring to a boil and allow to boil 1 minute.
Place 1/3 of the potatoes topped with 1/3 of the ham and 1/3 of the sauce. Repeat layers ending with sauce on top.
Bake covered for 50 minutes. Uncover and bake 25-35 minutes longer or until potatoes are tender. Add the cheese during the last 15 minutes if using.
ool at least 15 minutes before serving. Garnish with parsley.