Wait for a trigger
The Wait for a trigger building block pauses the automation or script until one of its triggers reacts to a change. Use it when the next steps should only run after something happens, for example, a door closing.
Using this building block from the user interface
- Go to Settings > Automations & scenes and open an automation. To edit a script, open the Scripts tab and open the script.
- In the Then do section of an automation, or the Sequence section of a script, select Add action.
- Select Blocks, then select Wait for a trigger.
- Select Add trigger, and add the trigger to wait for.
- You can use the same triggers as in the When section of an automation.
- You can add more than one trigger. The wait ends when any of them reacts.
- Optional: In Timeout, enter how long to wait at most.
- Optional: To stop the automation or script when the timeout ends, turn off Continue on timeout.
- Select Save.
Options in the UI
The longest time to wait. If you leave it empty, the automation or script waits until one of the triggers reacts.
Using this building block in YAML
In YAML, use wait_for_trigger with a list of triggers. This example waits for a custom event, or for a light to turn on and stay on for 10 seconds:
alias: "Wait for MY_EVENT or light on"
wait_for_trigger:
- trigger: event
event_type: MY_EVENT
id: my_trigger
- trigger: state
entity_id: light.living_room
to: "on"
for: 10
To stop the script if the event doesn’t happen in time, add a timeout and set continue_on_timeout to false:
wait_for_trigger:
- trigger: event
event_type: ifttt_webhook_received
event_data:
action: connected_to_network
timeout:
minutes: "{{ timeout_minutes }}"
continue_on_timeout: false
You can give each trigger an id, like in the triggers of an automation. To check which trigger ended the wait, use wait.trigger.id in a template:
if:
- "{{ wait.trigger.id == 'my_trigger' }}"
then:
- action: light.turn_on
target:
entity_id: light.living_room_table
Options in YAML
The triggers to wait for. Uses the same format as the triggers of an automation.
The longest time to wait. Uses the same formats as Wait for time to pass (delay), including templates.
Wait variable
After the wait ends, because a trigger reacted or because the timeout ended, the wait variable shows the result:
-
wait.completed:trueif a trigger reacted,falseif the timeout ended first. -
wait.remaining: The time left of the timeout, in seconds, ornoneif no timeout is set. -
wait.trigger: Information about the trigger that reacted, in the same format as the trigger data of an automation.noneif the timeout ended first.
Good to know
- The wait only reacts to a change that happens after the wait starts. If the state is already there, the automation or script keeps waiting. To continue right away in that case, use Wait for a template. For more about this difference, refer to Triggers react to changes.
-
Continue on timeout is turned on by default. To check whether a trigger reacted, use
wait.completed. - A restart of Home Assistant stops automations and scripts that are waiting. They don’t continue after the restart.
- The triggers can use the trigger variables, variables, and script variables that are defined before the wait.
- The Triggered by condition only lists the triggers of the automation itself, not the triggers of the wait. To check which trigger ended the wait, use
wait.trigger.id, as shown in Using this building block in YAML.
Examples
Automation: turn off the heating when a window stays open
When the window opens, this automation waits up to 5 minutes for it to close again. If the window is still open after 5 minutes, it turns off the heating.
-
Trigger: State changed
-
Entity: Living room window (
binary_sensor.living_room_window) - To: Open
-
Entity: Living room window (
-
Action: Wait for a trigger
-
Timeout:
5minutes -
Trigger: State changed
-
Entity: Living room window (
binary_sensor.living_room_window) - To: Closed
-
Entity: Living room window (
-
Timeout:
-
Action: If-then
-
If:
-
Condition: Template
-
Value template:
{{ not wait.completed }}(met if the wait ended because of the timeout)
-
Value template:
-
Condition: Template
-
Then:
-
Action: Turn off thermostat
-
Target: Living room thermostat (
climate.living_room)
-
Target: Living room thermostat (
-
Action: Turn off thermostat
-
If:
YAML example
alias: "Turn off the heating when a window stays open"
triggers:
- trigger: state
entity_id: binary_sensor.living_room_window
to: "on"
actions:
- wait_for_trigger:
- trigger: state
entity_id: binary_sensor.living_room_window
to: "off"
timeout:
minutes: 5
- if:
- "{{ not wait.completed }}"
then:
- action: climate.turn_off
target:
entity_id: climate.living_room
Script: wait a total of 10 seconds
This script uses two waits, with one timeout of 10 seconds for both. The second wait uses wait.remaining, the time that is left after the first wait.
YAML example
sequence:
- wait_template: "{{ is_state('binary_sensor.door_1', 'on') }}"
timeout: 10
continue_on_timeout: false
- action: switch.turn_on
target:
entity_id: switch.some_light
- wait_for_trigger:
- trigger: state
entity_id: binary_sensor.door_2
to: "on"
for: 2
timeout: "{{ wait.remaining }}"
continue_on_timeout: false
- action: switch.turn_off
target:
entity_id: switch.some_light