Headlights go on and off when engine is hot

Went to a cruse-in on a hot night. Stuck in traffic for about an hour. When I left it was dark. When I was about 5 mins from home the headlights kept going on and off. The dash switch may be the problem (dash lights don't dim properly, ordered a new 1) however could it be the headlight relay or another problem? All the other lights work, tail lights, parking and turn signals (they don't go on and off).

have seen the dimmer switch do this several times. I would start by replacing it