I would personally not use any delay and or wait for more than a matter of a few minutes in any automation and or script, as any restarting of HA and or any editing of automations in the front end will cause all automations to be reloaded and cease currently running actions.
I would maybe use a time pattern trigger to trigger on every half the hour but with the time range required as a condition, something like this maybe:
Only if the system was to go down during the 2 mins delay from switching on to switching off and then in theory the switch would remain on, if this is a concern then there is nothing stopping you from making an additional automation that is triggered by the switch having been on for more then 2.5 mins for example.
I would assume your sonoff devices were local so internet not required!
Or just use two automations. No switch.turn_off in the first one. The second one turns off the pump after 2 minutes, any time it’s on. Sometimes two simple automations are better than one complex one. This also avoids using delay.
You could add a timer in the mix and start the timer for the 2 minutes and turn the pump off when it finishes rather than have that delay in the script. You’d have a trigger for the event for hte timer finishing to turn it back off. Then after a reboot it would finish and turn it off as well.
Here’s a single, self-contained automation that triggers at the scheduled times and survives Home Assistant restarts and automation reloads. It turns on the switch if the current time falls within one of the two allowed 2-minute time periods (hour or half-hour plus two minutes) otherwise it turns off the switch.
For example, if Home Assistant is restarted at 06:59:45 and comes back at 07:01:10 it will have missed its scheduled Time Trigger at 07:00:00. However, the automation is triggered by a restart therefore its condition confirms the current hour (7) is between 4 and 8 and the service call’s template determines that the current minute (1) is between 0 and 2 so it produces switch.turn_on.
Similarly if the restart occurs at 07:01:50 and Home Assistant comes back at 07:02:15 it will miss its scheduled Time Trigger at 07:02:00. However, the automation will go through the same process I just described but this time the template will produce switch.turn_off.
FWIW, if you wish, the two Time Triggers can be consolidated into a single Time Trigger. They’re shown as two separate triggers only for legibility (to distinguish which times are for turning on the switch versus turning it off). The service call’s template doesn’t need two separate Time Triggers to set the switch’s state correctly.
EDIT
Correction. Replaced outer single-quotes with double-quotes.
Taras : I tried to use your template t control the hot water circulation pump in the house, to avoid it running 24/7. The template in your post #12 is accepted as an automation only if I remove the “service” line. HA doesn’t want to save the yaml file if I use either of your proposed if action statements :
action:
Tried it as per the edit you posted, when I copy paste the line into the automation and save it. HA removes the " and the pump never gets switched on. Almost there If i change the “iif” to “if” (typo?), it complains "Message malformed: string value is None for dictionary value @ data[‘action’][0]]‘service’]
HA removes the ", so that leaves me with :
service: switch.turn_{{ if(now().minute in [0, 1, 30, 31], ‘on’, ‘off’) }}
You mean HA’s Automation Editor removes the double-quotes. It follows its own formatting guidelines.
The outer double-quotes aren’t entirely necessary in this particular case because, even though there’s a template, the value is understood to be a string.
As to why it doesn’t turn on the switch, that will depend at what time this service call is executed (because it will only turn on when the current minute is one of only four values).
As to why it didn’t trigger my outlet switches, I used entity_id as per the original example, nothing even appeared in the logs. Switching to device_id made it work instantly. No clue why though.