Starting a Script Sequence With A For Loop

I have a script to control the LED bar on Inovelli switches, dimmers, and combo fan / light dimmers. At the moment I have to call it separately for each type of device (switches, dimmers, the fan portion of the combo device, and the light portion of the combo device). Goal: re-write the script so it can determine the device type of each entity and can be called once with mixed entity types. Ultimately, I’d like to call the script with an area and use area_entities(). I can’t assume all entities are devices of the same model and the parameters in Z-Wave JS are different for each model.

Here’s a simplified version of the script:

mode: parallel
max: 10

fields:
  entity:
    name: Entity
    description: The light.* entity for the LED we're setting.  Can be a comma separated list.
    required: yes
    example: light.office, light.guest_room
    selector:
      entity:
        integration: zwave_js
        domain:
          - light
          - fan
        multiple: true

  model:
    name: Model
    description: What type of device is this?
    required: yes
    example: Dimmer
    selector: 
      select:
        options:
          - Dimmer
          - Switch
          - Combo_Light
          - Combo_Fan

  LEDcolor:
    name: LED Color (non-effect)
    description: Sets the color of the LED indicating brightness levels
    required: no
    example: Blue
    selector:
      select:
        options:
          - "Off"
          - Red
          - Green
          - Blue

  parameters:
    "dimmer_effect_bulk": 16
    "dimmer_effect_color": "LED Indicator: Effect Color"

    "switch_effect_bulk": 8
    "switch_effect_color": "LED Effect Color"

    "combo_light_effect_bulk": 24
    "combo_light_effect_color": "Light LED Effect Color"

    "combo_fan_effect_bulk": 25
    "combo_fan_effect_color": "Fan LED Effect Color"

sequence:

##################
# LED strip color
##################
  - choose:
    - conditions: >
        {{ LEDcolor != "no change" }}
      sequence:
        - service: zwave_js.set_config_parameter
          data:
            entity_id: '{{ entity }}'
            parameter: |
              {% set effect_param = model + '_ledcolor' %}
              {{ parameters[effect_param] }}
            value: |
              {{ color_set[LEDcolor] }}

I thought I could use a for() loop to handle each entity one at a time, like this:

sequence:

  {% for entity in area_entities('office') %}
    {% if is_device_attr(entity, 'manufacturer','Inovelli') %}
      {% set model = device_attr(device_id(entity),'model') %}

##################
# LED strip color
##################
        - choose:
          - conditions: >
              {{ LEDcolor != "no change" }}
            sequence:
              - service: zwave_js.set_config_parameter
                data:
                  entity_id: '{{ entity }}'
                  parameter: |
                    {% set effect_param = model + '_ledcolor' %}
                    {{ parameters[effect_param] }}
                  value: |
                    {{ color_set[LEDcolor] }}
      {% endif %}
  {% endfor %}

However, that results in an error pointing to the start of the for() loop.

Error loading /config/configuration.yaml: while scanning for the next token
found character ‘%’ that cannot start any token
in “/config/scripts/inovelli_led_zwavejs.yaml”, line 342, column 4

There must be a way to do this, but I can’t find the right documentation. I’m not a software engineer but if you type slowly I might understand.

The for_each action in next month’s release (2022.5) could be used do this. If you are interested you can join the beta program and test it now. https://rc.home-assistant.io/blog/2022/04/27/release-20225/#for-each

Though please confine any discussion of the beta features to the Discord Home Assistant beta channel.

Thanks for the reply! I’ll try to check it out this weekend and mark it as the solution when I get it figured out.

The reason for the error is because what you posted contains invalid syntax. It’s YAML buried inside of a Jinja2 template and that’s simply wrong. YAML can contain a Jinja2 template, not the other way around.

Currently I use a Counted Repeat to iterate through all items in a list. The value of the count option is simply the list’s length. Each item in the list is referenced using repeat.index.

Although it’s a simple technique, it’ll get even simpler in the next release (as mentioned by tom_l).