brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: wait_for_trigger with a timeout stops Home Assistant automations from waiting forever

Yaml
~/tips/tip-home-assistant-wait-for-trigger-timeout

actions:

Set timeout and continue_on_timeout on wait_for_trigger to decide what happens when nothing arrives.

A wait step with no end is a fine way to end up with an automation that sits there for days. The timeout option is short, and the choice behind it is the one worth thinking about.

This is the shape, with a hallway light that waits for the motion sensor to go quiet:

actions:
  - action: light.turn_on
    target:
      entity_id: light.hallway
  - wait_for_trigger:
      - trigger: state
        entity_id: binary_sensor.hallway_motion
        to: "off"
    timeout:
      minutes: 10
    continue_on_timeout: true
  - action: light.turn_off
    target:
      entity_id: light.hallway

The docs say timeout is the longest time to wait, and without it the automation waits until a trigger reacts. With continue_on_timeout: true the next steps run when the time is up. With false the automation or script stops there. Pick it on purpose: in my example I want the light off either way, so true fits.

After the wait you get wait.trigger, the data of the trigger that fired, or none if the timeout ended first. That lets you branch on how the wait ended. There is also wait.remaining, the seconds left of the timeout.

Two caveats from the docs. The wait only reacts to a change that happens after it starts, so if the sensor is already off when the step begins, it keeps waiting. And a restart of Home Assistant stops automations that are waiting, and they do not continue afterwards.

Further reading: Wait for a trigger, Home Assistant docs

More Yaml tips yamllint -f parsable config.yaml yamllint -d relaxed docker-compose.yml iif(condition, if_true, if_false, if_none) Sensor is available Today_at() makes time-of-day checks in Home Assistant templates easy All Yaml tips (10) All topics

Link