I hae a template sensor where I try to output a list of all items matching a filter. That works great so far. But I now try to get entity_id or friendly name and state value.
Here is what I have now
{{ states.sensor|selectattr('attributes.unit_of_measurement','eq','W') |rejectattr('attributes.meter_type','in',[1]) |rejectattr('attributes.use_power','in',['false']) | map(attribute='entity_id') | list }}
How could I output the state together with the entity_id?
As far as I know, you can’t use map to extract more than one attribute.
I believe you will need to use a for-loop. It provides the flexibility to generate output in whatever form you want (in this case, a list of lists) containing whatever you want.
{% set x = states.sensor
| selectattr('attributes.unit_of_measurement','eq','W')
| rejectattr('attributes.meter_type','in',[1])
| rejectattr('attributes.use_power','in',['false'])
| list %}
{% set ns = namespace(result=[]) %}
{% for s in x %}
{% set ns.result = ns.result + [ [s.entity_id, s.state] ] %}
{% endfor %}
{{ ns.result }}
Here’s an example of it displaying some temperature sensors.
Jumping on this thread if that’s okay with a similar challenge! Following the availability of the update entity I was refactoring my various notifications.
My scenario is I have a group which wraps up various elements (HomeAssistant, Add On’s etc) and when an update is available notifies me. I’d previously done this by using a group and my notification would grab data out of the extended group. I can’t however seem to make this work now against updates.
If I were just to do {{updates}} then I get the fully expanded group so I know that’s okay. Wondering if either I’ve got something wrong or I just can’t do that with the expand - any insight would be massively appreciated before i commence on a rebuild on my notifications based around the update entity.
Figured it out having seen some other posts which prompted me in the correct direction, I wasn’t looking at the attributes, minor revision to include these unlocks my problem. Posting for info should anyone else make the same error as me!