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: add a round limit to a Home Assistant repeat until loop

Yaml
~/tips/tip-home-assistant-repeat-until-or-limit

actions:

A repeat until loop ends when its condition is met, so add a repeat.index check to bound it.

A repeat loop that waits for something to become true has one ugly failure mode: the thing never becomes true. The docs have a one-line fix for it, and I would add it to every loop like this.

A repeat with until runs its actions, then checks the conditions, and repeats until they are met. The actions always run at least once, because the check comes after each round. For a loop that retries up to five times, put the conditions in an Or condition:

actions:
  - repeat:
      sequence:
        - action: notify.notify
          data:
            message: "Where are you?"
        - delay:
            minutes: 5
      until:
        - condition: or
          conditions:
            - condition: template
              value_template: "{{ is_state('device_tracker.iphone', 'home') }}"
            - condition: template
              value_template: "{{ repeat.index >= 5 }}"

The docs say repeat.index holds the number of the round, starting at 1. The state check is the example from the docs. The Or part and the index check are the way the docs suggest to end a repeat after a number of rounds as well. There is also repeat.first, which is true during the first round.

The caveat is the always-once behaviour. Even if the device is already home, the sequence runs one time before the condition is looked at. If that is not what you want, put a condition or an if in front of the loop. The docs do not mention a built-in maximum number of rounds, so the limit is yours to write.

Further reading: Repeat until, 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