Automations: Should I break automations with timers in half?

I have a few automations that perform an action, wait some time, and perform some other action. One example is that if it’s dark and I open the door to my garage, the garage ceiling lights turn on. They then start a timer which runs for 5 minutes. Every time the door is opened again, the timer is restarted. After the timer expires, the lights turn off.

The way the automation is set up is to trigger with the door opening, condition on daylight, and start the timer. Then I have a second automation that is triggered by timer.completed on that timer, and it turns the lights off.

Is this the best way to do this? My only concern is that my automations will get a little messy if I use two of them to perform a single true automation. I understand I can use a delay in an automation but it’s less flexible than using timers, which can be restarted and paused.

There are two main methods. Your current method and using a for: <some_time> in a state trigger. e.g

You could achieve exactly the same thing with the first automation set to turn the lights on when the door is opened, and the second automation set to trigger when the door has been in the closed state for some specified time, and the lights are on (condition), then the action is to turn the lights off. This will achieve exactly the same thing, no timers required.

There are pros and cons to each method.

Using your timer method, with some effort you can get timers to restore their state if home assistant is restarted.

When using the for method the for time just starts again when home assistant is restarted (or automations are reloaded). Usually this is not a big deal for light timers, however it is less optimal for things like irrigation timing.

Using delays are even more troublesome to recover from restarts/reloads. Don’t delay in an automation for longer than a few tens of seconds.

1 Like

If you don’t mind using HACS integrations there is one called Entity Controller which is designed to handle this sort of thing.

1 Like

Thanks, both of you.

So @tom_l, my assumption has been that I don’t want automations to be in a wait state for any real period of time, and it appears you are confirming that. It somehow hadn’t occurred to me to use the for:<some_time> trigger. I had only considered using it as a condition.

@Stiltjack, I have some HACS integrations already and I will consider Entity Controller. I am going to see if I can manage without it first, and if I find that my Automations have become a cumbersome list, I’ll migrate.