Hi, I’m trying to create an automation, than first open downstairs door, and, 40 seconds later, upstairs door. I created a timer for 40 sc. called ‘upstairs’ in helpers, but, when I trigger the automation, it opens both doors, it doesn’t wait 40sc. Here it’s my code:
I’m sorry, but I forgot to explain that I want a timer to stop if accidentally I press it, to stop opening upstairs door. With the delay, there is no way back, that’s why I want a timer that trigger a picture in lovelace, with a click to stop all automation if you trigger by error…
I use a timer to turn on the bathroom fan after 3 minutes.
This starts the timer
- id: '1612134477709'
alias: Half Bath Timer Activation
description: Activate the timer for the half bath
trigger:
- platform: state
entity_id: switch.half_bath_light
from: 'off'
to: 'on'
condition: []
action:
- service: timer.start
data:
duration: '180'
target:
entity_id: timer.halfbath_timer
mode: single
This activates the fan if necessary
- id: '1624471421039'
alias: Half Bath Fan Run
description: Turn on Half bath fan if light was turned after after being on for
at least 3 minutes
trigger:
- platform: state
entity_id: switch.half_bath_light
from: 'on'
to: 'off'
condition:
- condition: state
entity_id: timer.halfbath_timer
state: idle
action:
- service: switch.turn_on
target:
entity_id: switch.half_bath_fan
- delay:
seconds: '{{ states(''input_number.fan_runtime'')| int }}'
- service: switch.turn_off
target:
entity_id: switch.half_bath_fan
mode: singlee
A timer doesn’t make the automation (or script ) “wait”.
The service call timer.start makes a timer begin its countdown to zero (based on the timer’s duration). Any action appearing after timer.start is executed immediately (the automation doesn’t wait for the timer to finish).
When the timer finishes counting down to zero, it reports it has finished by producing a timer.finishedevent. If you want an automation to be triggered by a timer’s “finished” event, you would use an Event Trigger.
An automation executes its actions in sequence. However if it encounters a delay statement, it will pause for the length of the delay and then resume executing any remaining actions.
If Home Assistant is restarted or automations are reloaded while the delay is in progress, the delay is immediately cancelled and the automation is terminated (meaning any remaining actions are not executed). In contrast, a timer is able to survive a restart/reload (provided you configure it correctly).
EDIT
If you only want the automation to wait for 40 seconds, use a delay because it’s simpler than employing a timer. Forty seconds is a short time period and the chance of a restart/reload interrupting it during that narrow 40-second window of opportunity is low.
You don’t have an “automation”. You have a “script”. An “automation” with no trigger is just a “script”.
a script will run each action step sequentially once activated.
in your script above you have this:
turn on door 1 switch
start the timer
turn on door 2 switch
there is nothing there to tell it to wait until the timer finishes before running the last step. So it just goes from one step to the next and both doors open at pretty much the same time and you have a 40 second timer running with no purpose.
as suggested you can either just use a delay of 40 seconds or you need to use a wait for trigger event of the timer finishing before executing the last step.