Same script, slightly different behaviour ..?

I use the following script to cycle through some different colours on an RGB strip and a separate RGB bulb to give a nice effect as I walk into a room.

This is the script:

alias: Colour Loop Lights
variables:
  colours: |-
    {{ [[0.217,0.077], [0.157,0.05], [0.136,0.04], [0.137,0.065],
        [0.141,0.137], [0.146,0.238], [0.151,0.343], [0.157,0.457],
        [0.164,0.591], [0.17,0.703], [0.172,0.747], [0.199,0.724],
        [0.269,0.665], [0.36,0.588], [0.444,0.517], [0.527,0.447],
        [0.612,0.374], [0.677,0.319], [0.701,0.299], [0.667,0.284],
        [0.581,0.245], [0.477,0.196], [0.385,0.155], [0.301,0.116]] }}
  startingseed: "{{range(0,(colours | count)-1) | random}}"
sequence:
  - repeat:
      count: "{{ counter if counter is defined else 20 }}"
      sequence:
        - service: light.turn_on
          data_template:
            xy_color: "{{ colours[((startingseed+repeat.index) % (colours | count))] }}"
            transition: "{{ transition if transition is defined else 1 }}"
          target:
            entity_id: >-
              {{ targetid if targetid is defined else
              'light.kitchen_light_group' }}
        - wait_template: >
            {{ state_attr(targetid,'xy_color')| list ==
            colours[((startingseed+repeat.index) % (colours | count))] }}
          continue_on_timeout: true
          timeout: "1"
mode: single

When run against the RGB bulb, it executes much faster as the bulb doesn’t seem to respect the transition value, completing every step in 2.01 seconds. Visually, it looks like the bulb is skipping some colours, rather than transitioning very quickly.

Now I run the same script targetting the RGB LED strip and it runs in 6.25 seconds, with a much nicer transition appearance.

However, given a repeat count of 10, and a transition time of 1 for each light turn on, I would expect both runs to take about 10 seconds to complete.

I tried to ‘even out’ the timings of both runs by making the entity wait to confirm the light was at its correct colour, but that didnt seem to do anything.

Any thoughts on how to make both lights execute as I would expect ? I thought about putting a 1 second delay in the loop, but that seems like a hack.

To answer my own question, it seems that my expectation that the light turn on and transition delay would fully complete before the loop started its next iteration was incorrect.

Looking in the docs:

        repeat:
          sequence:
            # Run command that for some reason doesn't always work
            - service: shell_command.turn_something_on
            # Give it time to complete
            - delay:
                milliseconds: 200

Its even suggesting the use of delay to ensure the action has completed before the next iteration of the loop fires.

So I’ve added a 1000 millisecond delay inside the loop, and it does appear to be running as expected. However, it regularly shows a loop iteration taking 2 seconds, and sometimes 6 seconds which is suggesting that at least the sending of the light on command can hold up the loop sometimes.

The value of delay should be set to the value of transition.

In other words, after instructing a light to transition to a color in X seconds, you want repeat to wait X seconds before transitioning to the next color.

@123 - Thats exactly what I ended up doing, setting a delay of 1000 milliseconds for a transition of 1 second.

Although I’m not sure thats enough, as in my loop, I’m actually targeting a light group of three LED strips, and I think sometimes that can actually take slightly longer 1second to set all three.