Help with repeat loop automation/script

Hello,

I just trying to do a very simple thing i guess but I just can’t make it because the UI automation/script does not have this option. Really appreciate if somebody could help me.

I just need a script thats when fired, repeats and checks the state of the binary_sensor.tor_offen every 2 seconds until the state is off. When the state is finally off, it should call another service.

Thanks in advance…

Use an automation.

trigger:
  platform: state
  entity_id: binary_sensor.your_sensor_here
  to: 'off'
action: 
  service:...

Guude!

If the 2 secs are important I would expand Toms automation example like that:


trigger:
  platform: state
  entity_id: binary_sensor.your_sensor_here
  to: 'off'
  for: 00:00:03
action: 
  service:...

What do you want to achieve in detail?

Why does it need to check every 2 seconds?

Unless you have an unusual application, it is normally unnecessary to poll an entity to determine when its state changes. Simply use a State Trigger to trigger when the desired state-change occurs (as shown in tom_I’s example).

Thanks guys but I think I should explain better.

This is the automation I use already:

alias: ALEXA Garagen Tor zu wenn offen
description: ''
trigger:
  - platform: state
    entity_id: script.garagen_tor_zu
    from: 'off'
    to: 'on'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.tor_offen
            state: 'on'
        sequence:
          - type: turn_on
            device_id: xxx
            entity_id: switch.tor
            domain: switch
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage geht zu.
      - conditions:
          - condition: state
            entity_id: binary_sensor.tor_offen
            state: 'off'
        sequence:
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage ist schon zu.
    default: []
mode: single

Now I want that after the automation did the first choose and the state of binary_sensor.tor_offen changed to off, I get an confirmation announce that it is closed.

For sure their are many solution to get this done maybe also with delay or wait service within the automation but even those I dont know to handle correct.

Because I dont want the announce triggered every time i closed the garage manual, a simple trigger for stet changed is not a solution.

Regrettably, I don’t understand the requirements of your second post. In addition, I don’t understand its relationship with the requirements of your first post. I think I will bow out and let others assist you. Sorry and good luck.

In short: Automation that closes a door, when door is closed sends me a feedback. My Idea was to run a script at the end of the automation which does the confirmation after the door is closed an therfor should repeat until state is fullfilled.

Perhaps you can borrow from this example:

1 Like

But… after the door’s closed the state IS fulfilled, isn‘t it?

This was meant for the script: Door closed → no → repeat script → door now closed? → yes → action announce.

Basicly this was my idea something like that. Thanks.

Here the final code for everybody:

alias: ALEXA Garagen Tor zu wenn offen
description: ''
trigger:
  - platform: state
    entity_id: script.garagen_tor_zu
    from: 'off'
    to: 'on'
condition: []
action:
  - choose:
      - conditions:
          - condition: state
            entity_id: binary_sensor.tor_offen
            state: 'on'
        sequence:
          - type: turn_on
            device_id: xxx
            entity_id: switch.tor
            domain: switch
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage geht zu.
          - repeat:
              until:
                - condition: state
                  entity_id: binary_sensor.tor_offen
                  state: 'off'
              sequence: []
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage ist jetzt zu.
      - conditions:
          - condition: state
            entity_id: binary_sensor.tor_offen
            state: 'off'
        sequence:
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage ist bereits zu.
    default: []
mode: single

This repeat does nothing because its sequence is empty.

          - repeat:
              until:
                - condition: state
                  entity_id: binary_sensor.tor_offen
                  state: 'off'
              sequence: []
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage ist jetzt zu.

See: repeat-until

I dont understand all this syntax. I did it with the Automation UI.

I can only tell yout that it works and does exactly what I wanted. The notify.alexa_media is called only after the state off is true.

It doesnt has to do any sequence. “Repeat the sequence UNTIL the conditions are true”

The automation keeps repeating an empty sequence, then when the UNTIL is true, it continues with the next action… doing the notify.alexa_media.

Based on what you just described, what you need is a wait_template .

What it’s doing is looping at very high speed (because it has nothing to do in each loop) until the binary_sensor is off. This is not a good application of repeat.

What you appear to want is for the action to wait until the binary_sensor if off and then send the notification. There is a straightforward and efficient way to do that in Home Assistant and that is the wait_template.

        sequence:
          - type: turn_on
            device_id: xxx
            entity_id: switch.tor
            domain: switch
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage geht zu.
          - wait_template: "{{ is_state('binary_sensor.tor_offen', 'off') }}"
          - service: notify.alexa_media
            data:
              data:
                type: announce
              target:
                - media_player.echo_wohnzimmer_2
                - media_player.echo_schlafzimmer_2
              message: Die Garage ist jetzt zu.

Thanks Taras. Seems like a better solution. Mine was really like spamming the host but worked.

I am just a rookie. Just started with HA a week ago.