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:
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?
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?
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 %}