I found it not obvious enough on how to get notified when battery Zigbee / Zwave devices, or smartplugs, have fallen off the network…
So, I made this blueprint that will report when any of your Zigbee/Zwave(Battery) devices falls off the network. Works with ZHA & Z2M. Works for any SmartPlugs / SmartSwitches also.
You can easily get a notification to your phone, or alerts spoken to your Alexa/Google speaker etc, when any device has gone offline.
This is an upgrade over my previous version - the main difference is this reports just the device that has fallen offline, whereas my previous version would report all sensors that were unreachable. This was unneccesarily verbose when one device had multiple sensors… Also cleaned up the code to make it more readable.
Use {{offline_devices}}
to get a list of all offline devices.
Below is an example on how you woud notify your phone with a list of offline devices.
service: notify.myphone
data:
message: "The following devices are unavailable: {{ offline_devices }}"
Click here for direct link to yaml source on github.
Full source:
blueprint:
name: Report offline zigbee/zwave/battery/smart plug devices
description: Works with Smart Plugs, ZWave, Zigbee etc (Works with ZHA & Z2M)
#By Tahutipai 2024-02-21
#Originally Based on the work of Sybx @ https://community.home-assistant.io/t/low-battery-level-detection-notification-for-all-battery-sensors/258664
#Note: This has been upgraded to report only the device that is offline, not multiple individual sensors within one device
domain: automation
input:
time:
name: Time to test on
description: Test is run at configured time
default: '10:00:00'
selector:
time: {}
day:
name: Weekday to test on
description: 'Test is run at configured time either everyday (0) or on a given
weekday (1: Monday ... 7: Sunday)'
default: 0
selector:
number:
min: 0.0
max: 7.0
mode: slider
step: 1.0
exclude:
name: Excluded Sensors
description: Battery sensors (e.g. smartphone) to exclude from detection. Only entities are supported, devices must be expanded!
default: {entity_id: []}
selector:
target:
entity:
device_class: battery
actions:
name: Actions
description: Call your notification here. {{offline_devices}} will replaced with the name of any offline devices
selector:
action: {}
source_url: https://gist.github.com/Tahutipai/971bf0e07e50ce6190e0dacd73262e2e
variables:
day: !input 'day'
exclude: !input 'exclude'
offline_devices: >-
{% set result = namespace(offline_devices=[]) %}
{% for sensor in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if "unavailable" in sensor | string and not sensor.entity_id in exclude.entity_id %}
{% set result.offline_devices = result.offline_devices + [device_attr(device_id(sensor.entity_id), "name")] %}
{% endif %}
{% endfor %}
{% for binary_sensor in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if "unavailable" in binary_sensor | string and not binary_sensor.entity_id in exclude.entity_id %}
{% set result.offline_devices = result.offline_devices + [device_attr(device_id(binary_sensor.entity_id), "name")] %}
{% endif %}
{% endfor %}
{% for switch in states.switch | selectattr('state','eq','unavailable') %}
{% if switch.entity_id not in exclude.entity_id %}
{% set result.offline_devices = result.offline_devices + [device_attr(device_id(switch.entity_id), "name")] %}
{% endif %}
{% endfor %}
{{result.offline_devices|join('\n')}}
trigger:
- platform: time
at: !input 'time'
condition:
- '{{ offline_devices != '''' and (day | int == 0 or day | int == now().isoweekday()) }}'
action:
- choose: []
default: !input 'actions'
mode: single