Repeat not starting or not finishing the whole count

I’m trying to start a repeat in a script with a specific count:

alias: Ziggo Next Startup
sequence:
  - if:
      - condition: state
        entity_id: switch.rack_pdu_ziggo_next
        state: "off"
    then:
      - data: {}
        target:
          entity_id:
            - switch.rack_pdu_ziggo_next
        action: switch.turn_on
      - delay:
          hours: 0
          minutes: 0
          seconds: 10
          milliseconds: 0
  - repeat:
      count: 20
      sequence:
        - choose:
            - conditions:
                - condition: state
                  entity_id: media_player.mediabox
                  state:
                    - unknown
              sequence:
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 20
                    milliseconds: 0
            - conditions:
                - condition: state
                  entity_id: media_player.mediabox
                  state:
                    - unavailable
              sequence:
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 30
                    milliseconds: 0
            - conditions:
                - condition: state
                  entity_id: media_player.mediabox
                  state:
                    - "off"
              sequence:
                - action: media_player.turn_on
                  metadata: {}
                  target:
                    entity_id: media_player.mediabox
                  data: {}
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 10
                    milliseconds: 0
            - conditions:
                - condition: state
                  entity_id: media_player.mediabox
                  state:
                    - standby
              sequence:
                - action: media_player.turn_on
                  metadata: {}
                  target:
                    entity_id: media_player.mediabox
                  data: {}
                - delay:
                    hours: 0
                    minutes: 0
                    seconds: 10
                    milliseconds: 0
            - conditions:
                - condition: or
                  conditions:
                    - condition: state
                      entity_id: media_player.mediabox
                      state:
                        - "on"
                    - condition: state
                      entity_id: media_player.mediabox
                      state:
                        - playing
              sequence: []
          default:
            - delay:
                hours: 0
                minutes: 0
                seconds: 30
                milliseconds: 0
    continue_on_error: true
mode: restart
icon: mdi:auto-fix

But I don’t ever see the count reaching 20.
Sometimes it does not even start the repeat
It went through the repeat just once
Most I saw 9 times…

I don’t see anything weird in the trace or logs. Where to troubleshoot any further?

Isn’t 9 the max you see on the trace graphic overview?
As I remember it just counts up to 9 and then it stops there but if you look in the actual trace then it has run.

Or did you look in the text based trace to confirm it just does 9 runs?

This was an occasion where it didn’t even start the repeat.
Translating dutch-english:
This step hasn’t been executed and therefore there’s no further trace info available.

Your if sequence is missing an else, that’s why it’s not executing the step.

I’ve used it similarly in other automations. I don’t think the “else” should be necessary (occasionally it runs the repeat at least once)

Could you try and add a counter helper and increment it in the loop.
That should give us a clear sign how many iterations are made

At first, around 16:07 I started the script.
It probably resetted to 0, but was 0 already so no visuals.

Then I changed the value manually to 7 to see changes.
I’ve run the script twice now (while the mediaplayer was already on/playing).
You see that it resets and immediately goes to 20 (as the option where the media player is on has no wait).

It looks like the first part is breaking the script. But all actions seem to be valid.
It checks a PDU outlet (which is there)
Then turns on that PDU outlet (which in real world it actually does)
Then wait a bit

Then the repeats…
If it skips the PDU thing, it looks like it is going through the repeats…

You have the script set to mode restart. So if the same script is triggered again before the count is 20 the loop gets aborted. The same happens if scripts are reloaded or HA is restarted.

In general you should avoid long running scripts and automations and use an event driven approach instead.

1 Like

I started with this script a while ago, where the mediaplayer would not turn on after a power on-command every time (or too soon after pdu power on).

You are right though…

I changed the automation where the script was triggered from and added the mediaplayer-status as trigger. (checked that the script was only triggered once)

Perhaps a firmware update (must have been 50+ in the meantime) fixed the ability to turn on quickly when power had been applied.

Although the “repeat” should work as intended, I’m not using it anymore now.