Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save slipperysaxophone/6b4c31b8f75fcb3d3817007e35384396 to your computer and use it in GitHub Desktop.
Save slipperysaxophone/6b4c31b8f75fcb3d3817007e35384396 to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Low battery level detection & notification for all battery sensors
blueprint:
name: Report offline zigbee/zwave/battery/smart plug devices
description: Works with Smart Plugs, ZWave, Zigbee etc (Works with ZHA & Z2M)
#SlipperySaxophone 2024-07-12 Now detects devices that have been offline
#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
stale:
name: Days Unresponsive
description: 'Number of days of no communication to consider a node dead. Recommended >= 2'
default: 2
selector:
number:
min: 1
max: 365
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
- switch
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/slipperysaxophone/6b4c31b8f75fcb3d3817007e35384396
variables:
day: !input 'day'
exclude: !input 'exclude'
stale: !input 'stale'
offline_devices: >-
{% set stale_date = now() - timedelta(days=stale) %}
{% set results = namespace(offline_devices=[]) %}
{% set devices = namespace(dev_ids=[]) %}
{% for sensor in states.sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if sensor.entity_id in exclude.entity_id %}
{% continue %}
{% endif %}
{% if "unavailable" in sensor | string %}
{% set results.offline_devices = results.offline_devices + [device_attr(device_id(sensor.entity_id), "name")] %}
{% continue %}
{% endif %}
{% set devices.dev_ids = devices.dev_ids + [device_id(sensor.entity_id)] %}
{% endfor %}
{% for sensor in states.binary_sensor | selectattr('attributes.device_class', 'defined') | selectattr('attributes.device_class', '==', 'battery') %}
{% if sensor.entity_id in exclude.entity_id %}
{% continue %}
{% endif %}
{% if "unavailable" in sensor | string %}
{% set results.offline_devices = results.offline_devices + [device_attr(device_id(sensor.entity_id), "name")] %}
{% continue %}
{% endif %}
{% set devices.dev_ids = devices.dev_ids + [device_id(sensor.entity_id)] %}
{% endfor %}
{% for switch in states.switch | selectattr('state','eq','unavailable') %}
{% if switch.entity_id not in exclude.entity_id %}
{% set results.offline_devices = results.offline_devices + [device_attr(device_id(switch.entity_id), "name")] %}
{% endif %}
{% set devices.dev_ids = devices.dev_ids + [device_id(switch.entity_id)] %}
{%endfor %}
{% for device_id in devices.dev_ids %}
{% for device_entity in device_entities(device_id)%}
{% if device_entity | contains("last_seen") %}
{% set last_seen = states(device_entity) %}
{% if not as_timestamp(last_seen, default=None) or stale_date | as_timestamp > last_seen | as_timestamp %}
{% set results.offline_devices = results.offline_devices + [device_attr(device_id, "name")] %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{{results.offline_devices|sort|unique|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
@slipperysaxophone
Copy link
Author

Modifed in order to detect devices that have completely fallen off the network and have not been tagged as unavailable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment