Devices via REST API

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"
        ]
      ]
    }
  },
...
4 Likes