Get multiple mapping values in templating

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?

1 Like

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.

Group is defined as:

ha_monitor2:
    name: Home Assistant Monitor
    icon: mdi:radar
    entities:
      - update.home_assistant_operating_system_update
      - update.home_assistant_core_update
      - update.home_assistant_supervisor_update

I’m then trying to do this:

{% set updates =  expand('group.ha_monitor2') | selectattr('state', 'eq', 'off') | list %}
{% set ns = namespace(result=[]) %}
{% for item in updates %}
  {% set ns.result = ns.result + [ [item.title], [item.latest_version] ] %}
{% endfor %}
{{ ns.result }}

[note: I know normally the select would be for ‘on’ items but I don’t have any updates due so for testing purposes using ‘off’]

The response I get back to this is:

[[Undefined, Undefined], [Undefined, Undefined], [Undefined, Undefined]]

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. :slight_smile:

Doh!

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!

  {% set ns.result = ns.result + [ [item.attributes.title], [item.attributes.latest_version] ] %}