Reading out the Device_ID or the entity associated with the Device_ID

Hello community,

for the people, like me, who always have problems when it comes to the device ID or the associated entities in automations or scripts, I have written two template codes here.

Simply insert one or both codes under the developer tools → template and replace “Insert your device_id here” or “Insert your entity here” in the code with your ID or entity.

Code to copy

Infer from “Device ID” to “Entities” to the Device ID:


{% set such_device_id = 'Insert your device_id here' -%}
Entities for Device ID: {{ such_device_id }}
{% for entity in states if device_id(entity.entity_id) == such_device_id %}
- {{ entity.entity_id }} ({{ entity.name }})
{% endfor %}



Infer from “Entity” to “Device ID”:

{% set ent = 'Insert your entity here' %}
Device ID for {{ ent }}: {{ device_id(ent) if device_id(ent) else 'nothing found' }}

Example code

Infer from “Device ID” to “Entities” to the Device ID:


{% set such_device_id = '92e0ff68a331c82a758ed355309xcf5h' -%}
Entities for Device ID: {{ such_device_id }}
{% for entity in states if device_id(entity.entity_id) == such_device_id %}
- {{ entity.entity_id }} ({{ entity.name }})
{% endfor %}



Infer from “Entity” to “Device ID”:

{% set ent = 'climate.wohnzimmer' %}
Device ID for {{ ent }}: {{ device_id(ent) if device_id(ent) else 'nothing found' }}

Output of the example code

Infer from “Device ID” to “Entities” to the Device ID:


Entities for Device ID: 92e0ff68a331c82a758ed355309xcf5h

- sensor.wohnzimmer_room_temperature (Wohnzimmer Raumtemperatur)

- climate.wohnzimmer (Wohnzimmer)

- device_tracker.klimawohnzimmer (klimawohnzimmer)

- switch.klimawohnzimmer_internet_access (klimawohnzimmer Internet Access)

- sensor.wohnzimmer_daily_energy_consumed (Täglich verbrauchte Energie)




Infer from “Entity” to “Device ID”:


Device ID for climate.wohnzimmer: 92e0ff68a331c82a758ed355309xcf5h

Hello Chrissi28,

Thank you for this contribution, it looks useful.
Consider creating a custom Template macro with your code, and then you could share it to a wider audience via the HACS loader process…