Calling script from repeat loop

Hey guys, I am trying to run a script via the repeat action within an automation but it only runs once.

alias: TEST
description: ''
trigger:
  - platform: sun
    event: sunset
condition: []
action:
  - repeat:
      count: '2'
      sequence:
        - service: script.wled_routine
          data: {}
        - delay: '00:05:00'
mode: single

It should run the script twice, separated by five minutes, right?

I see this in the logs:

TEST: Repeat at step 1: Error executing script. Unexpected error for call_service at pos 1: coroutine raised StopIteration
TEST: Error executing script. Unexpected error for repeat at pos 1: coroutine raised StopIteration
While executing automation automation.test

Does anyone have an idea why the repeat is failing to run?

I think it may be something in script, but I don’t know what. Sometimes it errors, sometimes it doesn’t.

Here’s the script:


alias: WLED routine
sequence:
  - data_template:
      entity_id: all
      effect: '{{ state_attr("light.wled_living_room", "effect_list") | random }}'
      intensity: '{{ (range(50, 200)|random) }}'
    service: wled.effect
  - data_template:
      entity_id: light.wled_living_room
      speed: '{{ (range(50, 200)|random) }}'
      palette: '{{state_attr("input_select.wled_palettes", "options") | random }}'
    service: wled.effect
  - data_template:
      entity_id: light.wled_balcony_upper
      speed: '{{ (range(50, 200)|random) }}'
      palette: '{{state_attr("input_select.wled_palettes", "options") | random }}'
    service: wled.effect
  - data_template:
      entity_id: light.wled_balcony_lower
      speed: '{{ (range(50, 200)|random) }}'
      palette: '{{state_attr("input_select.wled_palettes", "options") | random }}'
    service: wled.effect
  - data_template:
      entity_id: light.wled_tree
      speed: '{{ (range(50, 200)|random) }}'
      palette: '{{state_attr("input_select.wled_palettes", "options") | random }}'
    service: wled.effect
  - service: media_player.play_media
    data:
      media_content_id: 'http://192.168.1.24:8123/local/audio/sounds/discord_ping.mp3'
      media_content_type: audio/mp3
    entity_id: media_player.living_room_speaker
  - service: notify.lg_tv
    data:
      message: WLED single run compete
mode: restart
icon: 'mdi:led-strip-variant'
max: 5
  
  

Try:

action:
  - repeat:
      count: '2'
      sequence:
        - service: script.turn_on
          entity_id: script.wled_routine

Reason: https://www.home-assistant.io/integrations/script/#waiting-for-script-to-complete

1 Like

Awesome, thanks Tom.

Can you explain how that reason affects this situation (only one iteration when there ought to be two)?

This calls the script and waits for it to finish before executing the delay and waiting for 5 minutes.

service: script.wled_routine

This calls the script and immediately executes the delay and waits for 5 minutes.

service: script.turn_on
entity_id: script.wled_routine

How does that affect the number of iterations performed? As far as I can tell, the wled_routine script contains no delays and should execute and complete quickly (i.e. well within the 5 minutes before the next iteration should occur).

What have I missed here?

No, no I can not.

It was just something I thought worth a try.

I probably should have said “possible reason”.

OK :slight_smile: