Devices via REST API

Is there a way to query devices via the REST API? I can’t see anything here: REST API | Home Assistant Developer Docs

I would like to determine which entities are part of the same device, which doesn’t seem possible just by looking at the information in /api/states. Is there another way?

Thanks.

Easiest way is probably just to use POST /api/template. You can get to device info in templates so just ask HA to resolve a template that outputs the info you need.

Thanks. I can get all associated entity_ids with:

{{device_entities(device_id('sensor.my_sensor'))}}

Do you happen to know if there’s a way to get a list of all device_ids or iterate through them?

This template should create a list of all entities for each device:

    {% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq',None) | list %}
    {%- set ns = namespace(devices = []) %}
    {%- for device in devices %}
      {%- set entities = device_entities(device) | list %}
      {%- if entities %}
        {%- set ns.devices = ns.devices + [ entities ] %}
      {%- endif %}
    {%- endfor %}
    {{ ns.devices }}
2 Likes

Hi, thanks for your example.
is it possible to add the device id and/or name to the array?
I would like to get an array of device with name/id and a entity array for each device.

Currently I’m reading entities with Websocket API. But I search for a way to get the devices and their entities to group teh entities.
Many thanks :slight_smile:

Edit. I think I got it. Here is the result for anyone serching something similar:

    {% set devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('eq',None) | list %}
    
    {%- set ns = namespace(devices = []) %}
    {%- for device in devices %}
      {%- set entities = device_entities(device) | list %}
      {%- if entities %}
        {%- set ns.devices = ns.devices +  [ {device: {"name": device_attr(device, "name"), "entities":[entities ]}} ] %}
      {%- endif %}
    {%- endfor %}
    {{ ns.devices }}

Result:

[
  {
    "b1958109af6746099296912d0bbb9860": {
      "name": "Bew.Sensor Flur",
      "entities": [
        [
          "sensor.bew_sensor_flur_temperature",
          "sensor.bew_sensor_flur_light_level",
          "binary_sensor.bew_sensor_flur_motion",
          "sensor.bew_sensor_flur_battery",
          "switch.bew_sensor_flur_motion",
          "switch.bew_sensor_flur_illuminance"
        ]
      ]
    }
  },
...
3 Likes

Following up on this - THANK YOU Ronny!

Here’s an example cURL command that outputs json

curl -X "POST" "http://xxxxx:8123/api/template" \
     -H 'Authorization: Bearer xxxxx' \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "template": "{% set devices = states | map(attribute=\\"entity_id\\") | map(\\"device_id\\") | unique | reject(\\"eq\\",None) | list %}{%- set ns = namespace(devices = []) %}{%- for device in devices %}{%- set entities = device_entities(device) | list %}{%- if entities %}{%- set ns.devices = ns.devices +  [ {device: {\\"name\\": device_attr(device, \\"name\\"), \\"entities\\": entities}} ] %}{%- endif %}{%- endfor %}{{ ns.devices | tojson }}\\"}"
}'