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

  1. Go to Settings > Automations & scenes and open an automation. To edit a script, open the Scripts tab and open the script.
  2. In the Then do section of an automation, or the Sequence section of a script, select Add action.
  3. Select Blocks, then select Wait for a trigger.
  4. 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.
  5. Optional: In Timeout, enter how long to wait at most.
  6. Optional: To stop the automation or script when the timeout ends, turn off Continue on timeout.
  7. Select Save.

Options in the UI

Timeout (Optional)

The longest time to wait. If you leave it empty, the automation or script waits until one of the triggers reacts.

Continue on timeout (Optional)

If turned on, the next steps run when the timeout ends, even though no trigger reacted. If turned off, the automation or script stops. Turned on by default.

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:

ActionAn action makes Home Assistant do something, for example, turn on a light, send a notification, or activate a scene. Automations and scripts run a list of actions. [Learn more]
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:

ActionAn action makes Home Assistant do something, for example, turn on a light, send a notification, or activate a scene. Automations and scripts run a list of actions. [Learn more]
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:

ActionAn action makes Home Assistant do something, for example, turn on a light, send a notification, or activate a scene. Automations and scripts run a list of actions. [Learn more]
if:
  - "{{ wait.trigger.id == 'my_trigger' }}"
then:
  - action: light.turn_on
    target:
      entity_id: light.living_room_table

Options in YAML

wait_for_trigger list Required

The triggers to wait for. Uses the same format as the triggers of an automation.

timeout string | integer | float | map | template (Optional)

The longest time to wait. Uses the same formats as Wait for time to pass (delay), including templates.

continue_on_timeout boolean (Optional, default: true)

If true, the next steps run when the timeout ends. If false, the automation or script stops.

Wait variable

After the wait ends, because a trigger reacted or because the timeout ended, the wait variable shows the result:

  • wait.completed: true if a trigger reacted, false if the timeout ended first.
  • wait.remaining: The time left of the timeout, in seconds, or none if no timeout is set.
  • wait.trigger: Information about the trigger that reacted, in the same format as the trigger data of an automation. none if 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
  • Action: Wait for a trigger
    • Timeout: 5 minutes
    • Trigger: State changed
      • Entity: Living room window (binary_sensor.living_room_window)
      • To: Closed
  • Action: If-then
    • If:
      • Condition: Template
        • Value template: {{ not wait.completed }} (met if the wait ended because of the timeout)
    • Then:
      • Action: Turn off thermostat
        • Target: Living room thermostat (climate.living_room)
YAML example
AutomationAutomations in Home Assistant allow you to automatically respond to things that happen in and around your home. [Learn more]
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
ScriptA script is a saved list of steps that runs when you start it, for example, from a dashboard, with Assist, or from an automation. Unlike an automation, a script has no triggers, so it doesn't start by itself. [Learn more]
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