Unexpected template result

I was trying to use expand to expand some groups and other entities into a list to run through some filters. Here’s my template code:

{{['group.external_door_contacts','group.window_contacts','binary_sensor.office_door_sensor_contact']|expand}}

The output contains all the sensors, but I am not sure it has the right structure. I expected expand to return a list of state objects, but it appears to be returning some kind of template or something:

[<template state binary_sensor.front_door_sensor_contact=off; friendly_name=Front Door Sensor Contact, device_class=door @ 2020-08-23T08:45:47.188195-07:00>, <template state binary_sensor.office_door_sensor_contact=off; friendly_name=Office Door Sensor Contact, device_class=door @ 2020-08-23T08:45:47.169944-07:00>, <template state binary_sensor.primary_garage_door_sensor_contact=off; friendly_name=Primary Garage Door Sensor Conact, device_class=opening @ 2020-08-23T10:34:40.022195-07:00>, <template state binary_sensor.secondary_garage_door_sensor_contact=off; friendly_name=Secondary Garage Door Sensor Contact, device_class=opening @ 2020-08-23T08:45:47.195324-07:00>]

When I try to select the state value, name or other attributes using the attr filter I always end up with an empty list:

{{['group.external_door_contacts','group.window_contacts','binary_sensor.office_door_sensor_contact']|expand|attr('name')}}

I’m sure I’m doing something really clowny somewhere, but the examples in the wiki (https://www.home-assistant.io/docs/configuration/templating#states) don’t seem to be working as expected and all the experimenting I did didn’t yield the intended results.

I messed around with this a little more. The <template /> items really throw me off. My understanding is I should at least get a list of entity_ids of the entities contained in the groups. But I get this weird template construct. Is this a bug? Am I the only one with this problem?

Expand does the following

Pretty sure I do not get a list of state objects returned by expand. See the output I have shared above. Am I doing something wrong?

if you want to select an attribute from the state objects, you need to map it

{{ ['group.external_door_contacts','group.window_contacts','binary_sensor.office_door_sensor_contact']|expand|map(attribute='name') }}

the attr filter does not work on a list of objects, only a single object. Expand returns a list of state objects.

1 Like

Actually that is exactly what you are getting returned, state, friendly_name, device_class, and last activated for all entities in the group.

He’s getting state objects back. What you see in the printout is a string representation of each object.

Derp. Thank you! I was under the impression that attr filters each element in the list, but I stand corrected. Using map resolved my problem. Appreciate the help!

1 Like