Preheat oven to 350°F.
In a skillet, cook butter and onion over medium low heat until onions are translucent. Stir in flour and thyme. and continue cooking for 1-2 minutes.
Lower heat, whisk in milk, continue whisking until it is thick and smooth.
Whisk in chicken broth and salt and pepper. Bring to a boil and let boil for 1 minute.
Grease a 9x13 pan, layer 1/3 of potatoes, 1/3 of ham and 1/3 of sauce. Repeat until all of the ingredients are used.
Cover and bake for 45 minutes. Uncover and bake and additional 30 minutes or until the potatoes are tender.
Remove from oven and allow to sit 10 minutes before serving
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.