Automation Action device_id Template

I’m trying to use a template to list all the “on” devices within an area as devices under device_id for an automation action. What is the proper syntax to do this?

Most of the things I try end up outputting:

  domain: light
  service: turn_on
  service_data:
    kelvin: 1725
    brightness_pct: 100
    device_id:
      - |-
        - 91444cc30342e1f0ffb0cd25225743e0
        - a7557daf79f3a038e6f0b04d283e01b6
        - c72097e29dd6759de0dc2e6f3925b51a
  target:
    device_id:
      - |-
        - 91444cc30342e1f0ffb0cd25225743e0
        - a7557daf79f3a038e6f0b04d283e01b6
        - c72097e29dd6759de0dc2e6f3925b51a
running_script: false

which doesn’t seem to execute the action.

Hi Bob,

Device_id actions do not accept templates.

Use entity_id’s.

Why and how to avoid device_ids in automations and scripts,

I’m getting a “Error: Template rendered invalid entity IDs: - light.living_room_light_1_light - light.living_room_light_2_light_2 - light.living_room_light_3_light_3” for entity_id:

target:
  entity_id: >
    {% set kitchen_lights = area_entities('living_room_lights') |
    select('is_state', 'on') | reject("search", 'power_on') | reject("search",
    'lights') | list%} {% for light in kitchen_lights %} - {{light}} {%- endfor
    -%}

An actual list of entity ID’s is expected (not a list-shaped string) and there’s no need for the loop:

action: light.turn_on
data:
  kelvin: 1725
  brightness_pct: 100
target:
  entity_id: >
    {{ area_entities('living_room_lights') 
    | select('is_state', 'on') | reject('search', 'power_on') 
    | reject('search', 'lights') | list }}

FWIW, it is usually better to reserve use of Areas for actual areas like rooms or small groups of rooms. A Label would be more appropriate for a sub-grouping like “living_room_lights”. There is an analogous function label_entities() which you would use instead of area_entities().

I tried without the loop as well and it didn’t like it. The automation doesn’t really tell you why it doesn’t work so I’m just guessing. What does proper syntax look like in this case?

Each run of an automation should produce a debug Trace.

What I posted above should be a functional action.

I see now, I had double quotes around ‘search’ when I tried because that’s what the example I found had:

{{ area_entities('living_room_lights') | select('is_state', 'on') | reject("search", 'power_on') | reject("search", 'lights') | list}}

Apparently the automation didn’t like that despite getting the exact same result as yours when put in the template editor.

Also, the debug trace was where I was getting the error. All it was telling me was: Error: Template rendered invalid entity IDs: - light.living_room_light_1_light - light.living_room_light_2_light_2 - light.living_room_light_3_light_3

I’m going to leave this open for right now, but thanks for the help. I think it’s doing what I want.

Because of the way you used the loop, what the automation was receiving was a single string:

"- light.living_room_light_1_light - light.living_room_light_2_light_2 - light.living_room_light_3_light_3"

It may have looked like a list in the Template editor, but it was just “list-shaped”, not a true list.