WTH not able to list devices

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.

Here are the definitions for device and entity.

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.

2 Likes

Yeah, I see the problem now. But how to get device which is broken, not every entity of this device? Repairs also doesn’t show broken devices.

The only way for me to know that device doesn’t work to see something doesn’t work - lights don’t turn on, blinds don’t open and so on.

Listing entities can help but some entities are expected to be in “unavailalbe” state.

For example my washing maching reply “unavailable” on remaining time when there is no programm running.

But if for any reason my washing machine integration don’t work I’ll never get notifications when washing finishes.

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?

Try this excellent package:

You can set it to ignore entities that are supposed rot be unavailable.
You can also set it to ignore certain domains etc etc.

Really flexible and useful!

Paste this into the template editor and it will list out devices with entities that are unavailable.

{{ states | map(attribute='entity_id') | reject('has_value') | map('device_id') | unique | reject('none') | map('device_attr', 'name') | list }}
2 Likes

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.

For battery I have this card:

type: custom:battery-state-card
filter:
  include:
    - name: entity_id
      value: "*_battery_level"
    - name: attributes.device_class
      value: battery
  exclude:
    - name: state
      value: 15
      operator: ">"
    - name: state
      value: unavailable
      operator: "!="

So I see when battery low of completely drained.

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 }}
1 Like

This working like a charm!

With some help of markdown template card I was able to do follwing:
image

card config:

type: markdown
title: Problem with devices
content: >
  {% 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 = 'warning' %}
    {% else %}
      {% set status = 'error' %}
    {% endif %}
    {% set ns.device_status = ns.device_status + [status] %}
  {% endfor %}

  {% for d in zip(ns.device_id, ns.device_name, ns.device_status) |
  rejectattr(2, 'eq', 'Available') | list %}

  <ha-alert alert-type="{{ d[2] }}"><a href="/config/devices/device/{{d[0]}}">{{
  d[1] }}</a></ha-alert>

  {% endfor %}
{% endfor %}
2 Likes