Device_id: entity_id: where to find

Hya,

where do i find device_id, entity_id for a given device or entity?

Thx in advance.

2 Likes

under configuration, either devices or entities

image

I checked and it’s not there to be found.

1 Like

I think it’s in the URL.

28 Likes

The only way you can find this value is by looking in . storage/core. device_registry (or creating a Device Automation via the UI and examining the YAML it generates)

4 Likes

Many thanks! Helpful.

how to do this?

1 Like

URL works for me! Thank you :slight_smile:

2 Likes

Goto device page, in Automation div click plus button, choose “Do something when this device turns on or off” (or something like this). This will follow you to creating automation page with trigger on your device. Click three dots, choose “Edit in YAML” - you will see device_id and entity_id of this device.

9 Likes

The way you seem to find it is by clicking ‘MQQT INFO’

Seems to me, that the value in MQTT INFO is the IEEE address of the device.

From Terminal tool in home assistant browse to the folder using following command
cd /config/.storage
cat core.device_registry
screenshot below

1 Like

Just right-click on the integration’s three dots on the integrations page and choose download diagnostics. The id is part of the filename.

I spent way too much time on this and ended up writing a quick Python helper script for this:

import json
import os

def list_sensors():
    # Path to the core.entity_registry file
    registry_path = '.storage/core.entity_registry'

    try:
        # Read the JSON file
        with open(registry_path, 'r') as f:
            data = json.load(f)

        # Get all entities
        entities = data.get('data', {}).get('entities', [])

        # Print ID mappings for occupancy/motion sensors
        print("Motion/Occupancy Sensor Mappings:")
        print("-" * 50)
        for entity in entities:
            entity_id = entity.get('entity_id', '')
            if ('occupancy' in entity_id.lower() or 'motion' in entity_id.lower()) and 'binary_sensor' in entity_id:
                internal_id = entity.get('id')
                device_id = entity.get('device_id', 'N/A')
                print(f"Internal ID: {internal_id}")
                print(f"Entity ID: {entity_id}")
                print(f"Device ID: {device_id}")
                print("-" * 50)

    except FileNotFoundError:
        print(f"Error: Could not find file at {registry_path}")
    except json.JSONDecodeError:
        print("Error: Invalid JSON format in the registry file")
    except Exception as e:
        print(f"An error occurred: {str(e)}")

if __name__ == "__main__":
    list_sensors()

This will return something like this:

Motion/Occupancy Sensor Mappings:
--------------------------------------------------
Internal ID: [redacted]
Entity ID: binary_sensor.[redacted]
Device ID: [redacted]
--------------------------------------------------
[...]

If you’re interested, the following template duplicates most of what is produced by your python script. It reports the device_id and entity_id of every binary_sensor whose object_id contains either the string ‘motion’ or ‘occupancy’.

{% for x in states.binary_sensor | selectattr('object_id', 'search', '(motion|occupancy)') -%}
{{ device_id(x.entity_id) }}, {{ x.entity_id }}
{% endfor -%}

What it doesn’t report is the id field (as in your python script) because (to my knowledge) it’s not exposed to templating (inconsequential; it’s only used internally for cross-referencing items in the config files).

1 Like