What attributes do devices have?

I’m trying to make a template that lists unavailable devices. I’ve made it work for entities, but of course when a device is unavailable, all its entities are unavailable at the same time and it’s really the device I’m interested in. I’m making progress with this and the new device_id and device_attr template filters:

{% set device_ids = states
  | selectattr('state', 'in', ['unavailable', 'unknown'])
  | map(attribute='entity_id')
  | map("device_id")
  | map("string")
  | unique
  | select("ne", "None")
  | list
%}

{% for device_id in device_ids %}
  {{ device_id }}
  {{ device_attr(device_id, "name") }}
{% endfor %}

This produces a list of IDs and names, but not the name I assigned. For example, I unplugged a sonoff basic, and the list shows:

7c0c371a33b490cdf1e791e0b98af344
0x00124b101e7939c1

I know this device is called “Sonoff Switch F344”, but the name attribute produces the original zigbee2mqtt-assigned name. I admit I guessed that the attribute was called ‘name’. I tried ‘friendly_name’ but that doesn’t seem to be a device attribute. The docs show ‘manufacturer’ as an example attribute, but I can’t find a list of attributes nor how I go about querying a device to see what attributes it has.

So my questions are:

  • Is there a way to find out what attributes are exposed by a given device (not entity)?
  • Is there a better way for me to list my unavailable devices?
1 Like

Looks like they’re the one used to create the device:

So, for example {{ device_attr(device_id('switch.shelly1'), 'sw_version') }} can work, others are name

6 Likes

name_by_user is super helpful, thanks!

2 Likes

see also Device Properties

I am curious, why resurrect a 4 year old thread when the info is simply available via Developer Tools ?

where can I find the available device attributes like configuration_url for templating device_attr(device_or_entity_id, attr_name) in the developer tools?

What device do you have that contains a configuration_url as an attribute?

all Shelly devices for example

my use case:
script which uses selector to select a Shelly Plug

fields:
  shelly_device:
    selector:
      device:
        entity:
          - domain: switch
        filter:
          - model: Shelly Plug S Gen3
    name: Shelly Plug
    required: true
...

and getting the IP address of the Shelly Plug for a low level shell command like this:

      - action: shell_command.shelly_plug_led
        metadata: {}
        data:
          ip: "{{ device_attr(shelly_device,'configuration_url')[7:] }}"
          ...
        response_variable: response

I have some. I think the point is that there is no way to query a device to find out what properties it has. You can do this for an entity:

{{ states['sensor.my_sensor'] }}

…but for a device, it looks like you have to either know what you’re looking for, or assume the list in the link above is complete, and test each in turn.

Here’s a template that returns all the configuration_url device properties in your system, sorted by device name and pretty-printed:

{% set ids = set(states|map(attribute='entity_id')|map('device_id')|select) %}
{% set names = ids|map('device_name') %}
{% set curls = ids|map('device_attr','configuration_url') %}
{% set ns = namespace(d={}) %}
{% for name, curl in zip(names, curls) %}
{% if curl %}
{% set ns.d = dict(ns.d, **{name: curl}) %}
{% endif %}
{% endfor %}
{% set f = "%-" ~ (ns.d|map('length')|max + 1) ~ "s: %s" %}
{% for k in ns.d|sort -%}
{{ f|format(k, ns.d[k]) }}
{% endfor %}
2 Likes

Then I stand corrected, appreciate the clarification!

Just ran that template in developers tools. Seems I have a long list of them.

Apparently I do too :man_facepalming: Mostly showing my server’s local IP Address for each individual drives and HACs integrations.

Expanded template showing your system’s device properties for all the ones listed here:

{% set props = ('area_id', 'config_entries', 'configuration_url', 'connections',
                'default_manufacturer', 'default_model', 'default_name',
                'entry_type', 'hw_version', 'id', 'identifiers', 'name',
                'name_by_user', 'manufacturer', 'model', 'model_id',
                'serial_number', 'suggested_area', 'sw_version', 'via_device') %}
{% set ids = states|map(attribute='entity_id')|map('device_id')|select|unique|list %}
{% set names = ids|map('device_name')|list -%}
{% for prop in props -%}
{% set ns = namespace(d={}) -%}
{% set pl = ids|map('device_attr', prop) -%}
{% for id, name, p in zip(ids, names, pl) -%}
{% if p -%}
{% set ns.d = dict(ns.d, **{name if name else '['~id~']': p}) -%}
{% endif -%}
{% endfor %}
Property: {{ prop }}
{{ "=" * (prop|length+10) }}
{% if ns.d -%}
{% set f = "%-" ~ (ns.d|map('length')|max + 1) ~ "s %s" -%}
{% for k in ns.d|sort -%}
{{ f|format(k~":", ns.d[k]) }}
{% endfor -%}
{% else -%}
No devices with this property
{% endif -%}
{% endfor -%}
1 Like