Wait for a template

The Wait for a template building block pauses the automation or script until a template is true. If the template is already true when the wait starts, the next steps run right away. Use it when the next steps should only run once something is in a certain state, for example, when the media player has stopped.

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 template.
  4. In Wait template, enter the template to wait for, for example, {{ is_state('media_player.living_room', 'idle') }}.
  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

Wait template (Required)

The template to wait for. The wait ends as soon as the template is true.

Timeout (Optional)

The longest time to wait. If you leave it empty, the automation or script waits until the template is true.

Continue on timeout (Optional)

If turned on, the next steps run when the timeout ends, even though the template is not true. If turned off, the automation or script stops. Turned on by default.

Using this building block in YAML

In YAML, use wait_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]
alias: "Wait until media player is stopped"
wait_template: "{{ is_state('media_player.living_room', 'idle') }}"

To wait at most 1 minute, add a timeout:

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_template: "{{ is_state('binary_sensor.entrance', 'on') }}"
timeout: "00:01:00"

Options in YAML

wait_template template Required

The template to wait for. The wait ends as soon as the template is true.

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 the template is true or because the timeout ended, the wait variable shows the result:

  • wait.completed: true if the template became true, false if the timeout ended first.
  • wait.remaining: The time left of the timeout, in seconds, or none if no timeout is set.

Good to know

  • If the template is already true when the wait starts, the next steps run right away. To wait for a change that happens after the wait starts, use Wait for a trigger. For more about this difference, refer to Conditions check the current state.
  • The template is checked again whenever an entity that it references changes state. If the template uses now(), it is also checked at the start of every minute.
  • Continue on timeout is turned on by default. To check whether the template became true, use wait.completed.
  • A restart of Home Assistant stops automations and scripts that are waiting. They don’t continue after the restart.

Examples

Script: Take different actions depending on whether the door opened

This script waits up to 10 seconds for the door to open, and then uses wait.completed to decide what to do.

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', 'on') }}"
    timeout: 10
  - if:
      - "{{ not wait.completed }}"
    then:
      - action: script.door_did_not_open
    else:
      - action: script.turn_on
        target:
          entity_id:
            - script.door_did_open
            - script.play_fanfare

For an example that combines Wait for a template and Wait for a trigger with one timeout for both, refer to Script: wait a total of 10 seconds.