putting headlights on means the air within the headlight unit is warmer and therefore can hold more moisture. (warmer the air the more moisture it can hold)
Then the next time the air is cooled it could reach a temperature where it condenses. - rain or washing the car should cool it nicely to achive this effect as the lense will be cold and the air that is in contact with this lowers in temp and condenses. (same effect as in the winter when you have people in the car the windows steam up)
Might well be called the 'dew point' temperature which is the temperature where air condenses which depends on a mix between air temp and humidity level.
Options are to either:
1 have a sealed unit (which is difficult given you need access to change bulbs etc and they are not really designed to be 100% sealed) but even then you would need to ensure the air within the unit is low in moiture content/humidity otherwise this wont solve the issue.
2 allow air circulation or air change, so the opposite of a sealed unit. This is commonly used in bathrooms (air extractors / windows).
The units as standard get this issue as they dont allow air circulation, but are also not 100% sealed or contain dehumidified air.
For those that turn headlights on to demist, could do well to open the rear of the unit and allow some air circulation on a dry / low humidity day before closing again. This may
help prevent reoccurance, however washing the car/rain can clearly cause it to reappear if there is even the smallest leak or humidity in the air.