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

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

Hi.
I have tried your solution. But what is repeated is only the first mp3. The second is only played once.
This is my config:

- id: gg_food_waste  
  alias: gg_food_waste
  trigger:
    platform: time
    at: '20:00:00'
  condition:
    condition: time
    weekday:
      - tue
      - thu
      - sat
  action:
    - repeat:
          count: 2
          sequence:
              - data:
                  entity_id: media_player.gm_mini
                  volume_level: '0.3'
                service: media_player.volume_set
              - data:
                  entity_id: media_player.gm_mini
                  media_content_id: '/media/local/annunciazione.mp3'
                  media_content_type: audio/mp3
                service: media_player.play_media 
              - wait_for_trigger:
                  - platform: state
                    entity_id: media_player.gm_mini
                    from: playing
                    to: idle
                timeout:
                  seconds: 10   
              - data:
                  entity_id: media_player.gmini
                  media_content_id: '/media/local/conf_umido.mp3'
                  media_content_type: audio/mp3
                service: media_player.play_media 
  mode: single  

Any idea? Thanks

the automation itself looks OK.

is there anything in the logs or in the automation trace to indicate why it’s failing?

You are using repeat - count with two iterations so here’s what that looks like. Steps 1-4 are the first iteration and steps 5-8 are the second one.

  1. Set volume
  2. Play annunciazione
  3. Wait for it to finish
  4. Play conf_umido
  5. Set volume
  6. Play annunciazione
  7. Wait for it to finish
  8. Play conf_umido

Notice how it waits for annunciazione to finish before it proceeds to play conf_umido.

In the second iteration, does it wait for conf_umido to finish playing before it proceeds to set volume and play annunciazione? No.

I suggest you fix that.

1 Like

good catch. I missed the lack of delay on the second iteration to wait for the second audio to stop playing.

Hi for all.
This works:

- id: gg_food_waste  
  alias: gg_food_waste
  trigger:
    platform: time
    at: '12:37:00'
  condition:
    condition: time
    weekday:
      - sun
      - thu
      - sat
  action:
    - repeat:
          count: 2
          sequence:
              - data:
                  entity_id: media_player.gm_mini
                  volume_level: '0.3'
                service: media_player.volume_set
              - data:
                  entity_id: media_player.gm_mini
                  media_content_id: '/media/local/annunciazione.mp3'
                  media_content_type: audio/mp3
                service: media_player.play_media 
              - wait_for_trigger:
                  - platform: state
                    entity_id: media_player.gm_mini
                    from: playing
                    to: idle
                timeout:
                  seconds: 10 
              - data:
                  entity_id: media_player.gm_mini
                  volume_level: '0.3'
                service: media_player.volume_set                  
              - data:
                  entity_id: media_player.gm_mini
                  media_content_id: '/media/local/conf_umido.mp3'
                  media_content_type: audio/mp3
                service: media_player.play_media 
              - wait_for_trigger:
                  - platform: state
                    entity_id: media_player.gm_mini
                    from: playing
                    to: idle
                timeout:
                  seconds: 10                 
  mode: single 

Here’s another way to do the same thing:

- id: gg_food_waste  
  alias: gg_food_waste
  trigger:
    - platform: time
      at: '12:37:00'
  condition:
    - condition: time
      weekday:
        - sun
        - thu
        - sat
  action:
    - variables:
        media: ['annunciazione','conf_umido']
    - repeat:
        count: 2
        sequence:
          - service: media_player.volume_set
            target:
              entity_id: media_player.gm_mini
            data:
              volume_level: '0.3'
          - service: media_player.play_media
            target:
              entity_id: media_player.gm_mini
            data:
              media_content_id: '/media/local/{{ media[repeat.index-1] }}.mp3'
              media_content_type: audio/mp3
          - wait_for_trigger:
              - platform: state
                entity_id: media_player.gm_mini
                from: playing
                to: idle
            timeout:
              seconds: 10 
  mode: single

Hi taras
This solution is very interesting.
The code is much more compact. When the files to be played are more than one, it is very useful.
Thanks

Just commenting on an old post, there is a much easier way to do it now if anyone doesn’t know it, reply and I will explain.

Trying to do something similar. Please explain.

When you are writing your automation in actions, select repeat, select number of times do your task in in the task include a step to wait 90 seconds.