I used a similar solution to you @Ih8rain2 although my use case was a little different. In my case I wanted a list of the top 5 energy consumers in my house which displayed the friendly name of the entity. My solution works as follows:
I have a global macro defined in config/custom_templates/top_consumers.jinja
{% macro top_power_consumers(index) %}
{% set entity_list = [
{'entity_id': 'sensor.power_1', 'consumption': states('sensor.power_1') | default(0) | float },
{'entity_id': 'sensor.power_2', 'consumption': states('sensor.power_2') | default(0) | float },
{'entity_id': 'sensor.power_3', 'consumption': states('sensor.power_3') | default(0) | float },
{'entity_id': 'sensor.power_4', 'consumption': states('sensor.power_4') | default(0) | float },
{'entity_id': 'sensor.power_5', 'consumption': states('sensor.power_5') | default(0) | float },
{'entity_id': 'sensor.power_6', 'consumption': states('sensor.power_6') | default(0) | float },
{'entity_id': 'sensor.power_7', 'consumption': states('sensor.power_7') | default(0) | float },
{'entity_id': 'sensor.power_8', 'consumption': states('sensor.power_8') | default(0) | float },
{'entity_id': 'sensor.power_9', 'consumption': states('sensor.power_9') | default(0) | float }] %}
{% set sorted_list=entity_list | sort(attribute='consumption',reverse=true) | map(attribute='entity_id') | list %}
{{ sorted_list[index] | trim }}
{% endmacro %}
Then I have 5 template sensors defined as follows which just index through the sorted_list (this is just the first sensor):
- sensor:
- name: >
{% from 'top_consumers.jinja' import top_power_consumers %}
{% set name = state_attr(top_power_consumers(0) | trim, 'friendly_name') %}
{{ name }}
state: >
{% from 'top_consumers.jinja' import top_power_consumers %}
{% set state = states(top_power_consumers(0) | trim) %}
{{ state }}
unique_id: sensor.top_power_consumer_first
unit_of_measurement: W
state_class: measurement
Then I have an entites card in my custom energy dashboard which list each of the sensors:
type: entities
entities:
- entity: sensor.top_power_consumer_first
- entity: sensor.top_power_consumer_second
- entity: sensor.top_power_consumer_third
- entity: sensor.top_power_consumer_fourth
- entity: sensor.top_power_consumer_fifth
I do the same for energy, which seems to work as expected, and the names and state values in the dashboard updated dynamically as devices switch on and off.
I had a couple of issues during implementing. Firstly, I had was the some leading/trailing whitespace in the return value of the macro. Unfortunately the trim at the end of the macro did not work, and I have to use trim in all the places where the macro is called. Perhaps the whitespace is also the cause of your problem and you need to call trim?
Secondly, there is some bug when the template sensors get created where they did not receive the unique_id I defined, and they ended up with some unique_id which was derived from the name. It was enough to find the sensor in the settings->entities list and manually modify the unique_id via the GUI.