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
- 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 template.
- In Wait template, enter the template to wait for, for example,
{{ is_state('media_player.living_room', 'idle') }}. - 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 the template is true.
Using this building block in YAML
In YAML, use wait_template:
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:
wait_template: "{{ is_state('binary_sensor.entrance', 'on') }}"
timeout: "00:01:00"
Options in YAML
The template to wait for. The wait ends as soon as the template is true.
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 the template is true or because the timeout ended, the wait variable shows the result:
-
wait.completed:trueif the template became true,falseif the timeout ended first. -
wait.remaining: The time left of the timeout, in seconds, ornoneif 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
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.