Loop through dynamically generated color list

So after some YAML brush-up, I’ve managed to generate myself a list of colors to pass to a script. In abbreviated form:

data:
  color_name: |
    {{ colors }}

In human terms, {{colors}} looks like “[red, green]”, or “[red, blue, yellow]”, etc.

Now though, I’m drawing a blank as how to step through the list. Ideally, a single firing of the script would step through each color in the list, however many, with appropriate transition times, etc. I’ve seen a number of examples where people do something similar with input_select.select_next, but my list needs to be generated by YAML on the fly.

Has anybody tackled anything similar?

Use a Counted Repeat to step through the list. The count will simply be the list’s length. Easy peasy.

Basic example:

script:
  set_colors:
    sequence:
      - variables:
          colors_qty: "{{ color_names | count }}"
      - condition: "{{ colors_qty > 0 }}"
      - repeat:
          count: "{{ colors_qty }}"
          sequence:
            - service: light.turn_on
              target:
                entity_id: light.whatever
              data:
                color_name: "{{ color_names[repeat.index - 1] }}"
            - delay: '00:00:30'
1 Like

I knew I had seen something like this somewhere. Bless you. Got it working!

1 Like