Error "Template rendered invalid entity IDs" in automation with mutlitple action targets

Hi everybody,

I’d like to address multiple targets in an action using a template and I wonder how this can be achieved.

Context: My shutters behave like light: They can be switched on and off and setting the brightness they can be shut and opened.
I wanted to open or close the shutters in two steps:

  1. switching the shutters on, that I want to be opened or closed
  2. open or close the shutters with state “on” using a slider (1: close, 2: no movement, 3:open)

My dashboard looks like this:


In the first column I have put some buttons to switch certain groups of shutters on and off.

My action script looks like this:

- id: "1732403047762"
  alias: Rolläden steuern
  triggers:
    - trigger: state
      entity_id: input_number.rolladensteuerung
  action:
    - action: light.turn_on
      target:
        entity_id: >
          {% set entities = ( states.light | selectattr('entity_id', 'search', '^light.rolladen') | selectattr('state', 'equalto', 'on') | rejectattr('entity_id', 'equalto', 'light.rolladen_wohnzimmer') | map(attribute='entity_id') | list | join('\n') ) %}
          {{ entities }}
      data:
        brightness_pct: >
          {% set brightness = int(states('input_number.rolladensteuerung')) * 30 %}
          {{ brightness }}

If I replace the entity-template with a certain entity id everything works fine. But using the shown template, I get this error:

Error while executing automation automation.rolladen_steuern: Template rendered invalid entity IDs: light.rolladen_1_wz light.rolladen_3_wz light.rolladen_5_wz light.rolladen_4_wz light.rolladen_2_wz

The ids are ok.
I wonder if the indentation is wrong or if the entity ids need to be set in quotation marks? Do they have to be preceded by a hyphen?

If I use the template editor the entity list looks like this (multiline string):

light.rolladen_1_wz
light.rolladen_3_wz
light.rolladen_5_wz
light.rolladen_4_wz
light.rolladen_2_wz

or like this (if I omit the “join”-statement):

[
  "light.rolladen_1_wz",
  "light.rolladen_3_wz",
  "light.rolladen_5_wz",
  "light.rolladen_4_wz",
  "light.rolladen_2_wz"
]

Either way it’s not working.

Any ideas, how to solve this issue?
Thanks for helping!

blackmail24

I already solved the problem myself.
I found a hint in another post to use variables in the script, so a trace shows what a template resolves to.

automation.yaml

automation:
- id: "1732403047762"
  alias: Rolläden steuern
  variables:
    entities: >
      {{ states.light | selectattr('entity_id', 'search', '^light.rolladen') | selectattr('state', 'equalto', 'on') | rejectattr('entity_id', 'equalto', 'light.rolladen_wohnzimmer') | map(attribute='entity_id') | list }}
  triggers:
    - trigger: state
      entity_id: input_number.rolladensteuerung
  action:
    - action: light.turn_on
      target:
        entity_id: "{{ entities }}"
      data:
        brightness_pct: >
          {% set brightness = int(states('input_number.rolladensteuerung')) * 30 %}
          {{ brightness }}

Trace output:

Ausgeführt: 25. November 2024 um 22:18:20
Ergebnis:

params:
  domain: light
  service: turn_on
  service_data:
    brightness_pct: 60
    entity_id:
      - light.rolladen_1_wz
      - light.rolladen_2_wz
  target:
    entity_id:
      - light.rolladen_1_wz
      - light.rolladen_2_wz