Templating, retrieve entity names of which holds state == x

Hi,

Been trying for a while now, to make a value_template or even an automation, that will list all entities in a group, holding a state that equals “1”.
The purpose of this is to be notified of which garbage container that is giong to be picked up the next day.
I have already converted the sensors for each garbage container, which originaly holds a timestamp, to show remaining days to pickup.

What I would like to achieve:
List all entities whithin the “group.garbage” that is state == 1 ( 1 remaining days untill pickup ).
Then split the entities’ names, from “sensor.days_untill_pickup_xxx” to a Friendly_name etc “food” “paper”, then use does friendly names in a notify string message.

E.g: “Tomorrow the “xxxx” and “xxxx” containers are picked up”

Does anyone have a smart solution to achieve this?

best regards
Christian

Try this in the template editor:

"{{ expand('group.garbage') | selectattr('state', 'eq', '1')| map(attribute='name') | list | join(', ') }}"

Maybe friendly_name instead of name - it depends on your entities.

name will return the name on the object (this could be the initial setup name, but shoudl be the friendly name in most cases). If he wants to ensure the friendly name it would have to be attributes.friendly_name.

1 Like

Hi guys. Thank you so much for chiming in!
I’ve already tried the expand filter, but wasn’t able to make it across the finishline.
My group hasn’t got that many attributes. Look at attached screendump:

The template snippet you provided above, doesn’t output anything unfortunately. This does though:

{{ 
  expand(states.group) 
  |selectattr('state', 'eq', '1') 
  |map(attribute='entity_id')
  |list
}}

results:

Result type: list
[
  "sensor.dager_til_matavfall",
  "sensor.dager_til_plastavfall"
]

So this template will output the sensors that equals to 1 days. Then I would need to trim/split the sensor names, to only show what type of waste,/garbage (matavfall, plastavfall etc)
Then I could use those names in a new notification message.

Try this:

{{ 
  expand(states.group) 
  |selectattr('state', 'eq', '1') 
  |map(attribute='entity_id')
  |list
  |replace('sensor.dager_til_', '')
  |title
}}
1 Like

WOW! This solves it! :slight_smile: thank you very much :smiley:

Is there any way to do this with dynamic entity names? For example persistent notifications add a number on the end of each stacked notification.
How would I generate a list of all the current " persistent_notification.* " entities and then filter them by a particular state?