Automation with loop help please!

Hi,

I always use the GUI to create my automations, but pasted the yaml below to help.

Basically I want to loop around playing some speech until the alarm is disarmed or triggered but the loop keeps repeating forever, even if I manually change the entity state via developer options.

Any ideas what I have done wrong?

Thanks!!

repeat:
  until:
    - condition: state
      entity_id: alarm_control_panel.house_alarm
      state: disarmed
    - condition: or
      conditions:
        - condition: state
          entity_id: alarm_control_panel.house_alarm
          state: triggered
  sequence:
    - service: rest_command.kiosk_command
      data:
        value: Please disarm alarm
    - delay:
        hours: 0
        minutes: 0
        seconds: 5
        milliseconds: 0

The condition in until isn’t quite what you think it might be.

It’s doing this:

(state == disarmed) AND (state == triggered OR)

Notice how the logical OR should be choosing between two things but it only has one. Therefore the entire template is asking for the state to be simultaneously disarmed and triggered (which isn’t possible and so the repeat loops forever).

If you want it to loop until the state is disarmed or triggered, a concise way to express that is with a Template Condition in shorthand notation:

repeat:
  until: "{{ states('alarm_control_panel.house_alarm') in ['disarmed', 'triggered'] }"

There’s an example of it in the documentation for repeat - until.


NOTE

If the concept of a Template Condition in shorthand notation is too confusing, rearrange your existing two State Conditions like this:

  until:
    - condition: or
      conditions:
        - condition: state
          entity_id: alarm_control_panel.house_alarm
          state: disarmed
        - condition: state
          entity_id: alarm_control_panel.house_alarm
          state: triggered
1 Like

Hello,

Thanks very much for your help, really appreciated.

All working fine now :slight_smile:

1 Like