The docs say
Each of the label template functions can also be used as a filter
but I’ll be jiggered if can work out how and the docs don’t seem to say. I’m trying to build a template that will output the list of all the sensors that have stale values (usually because something has fallen of my zigbee network) and also have a specific label (because I don’t want to monitor everything, just some things I care about.)
So far, I have this, which can output all the stale sensors without the label filter (but has many things I do not want)
{{ states.sensor
| selectattr('last_changed', 'lt', now()-timedelta(seconds=3600))
| map(attribute='entity_id')
| list }}
I feel like there must be some syntax where I can do something like | select(label("monitored"))
in here, but I can’t figure it out.
It may also be possible to go the other way, start with a list of entity IDs and then filter down to those in the sensor
domain:
{{ label_entities("monitored")
| list
}}
…but I am not sure how to do that either. I don’t know how to apply a filter to a list of entities that will only select ones in a certain domain.
(I know there’s other ways to solve the problem too, like I could have a special label just for sensors I want to monitor, or I could put them in a group or something. But I want to use this to learn about templating.)
EDIT TO ADD SOLUTIONS, FULL DETAILS BELOW
This works, but is inefficient, because it’s iterating over ever sensor you have:
{{ states.sensor
| selectattr('last_changed', 'lt', now()-timedelta(seconds=3600))
| map(attribute='entity_id')
| select('in', label_entities("monitored"))
| list }}
This is the better solution:
{{
label_entities('monitored')
| select('match', 'sensor')
| expand()
| selectattr('last_changed', 'lt', now()-timedelta(seconds=3600))
| map(attribute='entity_id')
| list
}}