Creating a list using a variable passed from an automation

Hello community!

I have an automation that calls a script to produce a light notification in the room specified by a variable that is being passed (the area) Based upon this variable (room_focus) there forms a list of entities that are being snapshot into a scene before actual light notification is being performed (flashing). After that the initial states of the lights are being restored by activating the previously created scene.

I pass the variable (room_focus) and it seems to go well. I’ve put a test notification that runs smoothly. However, after that the script stops giving the following error in the trace:

My automation and script codes are as follows:

alias: TEST
description: ""
trigger: []
condition: []
action:
  - service: script.turn_on
    target:
      entity_id: script.1662922347981
    data:
      variables:
        room_focus: salon
mode: single
alias: Light Notification
sequence:
  - service: notify.service_ng
    data:
      message: "{{room_focus}}"
  - service: scene.create
    data:
      scene_id: initial_scene
      snapshot_entities: |-
        {{ (states.light 
           | selectattr('domain','eq','light')
           | selectattr('entity_id', 'in', area_entities('{{room_focus}}'))
           | rejectattr("entity_id", "search", "dnd")
           | rejectattr('attributes.entity_id', 'defined')
           | map(attribute='entity_id')
           |list)
           |join(', \n') }}
  - service: light.turn_on
    data:
      brightness_step_pct: -50
    target:
      entity_id: light.dim_salon
  - repeat:
      count: "3"
      sequence:
      #
      #   OMITED NOT TO OCUPY TOO MUCH SPACE HERE
      #
  - service: scene.turn_on
    data:
      transition: 3
    target:
      entity_id: scene.initial_scene
mode: single
icon: mdi:washing-machine

Also, if I put “salon” instead of the variable here | selectattr(‘entity_id’, ‘in’, area_entities(’{{room_focus}}’)) it works fine.

Could someone give me a hint why forming the list of entities is producing an error.

Like this:

| selectattr('entity_id', 'in', area_entities(room_focus))

If you’re interested, you can reduce the template to this:

        {{ expand(area_entities(room_focus) | select('match', 'light') | reject('search', 'dnd')) 
           | rejectattr('attributes.entity_id', 'defined')
           | map(attribute='entity_id')
           | list }}

snapshot_entities accepts a list value so there’s no need for the template to use join to produce a (comma/newline separated) string.

1 Like

Thanks a lot, Taras! Works just fine and special thanks for the optimisation advice!

1 Like

Taras, I have another question related to this topic.

If I want to choose entities from more than one area, how do I set the variable room_focus in this case. Or if not what could be the workaround to achieve that?

Assign a list of rooms to the variable room_focus. It should be a list even if you only want a single room.

Examples of the value of room_focus:
['kitchen', 'dining']
['garage']
['patio', 'pool', 'garage']

If you do that then you can use the following template to select the desired entities from all of the specified rooms in the list.

{% set ns = namespace(entities=[]) %}
{% for room in room_focus %}
  {% set ns.entities = ns.entities + 
    expand(area_entities(room) | select('match', 'light') | reject('search', 'dnd')) 
    | map(attribute='entity_id') | list %}
{% endfor %}
{{ ns.entities }}
3 Likes

Thanks a lot for help, Taras!

1 Like