My repeat/until does not seem to check the condition? (solved)

Hello World!

I have a simple switch (outlet) I would like to turn on at a certain time and off at another time. Since I want to be sure, that the switch turns on/off when it should, I’d like to repeat that, until it says it’s on.

What happens is: The switch turns on at 10:00 … but when I manually turn it off (it’s a ZigBee-Switch integrated view the Zigbee2MQTT Add-On), I expected it to stay off… but the automation kept turning it on every 15 seconds. I expected it to stop repeating, once the switch turned on.

Did I do something wrong here? I checked the state of the switch and it’s updating correctly.

- id: "Reliable Switch"
  alias: Reliable Switch
  description: Switch on/off until really on/off
  trigger:
    - platform: time
      at: "10:00"
      id: "on"
    - platform: time
      at: "12:00"
      id: "off"
  action:
    repeat:
      sequence:
        - service: "switch.turn_{{ trigger.id }}"
          entity_id: 
            - switch.test
        - delay: 00:00:15
      until:
        - condition: state
          entity_id: 
            - switch.test
          state: "{{ trigger.id }}"

Did you wait the full 15 seconds after the switch turned on before turning it off?

I believe that the entire “repeat:” section needs to run before it checks the “until:” conditions and either re-runs the repeat or cancels the automation. So if you don’t wait for the time delay when it checks the conditions it sees the switch off and turns it back on.

1 Like

I’m not exactly sure why its not working. but this work?

        - condition: template
          value_template: '{{ states(''switch.test'') == trigger.id }}'

i see this in the log with yours…

Iteration 1
Executed: July 31, 2022, 09:49:15
Result:
result: false
state: 'on'
wanted_state: '{{ trigger.id }}'

I would have thought it would have worked though. Im sure someone else will explain the ‘why’. It looks like its evaluating the literal of ‘trigger.id’

edit: I think i understand why. Its evaluated when the script is saved rather than fired. So you need a template that will evaluate during execution.

I tried that, but the automation is still running (after hours) and not stopping.
The Logs show it switching to ON every 15 seconds:

`MQTT publish: topic 'zigbee2mqtt/test', payload '{"linkquality":183,"state":"ON","update":{"state":"idle"},"update_available":false}'`

`MQTT publish: topic 'zigbee2mqtt/test', payload '{"linkquality":183,"state":"ON","update":{"state":"idle"},"update_available":false}'`

It seems you are absolutely right!

I changed my automation and now it seems to be working as expected. I did not know that I need to consider value that are set or changed during runtime.

Thank you for your help! :slight_smile: