How do I read entities of a scene?

Hi,
within a blueprint I would like to retrieve all entities from a scene. How can I do that? I tried already:

variables:
  scene_to_activate: !input scene_to_activate
  scene_entities: "{{ state_attr(scene_to_activate, 'entity_id') }}"

But it seems not to work.

This should be in #configuration:blueprints.

Thanks. I moved the topic.

I don’t know how you tested it to arrive at the conclusion that it doesn’t work. Perhaps you assumed scene_entities contains a string but, in fact, it will be a list.

If you try the following blueprint, you’ll see that it does work. The list is converted to a comma-separated string and displayed as a persistent_notification.

Blueprint
blueprint:
  name: Test C
  description: Test C
  domain: automation
  input:
    the_scene:
      name: Scene
      description: Select a scene.
      selector:
        entity:
          domain: scene
      default: ''
    the_boolean:
      name: Boolean
      description: Select an input_boolean.
      selector:
        entity:
          domain: input_boolean
      default: ''
variables:
  s1: !input the_scene
  e: "{{ state_attr(s1, 'entity_id') }}"
trigger:
- platform: state
  entity_id: !input the_boolean
action:
- service: persistent_notification.create
  data:
    title: "{{ now().timestamp()|timestamp_local() }}"
    message: "{{ e | join(', ') }}"

Screenshot from 2020-12-21 11-02-16

1 Like

Thank you very much. I was trying to get the entitites of a scene to turn them off. I got an error, that in my case {{scene_entities}} is not valid target when calling homeassistant.turn_off service.

That is because this is a list and you cant call on a list.
But you can loop over the list, something like this:

{{ state_attr('scene.test_scene', 'entity_id') }}

{% for entity_ids in state_attr('scene.test_scene', 'entity_id') -%}
  {{ entity_ids }} 
  {{- '\n' -}} 
{%- endfor %}.

results in:

['light.hallway_below_1_light', 'light.hallway_below3', 'light.hallway_below2']

light.hallway_below_1_light
light.hallway_below3
light.hallway_below2

the {{- ‘\n’ -}} is only for a new line and not needed.

Hi, thanks for that. It helped me a lot.

You can’t ‘call on a list’? Well that’s just wrong. The blueprint I posted does in fact ‘call on a list’.

As for ‘looping over the list’ with a for-loop, that’s fine in the Template Editor but nowhere else. I invite you try it in a Template Sensor or automation to discover for yourself why the result is not usable when generated that way.

The blueprint I posted uses the join filter to convert the list of entities into a comma-separated string of entities.

you shouldn’t need to even do that. The entity_id field accepts a fully formatted list (thanks to the new typing). So joining or not should work.

True, not if used with entity_id. My example was using it in a persistent_notification message so it had to be converted from list to string.

1 Like