Generating a List of Entities From area_entities() vs device_entities()

I have a script to control the LED bar on Inovelli dimmers and other devices. In Home assistant 2022.5.3 I added functionality to receive an area ID, discover any Inovelli devices in the area, and configure the LED and LED effects on those devices according to other parameters I’d passed. After updating 2022.6.2 I started getting errors when passing areas to the script. I’ve been through the breaking changes for 2022.6 and I was already running the 6.11.0 zwavejs2mqtt Docker container. I don’t see anything else that would apply.

2022-06-05 14:28:48 ERROR (MainThread) [homeassistant.components.script.inovelli_led_zwavejs] Inovelli Dimmer and Switch LEDs ZwaveJS: Error executing script. Error rendering template for variables at pos 1: TemplateError: str: Must provide a device or entity ID
2022-06-05 14:28:48 ERROR (MainThread) [homeassistant.helpers.script.websocket_api_script] websocket_api script: Error executing script. Error rendering template for call_service at pos 1: TemplateError: str: Must provide a device or entity ID
2022-06-05 14:28:48 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection] [140441646101312] Error handling message: TemplateError: str: Must provide a device or entity ID (unknown_error)

I believe the issue is with the code that discovers Inovelli entities in an area because I can pass a device ID or entity and the script works fine. I’m not completely sure I’m looking in the right spot though. The two sections of code are nearly identical.

Code to discover dimmer entities from an AREA ID give me the errors above:

      area_dimmer: >
        {% set entities = namespace(areas=[]) %}
        {% for dev in area %}
          {% for ent in area_entities(dev) %}
            {% if is_device_attr(device_id(ent),'model', 'LZW31-SN') and ent.split('.')[0] == 'light' %}
              {% set entities.areas = entities.areas + [ent | string] %}
            {% endif %}
          {% endfor %}
        {% endfor %}
        {{ entities.areas|lower }}

Code to discover dimmer entities from a DEVICE ID works just fine:

      device_dimmer: >
        {% set entities = namespace(devices=[]) %}
        {% for dev in device %}
          {% for ent in device_entities(dev) %}
            {% if is_device_attr(device_id(ent),'model', 'LZW31-SN') and ent.split('.')[0] == 'light' %}
              {% set entities.devices = entities.devices + [ ent | string] %}
            {% endif %}
          {% endfor %}
        {% endfor %}
        {{ entities.devices|lower }}

In the templating sandbox, this code:

{% set area = ['62d97e01b9124f309bdf8a897547071d'] %}
{% set entities = namespace(areas=[]) %}
{% for dev in area %}
  {% for ent in area_entities(dev) %}
    {% if is_device_attr(device_id(ent),'model','LZW31-SN') and ent.split('.')[0] == 'light' %}
      {% set entities.areas = entities.areas + [ent | string] %}
    {% endif %}    
  {% endfor %}
{% endfor %}
{{ entities.areas|lower }}

gives me this error TemplateError: str: Must provide a device or entity ID which seems related to the errors I get from the script. If I switch

{% if 'LZW31-SN' == device_attr(device_id(ent),'model') and ent.split('.')[0] == 'light' %}
to:
{% if ent.split('.')[0] == 'light' and 'LZW31-SN' == device_attr(device_id(ent),'model') %}

it renders just fine in the template sandbox. This doesn’t work in the script, however, so maybe I’m way off track. I’ve tried removing the if statement entirely and even replacing [ent | string] with ['light.outside_rear_lights_level' | string ] but I’m still getting the three errors at the top.

I’m not a programmer or expert of any kind, but if you type real slowly I’ll try to keep up. I’d be thankful for any guidance.

Stripping the script down to basic functionality makes me think the issue is indeed somewhere in the area_dimmer variable definition. I get the same errors with this version of the script:

alias: Inovelli Testing
description: 'Handles setting the LED colors and notifications on Inovelli "Red" model switches and dimmers through the Zwave JS integration with zwavejs2mqtt 2.4.0 using webhooks.'
mode: parallel
max: 100

fields:
  area:
    name: Area
    description: Areas contaiting Inovelli devices.
    required: no
    #example:
    selector:
      area:
        multiple: true
        device:
          manufacturer: Inovelli
          integration: zwave_js
          #model: LZW36, LZW30-SN, LZW31-SN

  device:
    name: Device
    description: Inovelli LZW36, LZW30-SN, LZW31-SN device IDs.  Mix and match as you like
    required: no
    #example:
    selector:
      device:
        multiple: true
        manufacturer: Inovelli
        integration: zwave_js
        #model: LZW36, LZW30-SN, LZW31-SN

  LEDcolor:
    name: LED Color (non-effect)
    description: Sets the color of the LED indicator brightness levels
    required: no
    example: Blue
    selector:
      select:
        options:
          - "Off"
          - Red
          - Orange
          - Yellow
          - Green
          - Cyan
          - Teal
          - Blue
          - Purple
          - Light Pink
          - Pink
          - Hot Pink
          - White

variables:
  color_set:
    "off": 0
    "red": 0
    "orange": 8
    "yellow": 42
    "green": 85
    "cyan": 127
    "teal": 145
    "blue": 170
    "purple": 195
    "light pink": 220
    "pink": 234
    "hot pink": 234
    "white": 255

  parameters:
    "dimmer_ledcolor": "LED Indicator: Color"

sequence:
  - variables:
      area: '{{ area|default("invalid")|lower }}'
      device: '{{ device|default("invalid")|lower }}'
      area_dimmer: >
        {% set entities = namespace(areas=[]) %}
        {% for dev in area %}
          {% for ent in area_entities(dev) %}
            {% if is_device_attr(device_id(ent), 'model' , 'LZW31-SN' ) and ent.split('.')[0] == 'light' %}
              {% set entities.areas = entities.areas + [ent | string] %}
            {% endif %}
          {% endfor %}
        {% endfor %}
        {{ entities.areas|lower }}
      device_dimmer: >
        {% set entities = namespace(devices=[]) %}
        {% for dev in device %}
          {% for ent in device_entities(dev) %}
            {% if device_attr(device_id(ent),'model') == 'LZW31-SN' and ent.split('.')[0] == 'light' %}
              {% set entities.devices = entities.devices + [ ent | string] %}
            {% endif %}
          {% endfor %}
        {% endfor %}
        {{ entities.devices|lower }}
      LEDcolor: '{{ LEDcolor|default("no change")|lower }}'

  - repeat:
      for_each:
        - device_type: dimmer
          entities: '{{ area_dimmer + device_dimmer }}'
      sequence:
        - choose:
          - conditions: >
              {{ LEDcolor != "no change" }}
            sequence:
              - service: zwave_js.set_config_parameter
                data:
                  entity_id: '{{ repeat.item.entities }}'
                  parameter: |
                    {% set effect_param = repeat.item.device_type + '_ledcolor' %}
                    {{ parameters[effect_param] }}
                  value: |
                    {{ color_set[LEDcolor] }}
          default:
 

Defining area_dimmer in the variable section above sequence seems to have solved the issue. I don’t understand why though.