Automation: (Loops) How to repeat an action 4 times ..at 90 second intervals

@finity I feel really bad for pulling you into this message. You’ve gone completely out of your way to help me. Please don’t feel obligated to help me. I don’t wantto take advantage of your kindness.

Having said that… and, taking a couple of steps back… Is there a reason why I can’t just stick to the original plan to use a single automation… using the repeat feature… then, use the until condition to stop it from repeating. If I understood right, the only reason we dumped the idea of using the repeat feature is because it would continue to repeat even if the condition isn’t true anymore.

we never dumped the repeat feature. :wink: You never said you wanted to use that method. You can use whichever method fits your needs and skill set.

And no I don’t mind helping at all and don’t feel obligated in any way. You’re just some “stranger on the internet” so If I wanted to walk away I could do so any time. :laughing:

At least your trying to work thru things and figure it out.

The people I really don’t like to help are the ones that don’t even try. And what’s worse are the ones who keep coming back over and over basically expecting you to write the code for them because they don’t want to put the effort in to learn. There’s been a few of those over the years.

Oh, and you don’t have to put the @username symbol in the post. Just hit the reply button in my post and it will notify me too.

Thanks. I was just trying to say is when you had mentioned that if I use the repeat feature, “…Realize tho that once this triggers and starts the count the actions will continue to run for 4 repeats even if the door gets closed in the meantime.”
That’s what turned me off from using that method; looking for a way that doesn’t have that limitation.

I had missed Tom_I’s comment that “That’s where you would use “repeat until”. e.g. repeat until the door is closed.” until noticed it just a little while ago.

Anyway, I added the until condition to the script below. I’m not sure if it’s right… I haven’t had a chance to test it; but, I’m hoping it does what I think it does…

- id: '1597611826726'
  alias: Back Door open warning
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 00:07:00
    from: 'off'
    platform: state
    to: 'on'
  action:
  - repeat:
      count: '4'
      sequence:
      - data:
          entity_id: media_player.living_room_speaker
          message: Please don't forget to close the back door!
        service: tts.google_translate_say
      - data:
          data:
            type: announce
          message: Please don't forget to close the back door!
          target:
          - media_player.mka_net_echo_dot
        service: notify.alexa_media
      - data:
          message: Please don't forget to close the back door!
        service: notify.sms_family
      - delay: 00:01:30
      until:
      - condition: state
        entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
        state: 'off'
  mode: single

1 Like

Looks reasonable.

The new automation functions are very new and I haven’t used them enough to be confident in them yet.

But looking at the docs I’m not sure that the “count:” option is available in the until structure. So you may be in a similar boat as you were before with it continuing to run until the door is closed instead of it running only 4 times.

But try it and see. It may just not be documented.

That example doesn’t pass Configuration Check on my system.
Screenshot from 2020-08-21 10-06-26

It should use only one way to repeat (loop) but it uses both count and until.

  • count is for repeating a given number of times (4) in your example.
  • until is for repeating until some condition is met.

Use one or the other, not both.

Thanks 123. I’ll remove the count and keep the until.

finity, using repeat… until works great (after removing count). This is been a great learning experience. I appreciate your guidance. I still plan on getting my alexa/google notifiers setup later in case I ever need to use HA alerts.

1 Like

Hi there,
I had a similar automation that neatly emitted a notification if the door was open that I wanted to adjust so that this notification is repeated if the door is left open.
Now that I’ve added ‘repeat until’, however, I get an error

type oInvalid config for [automation]: expected dict for dictionary value @ data['action'][1]['repeat']['sequence'][0]['data']. Got None extra keys not allowed @ data['action'][1]['repeat']['sequence'][0]['message']. Got None extra keys not allowed @ data['action'][1]['until']. Got None. (See ?, line ?).r paste code here

