It sometimes happens that devices silently go down without any chance to tell Home Assistant about that.
Most of the time I see this with battery powered devices like door/window sensors (but also others). The last battery level it reports was for example 45% or so and the next day the device is offline and will never come back.
If you want to know about this, here is my solution to detect this issue:
(REMARK: not the latest version, see UPDATE post)
input_select:
connectivity_check_blacklist:
name: "Connection check: Devices blacklist"
options:
- device-1-which-should-never-be-alarmed
- device-2-which-should-never-be-alarmed
- ...
icon: mdi:block-helper
automation:
- id: "connectivity_check"
alias: "Connection check: Check connectivity for all devices"
mode: restart
trigger:
- platform: time
at: "08:10"
action:
- service: script.connectivity_check
script:
connectivity_check:
alias: "Connection check: Check connectivity for all devices"
mode: queued
variables:
devices: >-
{# create empty list to hold the devices #}
{% set ns = namespace(devices = []) %}
{# loop through ALL entities #}
{% for entity in (states | map(attribute='entity_id') | list) %}
{# ignore everything which does not have a device #}
{% if device_attr(entity, 'name_by_user') != none %}
{# this is to avoid doubled entries, so remove possible former entries and add a new one #}
{% set ns.devices = ns.devices | reject('in', [(device_attr(entity, 'name_by_user'), area_name(entity))]) | list %}
{% set ns.devices = ns.devices + [(device_attr(entity, 'name_by_user'), area_name(entity))] %}
{% endif %}
{% endfor %}
{# loop again through ALL entities #}
{% for entity in (states | map(attribute='entity_id') | list) %}
{# ignore everything which does not have a device #}
{% if device_attr(entity, 'name_by_user') != none %}
{# look at last updated and last changed #}
{% if states[entity].last_changed > (now() - timedelta(hours=8)) or states[entity].last_updated > (now() - timedelta(hours=8)) %}
{# remove every entry where any entity of the device was updated or changed within the last 8 hours #}
{% set ns.devices = ns.devices | reject('in', [(device_attr(entity, 'name_by_user'), area_name(entity))]) | list %}
{% endif %}
{% endif %}
{% endfor %}
{# remove any device from the blacklist #}
{% for device in ns.devices %}
{% if device[0] in state_attr('input_select.connectivity_check_blacklist', 'options') %}
{% set ns.devices = ns.devices | reject('in', [(device[0], device[1])]) | list %}
{% endif %}
{% endfor %}
{# here we have our unresponsive devices list #}
{{ ns.devices }}
sequence:
- repeat:
# loop through our devices
for_each: "{{ devices }}"
sequence:
# send message
- service: script.message_warning_device_offline
data_template:
device: "{{ repeat.item[0] }}"
area: "{{ repeat.item[1] }}"
Itās fully automated, no need to do any configurations on your devices or entities. Any future device is covered also automatically.
What is it doing?
-
first step: create a list of all devices by looping through every entity and get the device (if set) into a list (together with the area)
-
second step: loop again through all entities and check the last update and last change; if the entity changed anyhow then remove the corresponding device from the list (no need to worry, this device seems to be alive)
-
third step: remove any device defined in the blacklist (I use an input_select helper for this, if anyone has a better ideaā¦)
-
last step: the remaining devices in the list must be those where no entity changed within the last 8 hours, so send an alarm message
The messaging script āscript.message_warning_device_offlineā turns everything into a Pushover message but this is not the scope of this post. Feel free to adapt it to your needs.
The solution is working fine with my Home Assistant setup and my devices. There may be configurations where this is not working as expected.
Blacklist
I defined it in a YAML file, but it should be possible to setup a dropdown helper in the UI for this purpose. Using the UI gives the option to change the blacklist in the UI.
Remark
It seems, after restarting Home Assistant, entities of dead devices get a refreshed ālast_changedā and/or ālast_updatedā. For the script it looks like this devices are online. But after the defined 8 hours this devices were detected again.