Get list of friendly names from list of entity_ids

I have the following template:-

{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none']) | map(attribute='entity_id') | select('search','lqi*') | list | join(', ') }}

It produces a list of all entities where their state is either unavailable, unknown or none, where the entity id contains the text ‘lqi’. On my system it produces this output:-

sensor.tz3210_rcggc0ys_ts0505b_lqi_3, sensor.lumi_lumi_sensor_magnet_aq2_lqi_9

How would I extend this template to convert these entity_ids into their device friendly name ?

I’d like to then use this in an automation to notify me with a list of those friendly names.

If I take out the map(attribute=‘entity_id’) the data returned is this:-

<template TemplateState(<state sensor.tz3210_rcggc0ys_ts0505b_lqi_3=unavailable; state_class=measurement, friendly_name=Moes RGB Bulb - Ground Floor Hall LQI @ 2024-08-22T11:40:37.928669+01:00>)>, <template TemplateState(<state sensor.lumi_lumi_sensor_magnet_aq2_lqi_9=unavailable; state_class=measurement, friendly_name=Aqara Contact - Doorbell LQI @ 2024-08-22T10:15:13.225836+01:00>)>

So the friendly name is in there, I just don’t know how to parse it out.

1 Like

Use selectattr for your search, then map to the name.

{{ states | selectattr('state', 'in', ['unavailable', 'unknown', 'none'])
| selectattr('entity_id', 'search', 'lqi*') 
| map(attribute='name') | list | join(', ') }}

If all of your “lqi” entities are in the sensor domain, it would be better to use states.sensor as your starting point rather than states.

Thanks for the heads up.
This now lets me hopefully remove the dependency on ZHA Toolkit to alert me when ZHA devices drop off the network using a pretty trivial automation.
For this to work, you need to expose the LQI entity thats usually hidden for zigbee devices, but I do this routinely anyway for a dashboard page I have:-

alias: ZHA Devices Offline
description: ""
trigger:
  - platform: time_pattern
    hours: /1
condition:
  - condition: template
    value_template: >-
      {{ states.sensor | selectattr('state', 'in', ['unavailable', 'unknown',
      'none'])
      | selectattr('entity_id', 'search', 'lqi') 
      | list | count >0 }}
action:
  - action: notify.mobile_app_someones_phone_here
    data:
      title: ZHA Devices Offline
      message: >-
        "Offline Devices:- " % {{ states.sensor | selectattr('state', 'in',
        ['unavailable', 'unknown', 'none']) | selectattr('entity_id', 'search',
        'lqi')  | map(attribute='name') | list | join(', ') }}
mode: single

I decided to use a simple time based trigger rather than a template trigger as I don’t need to instantly know when it happens, and I don’t want to be spammed if they drop in and out.