I have a need to detect if any of my devices are unavailable, like run out of batteries, lost connection, integration misbehave, expired cloud token or anything else.
I want to have a list of devices (not entities) which are in unavailable state. There is no filter for devices only and listing entities is pointless as sometimes entity can be unavailable but device working properly.
There are several topics on forum and solution to use pyscrtipt for it.
But it would be nice to have ability to use filter-entities card for such use case.
In brief, a device is a collection of entities representing a physical product. A device does not have a state but an entity does. So a device cannot report an unavailable state but one of its entities can.
Not only is your request not possible to realise now, it is impossible in principle.
The same device can be integrated by multiple integrations. A receiver for example can be integrated by a DLNA integration and a manufacturer specific integration.
What should the device state be if one of the integrations loses connection, but the other one doesn’t? Is the receiver available or not?
Good question when device have multiple integrations. I can suggest to have 3 states then:
available
non-available
partly-available
As I said the main problem here that if device not available it is really hard to see it anywhere.
I have a battery-sensor card which helps me to track if any sensor require battery change.
And I’d like to see similar card for devices which aren’t working properly.
If Home Assistant loses connection to a physical device, typically one or more of the device’s entities will have a state of unavailable. The connection failure might only be temporary (the device was powered off) so it’s not normally reported as a repair (which is reserved for reporting other issues).
The first link in your initial post leads to a solution that produces a sensor which reports unavailable entities (without employing pyscript). Petro has also posted a template that reports any devices containing unavailable entities. In other words, it’s all achievable now using existing, native resources.
You need to review the concepts of devices, entities and State Objects to understand why your proposal is impractical.
Well if you want to look at every device in your HA instance and classify it as Available, Partly Available, or Unavailable according to whether it has zero, some, or all associated entities being unavailable, you can do something like this. This will create a nested list but you could make it a list of dictionaries or whatever you want.
{% set all_devices = states | map(attribute='entity_id') | map('device_id') | unique | reject('none') | list %}
{% set ns = namespace(device_id=[], device_name=[], device_status=[]) %}
{% for device in all_devices %}
{% set unavailable_list = device_entities(device) | select('is_state', 'unavailable') | list %}
{% set ns.device_id = ns.device_id + [device] %}
{% set ns.device_name = ns.device_name + [device_attr(device, 'name')] %}
{% if unavailable_list | count == 0 %}
{% set status = 'Available' %}
{% elif unavailable_list | count != device_entities(device) | list | count %}
{% set status = 'Partly Available' %}
{% else %}
{% set status = 'Unavailable' %}
{% endif %}
{% set ns.device_status = ns.device_status + [status] %}
{% endfor %}
{{ zip(ns.device_id, ns.device_name, ns.device_status) | rejectattr(2, 'eq', 'Available') | list }}