I want to ensure that lights are turned off if they have been left on for a period of time. I am using an automation with a hold state trigger which under normal circumstances works ok, however if Home Assistant is restarted or if I reload automations the automation gets cancelled and the light is left on.
I can get around Home Assistant restarts by creating an automation triggered by the Home Assistant Start event which turns off the light:-
You could set an input_datetime helper to the current time + the duration you want the lights to remain on when you turn the lights on, and have another automation that triggers on that input_datetime to turn them off. That way, your lights donât go off when you reload automations.
Set up an input_datetime called lights_off_check (under Configuration / Helpers), then on the end of your automation that turns the lights on (where 3600 is the duration in seconds), add this:
As you have already discovered, anything in Home Assistant that performs a countdown (for, delay, timer, etc) is reset by a restart or reload.
The suggested triggers are a good âfail-safeâ, and ensure the light is not left on indefinitely but, as you know, donât take into consideration any remaining time the for may have had prior to being reset (the remaining time is lost so itâs not possible to restore it).
FWIW, if you use timers that must be restored after a restart, I created a means of doing that here:
Thatâs a good suggestion as long as one understands there remains a narrow window of opportunity for the lights to remain on. If the restart/reload occurs moments before the Time Trigger is scheduled to occur, it will not trigger and the lights will remain on.
I think an easy way to seal this tiny opening is to add a second trigger to the âturn offâ automation:
(Possibly even check for a restart event as well.)
Add a condition to check if the current time is at or later thaninput_datetime.lights_off_check and it will ensure the light gets turned off, even it it missed its Time Trigger.