Help converting a loop into actions using lists

I'm trying to set up a script that briefly flashes light(s) a color as a means of notification. I need help converting this loop into a single sequence that uses lists for entities to perform each action for all targets at once instead of onesies with the looping structure.

sequence:
  - repeat:
      for_each: "{{ target_light_entities }}"
      sequence:
        - action: scene.create
          metadata: {}
          data:
            scene_id: light_before_notification
            snapshot_entities:
              - "{{ repeat.item }}"
          enabled: true
        - action: light.turn_on
          metadata: {}
          target:
            entity_id: "{{ repeat.item }}"
          data:
            transition: 0.001
            color_name: "{{ color_name }}"
          enabled: true
        - delay:
            hours: 0
            minutes: 0
            seconds: 0
            milliseconds: 500
        - action: scene.turn_on
          metadata: {}
          target:
            entity_id: scene.light_before_notification
          data:
            transition: 0.001
          enabled: true
    enabled: true
alias: Light notification (via loops)
description: "Briefly flash light(s) a color as a means of notification."
icon: mdi:lightbulb-on-outline
fields:
  target_light_entities:
    selector:
      entity:
        filter:
          - domain: light
        multiple: true
    name: Target Light Entities
    default:
      - light.corner_lamp
      - light.office_light
  color_name:
    selector:
      text: null
    default: Cyan
    description: Cyan, Red, or any other system level color name.
mode: parallel
max: 10

I am trying to learn the syntax for handling lists but haven't managed to get anything working yet. Perhaps all I need is how to generate a string list of entities from the target_light_entities variable in another script I'm experimenting with. I've tried many variations of the following for the Create Scene action but everything I've tried errors.

sequence:
  - action: scene.create
    metadata: {}
    data:
      scene_id: light_before_notification
      snapshot_entities: |-
        {%- for targetLight in target_light_entities -%}
         - {{ targetLight }}
        {% endfor -%}
    enabled: true
alias: Light notification (via lists instead of loops)
description: Briefly flash light(s) a color as a means of notification.
icon: mdi:lightbulb-on-outline
fields:
  target_light_entities:
    selector:
      entity:
        filter:
          - domain: light
        multiple: true
    name: Target Light Entities
    default:
      - light.corner_lamp
      - light.office_light
  target_areas:
    selector:
      area:
        multiple: true
    name: Target Areas
    default:
      - office
  color_name:
    selector:
      text: null
    default: Cyan
    description: >-
      Cyan, Red, or Blink (briefly toggle light on or off depending on current
      state)
mode: parallel
max: 10

Relevant documentation:

This isn't creating a list, it's creating a list-shaped string with a bunch of dashes and line returns in it... just use the variable:

      snapshot_entities: "{{ target_light_entities }}"

I doubt that 0.001 is a valid value for transition, many lights only support whole-number seconds transitions between 0-10... you might as well just use 0 to avoid issues. Also, if you actually plan on using the defaults, it's usually best to

alias: Light notification (via entity list)
description: "Briefly flash light(s) a color as a means of notification."
icon: mdi:lightbulb-on-outline
fields:
  target_light_entities:
    selector:
      entity:
        filter:
          - domain: light
        multiple: true
    name: Target Light Entities
    default:
      - light.corner_lamp
      - light.office_light
  color_name:
    selector:
      text: null
    default: Cyan
    description: Cyan, Red, or any other system level color name.
sequence:
  - alias: These aren't strictly necessary, but they do help protect against an edge case where the script is called with no data.
    variables:
      target_light_entities: "{{ target_light_entities | default(['light.corner_lamp','light.office_light'], true)}}"
      color_name: "{{ color_name | default('Cyan',true) }}"
  - action: scene.create
    metadata: {}
    data:
      scene_id: light_before_notification
      snapshot_entities: "{{ target_light_entities }}"
    enabled: true
  - action: light.turn_on
    metadata: {}
    target:
      entity_id: "{{ target_light_entities }}"
    data:
      color_name: "{{ color_name }}"
      transition: 0
    enabled: true
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 500
  - action: scene.turn_on
    metadata: {}
    target:
      entity_id: scene.light_before_notification
    data:
      transition: 0
mode: parallel
max: 10

I thought I read in some post somewhere that some actions couldn't accept a list variable and had to be a list-shaped string. dunno if I misunderstood something or it was outdated advice but simply using the list variable works fine for all the actions I tried.

as for the transition, not having one was not instant (lights seem to have some kind of default transition for fading on/off) so I had to add something. I assumed HA would automatically disable the transition setting if not >0 (it frustratingly auto-disables the setting and blocks the field when you try to change the number because you briefly clear the field) but I just actually tried it and it will hold the 0.

the bonus handling of blank inputs is a great idea but I was just using defaults to speed up testing so I made the variables required.

so big double-thanks for the help, @Didgeridrew !

also of note, apparently some lights will not respond to subsequent commands that come in "too quickly" so at least for one of my lights I had to use a kinda high delay so that's something else to keep in mind.

here's the final version.

alias: Light notification
description: Briefly flash light(s) a color as a means of notification.
icon: mdi:lightbulb-on-outline
fields:
  target_light_entities:
    selector:
      entity:
        filter:
          - domain: light
        multiple: true
    default: []
  color_name:
    selector:
      text: null
    default: Cyan
    description: Cyan, Red, or any other system level color name.
    required: true
sequence:
  - variables:
      delay_milliseconds: 900
  - sequence:
      - action: scene.create
        metadata: {}
        data:
          scene_id: light_before_notification
          snapshot_entities: "{{ target_light_entities }}"
        enabled: true
      - action: light.turn_on
        metadata: {}
        target:
          entity_id: "{{ target_light_entities }}"
        data:
          transition: 0
          color_name: "{{ color_name }}"
        enabled: true
      - delay:
          hours: 0
          minutes: 0
          seconds: 0
          milliseconds: "{{ delay_milliseconds }}"
      - action: scene.turn_on
        metadata: {}
        target:
          entity_id: scene.light_before_notification
        data:
          transition: 0
        enabled: true
mode: parallel
max: 10

As far as I'm aware, no action accepts entity IDs in a "list-shaped string" like what you had. There are, however, a few different things that are accepted... which may have contributed to your misunderstanding:

#The standard: a YAML list
action: light.turn_on
target:
  entity_id: 
    - light.gledopto_lightbar_01
    - light.behind_tv
# A JSON list
action: light.turn_on
target:
  entity_id: ["light.gledopto_lightbar_01", "light.behind_tv"]
# Comma-separated strings
action: light.turn_on
target:
  entity_id: light.gledopto_lightbar_01, light.behind_tv
1 Like