Here’s my automation file (entities\automations\kitchen\open_door.yaml :

alias: 'deur_open'
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_keuken_deur_01
    from: 'off'
    to: 'on'
    for: '00:00:30'

condition:
  - condition: state
    entity_id: binary_sensor.sonoff_keuken_deur_01
    state:  'off'

mode: single

action:
- service: media_player.volume_set
  data_template:
    entity_id: media_player.keuken
    volume_level: 0.8

- alias: herhaal bericht totdat de deur gesloten is
- repeat:
#    count: '4'
    sequence:
      - service: tts.google_say
        data:
          entity_id: media_player.keuken
          message: 'Hallo! Graag de deur dicht doen, anders wordt de verwarming uitgeschakeld'
      - delay: '00:00:10'  
  until:
    - condition: state
      entity_id: binary_sensor.sonoff_keuken_deur_01
      state:  off

Any help is appreciated

Hi @Plaatjesdraaier . I wish I knew how to help you with the error you’re getting. Does that error only happen after you add “repeat until”? Your code looks very similar to mine. This probably requires someone with a deeper knowledge of Home Assistant than me such as @finity

My original flle looks like this and is working fine:

alias: 'deur_open'
initial_state: 'on'
trigger:
  - platform: state
    entity_id: binary_sensor.sonoff_keuken_deur_01
    to: 'on'
    for: '00:0:30'
  - platform: state
    entity_id: binary_sensor.sonoff_hk_terras_deur
    to: 'on'
    for: '00:0:30'    

action:

  - service: media_player.volume_set
    data_template:
      entity_id: media_player.keuken
      volume_level: 0.8  
  - service: tts.google_say
    data:
      entity_id: media_player.keuken
      message: 'Hallo! Graag de deur dicht doen, anders wordt de verwarming uitgeschakeld'
mode: single

I tried to adjust the code so that the message is repeated using ‘repeat until’ but no luck so far.

I’m very bad with yaml. However, I’ve found out that Home Assistant is extremely picky sometimes (for no apparent reason) when it comes to which items are chosen to be preceded by a hyphen in yaml code.

The main difference I see between the code I’m using (below) and the one you’re using is in the sequence… you’re adding a hyphen to - service instead of - data. Try making your code as close to mine as possible by taking off the hyphens from service… and adding hyphens to data instead. Maybe, that’s all that you need to do?

- id: '1598025836523'
  alias: 'Back Door open warning:  Alexa/Google'
  description: Alexa/Google voice message after back door has been open for more than
    7 minutes
  trigger:
  - entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
    for: 00:07:00
    from: 'off'
    platform: state
    to: 'on'
  action:
  - repeat:
      sequence:
      - data:
          volume_level: 0.7
        service: media_player.volume_set
        entity_id: media_player.living_room_speaker
      - data:
          entity_id: media_player.living_room_speaker
          message: Please remember to close the back door!
        service: tts.google_translate_say
      - data:
          volume_level: '0.8'
        service: media_player.volume_set
        entity_id: media_player.kookoo_s_echo_flex
      - data:
          data:
            type: announce
          message: Please remember to close the back door!
          target:
          - media_player.mka_net_echo_dot
          - media_player.kookoo_s_echo_flex
        service: notify.alexa_media
      - delay: 00:02:00
      until:
      - condition: state
        entity_id: binary_sensor.ecolink_door_window_sensor_sensor_2
        state: 'off'
  mode: single

Your indentation is off.

look at the example here and make yours look the same.

My indentation is different from the example because I have my configuration split up.
I use different files for automation :

automation: !include ../automations.yaml
automation jja: !include_dir_list ../entities/automations/

Please try using an alert like someone suggested at the top of them thread. The hard work has already been done for you.

Tried it, but unfortunately a new error:

Invalid config for [automation]: extra keys not allowed @ data['action'][0]['repeat']['until'][0]['mode']. Got None must contain at least one of service, service_template. @ data['action'][0]['repeat']['sequence'][0]. Got None must contain at least one of service, service_template. @ data['action'][0]['repeat']['sequence'][1]. Got None. (See ?, line ?).

Tried it, but no luck either.

the indentation i was referring to has nothing at all to do with the splitting up of your config.

It has to do with the syntax of the code for the “repeat” section of your automation as you had it posted.

Did you even look at the link I posted?

If you did then you would see that your “repeat” code syntax (indentation) is not the same as the example given there.

Offcourse I looked at the link you posted, but I just couldn’t get it right.

action:
  - service: media_player.volume_set
    data:
      entity_id: media_player.keuken
      volume_level: 0.8

  - alias: herhaal bericht totdat de deur gesloten is
    repeat:
    #     count: '4'
      sequence:
        - service: tts.google_say
          data:
            entity_id: media_player.keuken
            message: 'Hallo! Graag de deur dicht doen, anders wordt de verwarming uitgeschakeld'
        - delay: '00:00:10'  
      until:
        - condition: state
          entity_id: binary_sensor.sonoff_keuken_deur_01
          state:  'off'

and you also don’t need the “alias” portion in the repeat section. It’s only there as a description for commenting.

action:
  - service: media_player.volume_set
    data:
      entity_id: media_player.keuken
      volume_level: 0.8

  - repeat:
      sequence:
        - service: tts.google_say
          data:
            entity_id: media_player.keuken
            message: 'Hallo! Graag de deur dicht doen, anders wordt de verwarming uitgeschakeld'
        - delay: '00:00:10'  
      until:
        - condition: state
          entity_id: binary_sensor.sonoff_keuken_deur_01
          state:  'off'

I also changed “data_template” to “data:” since the “_template” is no longer needed.

And you were missing quotes (') around the “off” state.

1 Like

Thank you!
Seems I haven’t paid attention. You’re my hero!

1 Like