Handling state for time after restart

When using an automation triggered on state change for X time, how can I make this survive a restart? There appear to be github issues related to this, but all closed, and I can’t seem to find any answers in the forums… so I’m at a bit of a loss.

I have a switch set to turn off after it’s been on for 30 minutes, but if Home Assistant restarts during that time, it never turns off.

It’s always risky to have an automation wait for more than a minute or two.

If you use a timer helper instead, there is an option to restore the countdown after a restart. You would need two automations, one to start the timer and another perform an action when the timer finished - but there’s nothing wrong with that.

An alternative is to have a general-purpose “housekeeping” automation triggered by a restart. It could only make sure the switch was off, though, not pick up where your original automation left off.

1 Like

I agree with @jackjourneyman suggestions.

one additional way that is pretty simple is to trigger on a template like this:

{{ 
states('switch.your_switch') == 'on'
and 
now() - states.switch.your_switch.last_changed > timedelta(minutes=30) 
}}
1 Like

Oh! I like this template solution and hadn’t considered it! Thank you!

I’ve read about setting timers in automations, but it’s annoying to have to create two separate automations for the single task.

I’ll play around with both options and see which works better for me. Thanks guys!