Forked from Mr-Groch/offline-notification-for-sensors-with-last_seen.yaml
Last active
August 23, 2023 11:59
-
-
Save gamba69/75582be835b8c57c073ef40c325de8fb to your computer and use it in GitHub Desktop.
Home Assistant Blueprint: Offline detection for Z2M devices with last_seen
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
blueprint: | |
name: Offline detection for Z2M devices with last_seen | |
description: | |
Regularly test all sensors ended with '_last_seen' in name and 'timestamp' device_class | |
('last seen' Z2M sensors) to detect offline devices and if so - execute an action. | |
domain: automation | |
input: | |
unseen: | |
name: Minutes last seen | |
description: Devices not seen this amount of time are assumed to be offline | |
default: 60 | |
selector: | |
number: | |
min: 15 | |
max: 360 | |
unit_of_measurement: m | |
mode: slider | |
step: 15 | |
hours: | |
name: Repeat at hour(a) | |
description: Check devices at specified hours (time_pattern trigger value) | |
default: "*" | |
selector: | |
text: | |
minutes: | |
name: Repeat at minutes(a) | |
description: Check devices at specified minutes (time_pattern trigger values) | |
default: "/30" | |
selector: | |
text: | |
exclude: | |
name: Exclude Devices | |
description: Select MQTT device(s) to exclude from monitoring | |
default: [] | |
selector: | |
device: | |
multiple: true | |
filter: | |
- integration: mqtt | |
actions: | |
name: Actions | |
description: | |
Notifications or similar to be run. {{sensors}} is replaced with | |
the names of sensors being offline. | |
selector: | |
action: {} | |
source_url: https://gist.github.com/gamba69/75582be835b8c57c073ef40c325de8fb | |
variables: | |
unseen: !input "unseen" | |
exclude: !input "exclude" | |
sensors: >- | |
{% macro zigbee_unseen(minutes) %} | |
{% set result = namespace(devices=[], formatted=[]) %} | |
{% for state in states %} | |
{% if state.entity_id.endswith("_last_seen") and not device_id(state.entity_id) in exclude and ( | |
(not has_value(state.entity_id)) | |
or (as_timestamp(now()) - as_timestamp(state.state) > minutes * 60 ) | |
) %} | |
{% if not has_value(state.entity_id) %} | |
{% set result.devices = result.devices + [({"name": device_attr(state.entity_id, "name_by_user"), "seen": "-" })] %} | |
{% else %} | |
{% set result.devices = result.devices + [({"name": device_attr(state.entity_id, "name_by_user"), "seen": state.state })] %} | |
{% endif %} | |
{% endif %} | |
{% endfor %} | |
{% set result.devices = result.devices|sort(attribute="seen") %} | |
{% for d in result.devices %} | |
{% if d.seen == "-" %} | |
{% set result.formatted = result.formatted + [d.name ~ ' (offline)'] %} | |
{% else %} | |
{% set result.formatted = result.formatted + [d.name ~ ' (' ~ relative_time(strptime(d.seen, '%Y-%m-%dT%H:%M:%S%z')) ~ ')'] %} | |
{% endif %} | |
{% endfor %} | |
{{ result.formatted|join(', ') }} | |
{% endmacro %} | |
{{ zigbee_unseen(unseen) }} | |
trigger: | |
- platform: time_pattern | |
hours: !input hours | |
minutes: !input minutes | |
condition: | |
- "{{ sensors != '' }}" | |
action: | |
- choose: [] | |
default: !input "actions" | |
mode: single |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment