Getting unique_id in template editor

Is it possible to get the unique_id of an entity in the template editor?

I’m able to iterate over the entities of a specific device with

{% set device_name = 'test_dev' %}
{% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq',None) | list %}
{% for device in devices if device_attr(device, 'name') is not none and device_attr(device, 'name') in device_name %}
    {% set entities = device_entities(device) | list | sort %}
        {% for entity in entities %}
            {#{ print unique_id of entity }#}
        {% endfor %}
{% endfor %}

but I have no idea how to get the unique_ids, which I would need.

You created a list of strings that contains the entity ids. So …

{{ entity }}

This prints the object_id or the device name, followed by an underscore, followed by the entity name. I would like to have the unique_id instead.

First … why do you think you ‘need’ the unique_id of an entity.

Second … my testing of the above does not print object_id or device name, followed by an underscore, followed by the entity name. I only get the entity_id. ie: cover.big_garage_door or light.ratgdov25i_eeef35_light.

I’m working on GitHub - pikim/tomato-mqtt. These are a couple of scripts on my router that send data to Mosquitto. They support device discovery and I want to send the discovery message only if an entity wasn’t registered, yet. I’m using a twig template to query the existing entities of the according router device and also some state and meta information. For this I’d like to get the unique_id as the entity name and object_id can be changed by the user. So it seems difficult to use those.

EDIT:
According to core/homeassistant/helpers/template.py at master · home-assistant/core · GitHub it seems impossible to get the unique_id. So unfortunately I have to think about another solution.

Hi MK

Just posted this, the same applies to you…

1 Like

When I saw that it’s not possible to get the unique_id in templates I opened a related topic here https://community.home-assistant.io/t/how-to-identify-mqtt-entity-when-object-id-has-changed and decided to add an own custom uid to my entities. In the other thread I also explained why I need to get the unique_id. In the meantime it’s working as desired.