Preheat the oven to 350°F and grease a 9x13-inch baking dish. Set aside.
In a large skillet over medium-low heat, melt the butter and cook the diced onion until tender, about 5 minutes. Stir in the thyme and flour, and cook for 1-2 minutes, stirring constantly.
Reduce heat to low and gradually add the milk, whisking continuously to avoid lumps. Once all milk is incorporated, stir in the chicken broth, salt, and pepper. Bring the sauce to a boil and let it boil for 1 minute, then remove from heat.
Layer one-third of the sliced potatoes in the prepared dish, top with one-third of the diced ham, and pour one-third of the sauce over the top. Repeat these layers two more times, ending with a final layer of sauce on top.
Cover the dish with foil and bake for 50 minutes. Remove the foil and continue baking for 25-35 minutes, or until the potatoes are tender. If using cheese, sprinkle it on during the last 15 minutes of baking.
Allow the dish to cool for at least 15 minutes before serving. Garnish with fresh parsley if desired.