There is an issue with using a delay in an automation that could possibly be re-triggered while the actions are still running from a previous trigger. Basically, if the automation triggers and starts running the actions, then hits a delay step, and while the delay is still pending the trigger happens again, the automation will actually immediately skip the rest of the delay and pick up with the next action step. As a concrete example:
trigger:
platform: time
minutes: '/5'
seconds: 00
action:
- service: switch.turn_on
entity_id: switch.my_switch
- delay:
minutes: 10
- service: switch.turn_off
entity_id: switch.my_switch
This is triggered every 5 minutes. When triggered it turns a switch on, then starts a 10 minute delay. After 10 minutes it will turn the switch off. Now, of course youâd never write an automation like this! Itâs just for explanation purposes.
Youâd think that since itâs triggered every 5 minutes and has a delay of 10 minutes it would never get to the step of turning off the switch. Actually, it would. The first time itâs triggered it turns on the switch and starts the 10 minute delay. Then 5 minutes later itâs triggered again. Immediately the 10 minute delay is âfinishedâ and the automation goes on to the next step which turns off the switch. 5 minutes later the cycle repeats.
Anyway, the way to âfixâ this is to move the actions of the automation to a script. Then in the action of the automation, add two steps: the first âcancelsâ the script (in case it was still running), and the second starts the script. This is assuming you want to restart the sequence for every trigger. It looks like this:
automation:
- alias: Restart script if retriggered
trigger:
# Whatever
action:
- service: script.cancel
entity_id: script.my_script
- service: script.my_script
script:
my_script:
sequence:
# Some steps
- delay: xxx
# Some more steps
Now letâs say youâd rather ignore triggers until the steps are done from the first trigger. The script would be the same, but the automation would look like this:
automation:
- alias: Ignore triggers while script is running
trigger:
# Whatever
condition:
condition: state
entity_id: script.my_script
state: 'off'
action:
service: script.my_script
UPDATE: Starting with the 0.113 release this behavior has been fixed. Now you can do:
automation:
- trigger:
# Whatever
mode: MODE
action:
# Some steps
- delay: xxx
# Some more steps
Where to get the ârestartâ behavior set MODE
to restart
, and to get the âignoreâ behavior set MODE
to single
. There are also queued
and parallel
options.