Show Friendly Name in Lovelace Card

I’ve created a template sensor to list entities in a group with a state of 'on' but in a Lovelace markdown card it shows the entity_id instead of the friendly name.

Can someone please advise what I need to change or add to get the friendly name to show in the card?

Template Sensor

- platform: template
  sensors:
    list_doors_open:
      friendly_name: "List Doors Open"
      value_template: "{{ state_attr('binary_sensor.doors', 'entity_id') | selectattr('is_state', 'on') | list }}"

Markdown Card Code

   content: '{{ states(''sensor.list_doors_open'')}}'
   view_layout:  
      grid-area: warning```

Markdown Card View
Screenshot 2023-05-09 at 11.53.52

friendly_name: "List Doors Open"
      value_template: "{{ state_attr('binary_sensor.doors', 'entity_id') | selectattr('is_state', 'on') | map('attribute', 'eq', 'name') | list }}"

Thanks Tom, but the output from that is ‘unavailable’Screenshot 2023-05-09 at 13.41.03

*edited to add screenshot

Try | map(attribute='attributes.friendly_name') | list

No, still ‘unavailable’.

Not sure if any of these factors make a difference but the devices are zigbee binary sensors and I created a binary sensor group via ‘Helper’.

Do the binary sensors actually have friendly names?

Check Developer Tools-> States.

Yes, they all have friendly names, for example:

And when I try {{ states('sensor.list_doors_open')}} in Developer Tools > Templates it also returns ‘unavailable’

Bump this one because I’m scratching my head at how to resolve it and I’m desperate to get it sorted to finish off my dashboard

Try this:

value_template: "{{ expand(state_attr('binary_sensor.doors', 'entity_id')) | selectattr('is_state', 'on') | map('attribute', 'eq', 'friendly_name') | list }}"

Thanks Tom, but that again still returns ‘unavailable’

Try

value_template: "{{ expand(state_attr('binary_sensor.doors', 'entity_id')) | selectattr('state', 'eq', 'on')  | map('attribute', 'eq', 'friendly_name') | list }}"

Sorry Tom, but that’s also returning ‘unavailable’.

I’ve even tried creating the group the “old way” in `groups.yaml’ and the suggested codes still show ‘unavailable’. I’ve also cleared the cache and restarted HA each time.

To try and isolate the issue, I have created a new group of different binary sensors, and used the same template sensor code for that group and it still shows ‘unavailable’ in the dashboard and Developer Tools > Templates.

I then created a new dashboard and tried adding both a template card and markdown card but still getting ‘unavailable’.

I’m assuming the issue is one of three things:

  • A bug in the latest HA release (unless it’s only happening to me)
  • An issue with my template sensor code
  • An issue with my code in the dashboard card

If someone could please help me solve this I would be grateful as it’s making me age quickly.

I’ve opened an issue case for this because it is not the coding that’s the issue, I’m guessing.

Issue Case

It’s select('is_state', 'on')

Or:


{{ expand('binary_sensor.doors')
  |selectattr('state', 'eq', 'on') |map(attribute='name') |list }}

2 Likes

Brilliant, that worked. Thanks so much.

I’m sorry I have the same problem (but with light entities) but I do not understand where the (template sensor) code lives (is stored).

Where do I add (and edit for my needs):
value_template: “{{ expand(‘binary_sensor.doors’)|selectattr(‘state’, ‘eq’, ‘on’) |map(attribute=‘name’) |list }}”

in any of the files? It does not seem to be the Card itself or confugaration.yaml

Also I do not need the selected on state=‘on’, just the friendly name, what can I leave out?

It may have changed, but I believe it was config/.storage/core.config_entries

IMO, you shouldn’t edit the templates at location the code is stored. My understanding is you can render your entire config invalid if the code is incorrect.

You can place template sensors directly in the configuration file or a referenced YAML file.

Here is an example you can test in your config file to show the number of active lights.

sensor:
  - platform: template
    sensors:
     all_lights_on:
       friendly_name: 'All Lights ON'
       value_template: >
          {{ states.light | rejectattr('attributes.entity_id', 'defined') | selectattr('state', 'eq', 'on') | list | count }}