Repeat until a count number

I would like to repeat the script (script.watch_news) by 5 times by using the count. How to do the coding? Thanks.

action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
    data:
      volume_level: 1
  - service: script.watch_news

https://www.home-assistant.io/docs/scripts#repeat-a-group-of-actions

Hi Tom,

Meaning i need to create another script (flash_hallway_light) to do the count looping, right?

count: "{{ count|int * 2 - 1 }}"
count|int * 2 - 1 meaning what?

  flash_light:
    mode: restart
    sequence:
      - service: light.turn_on
        target:
          entity_id: "light.{{ light }}"
      - alias: "Cycle light 'count' times"
        repeat:
          count: "{{ count|int * 2 - 1 }}"
          sequence:
            - delay: 2
            - service: light.toggle
              target:
                entity_id: "light.{{ light }}"
  flash_hallway_light:
    sequence:
      - alias: "Flash hallway light 3 times"
        service: script.flash_light
        data:
          light: hallway
          count: 3

Nope. Automations are just scripts with a trigger. The syntax is identical for both. You can use the repeat syntax in your automation actions.

But using your example you can create different automations to control different lights and counts by running the same script with different variables sent to it.

count: "{{ count|int * 2 - 1 }}"

I’m not sure why you are using that code to set the count. Is that code important? It seems you might have just copied that code from an example somwhere?

but basically it means take the variable count (3 in your case) multiply it by 2 and subtract 1 from it so the result is 5.

IMO, if you want a count of 5 just use 5 as the value of “count” instead of 3. Then just use the variable without the math.

I want to loop 3 times, but there is error message in the script. I believe there is syntax error.
Can u help me to correct it?

action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
    data:
      volume_level: 1
    count: 3
  - repeat:
      sequence:
        - count: '{{ count|int - 1 }}'
        - service: script.speaker_play_media
        - delay:
            seconds: 7
      until:
        - condition: template
          value_template: '{{ count | int = 0 }}'

It would help if you included the actual error. But it looks like you have your count in the wrong place and you dont need an until if you just want to loop for a count of three times. Try this:

action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
    data:
      volume_level: 1
  - repeat:
      count: 3
      sequence:
        - service: script.speaker_play_media
        - delay:
            seconds: 7

Thanks Tom, the repeat count solve my repetitive problem.

Have you considered using a wait template instead of a delay after the script call?

  - repeat:
      count: 3
      sequence:
        - service: script.speaker_play_media
        - wait_template: "{{ is_state('media_player.living_room_speaker', 'idle') }}"

This way you don’t have to adjust the delay for different play lengths.

I just tried the wait_template, but it only play the media for 1 time, it never repeat.

alias: Watch 8pm news
description: ''
mode: single
trigger:
  - platform: time
    at: '19:55:00'
condition: []
action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
    data:
      volume_level: 1
  - repeat:
      count: '5'
      sequence:
        - service: script.speaker_play_media
        - wait_template: "{{ is_state('media_player.living_room_speaker', 'idle') }}"

Watch the state of media_player.living_room_speaker when you trigger the automation. Does it change from playing to idle or something else?

before i start the automation, the state of media_player.living_room_speaker is idle, after i start the automation, it showed playing, then it will show idle again. So, there is no repeat at all.

Weird. What happens if you do this:

action:
  - service: media_player.volume_set
    target:
      entity_id: media_player.living_room_speaker
    data:
      volume_level: 1
  - repeat:
      count: '5'
      sequence:
        - service: script.turn_on
          entity_id: script.speaker_play_media
        - wait_template: "{{ is_state('media_player.living_room_speaker', 'idle') }}"

The difference is documented here: https://www.home-assistant.io/integrations/script/#waiting-for-script-to-complete

I would have thought that the way you had it originally would have been the one to work, but if you feel like humouring me try the script.turn_on service. Or if you can’t be bothered just go back to your delay that works.

same, only play the script.speaker_play_media for 1 time only.

:man_shrugging:

It’s ok, i will just use delay. Thanks for help

You can also use a number helper in this case and add it to automation or script

repeat:
  count: "{{states['input_number.cast_kidsroom_music_bedtime_play_times'].state|int}}"