I have several sensors that I wanted to make sure are still reporting information. I wrote the automation & script listed below that will pop up a persistent notification if any device I’m watching hasn’t updated within the required amount of time. If that device later updates, the persistent notification is removed. This allows me to very quickly see which devices might need to be checked on from the HomeAssistant web interface.
##
# Check last update of an entity
automation:
- alias: Watchdog
trigger:
platform: time_pattern
minutes: "/5"
action:
- service: script.watchdog
data:
entity_id: sensor.living_room_51_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.kitchen_52_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.bedroom_53_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.porch_54_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.guestroom_55_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.office_56_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.garage_57_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.baby_58_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.attic2_59_10
time: 3600
- service: script.watchdog
data:
entity_id: sensor.crawlspace_80_1
time: 7200
- service: script.watchdog
data:
entity_id: sensor.living_room_temperature
time: 600
script:
watchdog:
alias: 'Entity Watchdog'
# description: "Entity Watchdog"
# fields:
# entity_id:
# description: 'The entity to check'
# example: 'binary_sensor.sensor'
# time:
# description: 'The max number of seconds before an alert'
# example: '300'
sequence:
- service: persistent_notification.dismiss
data_template:
notification_id: 'Watchdog:{{entity_id}}'
- condition: template
value_template: '{{ ( now()|as_timestamp() - states[entity_id].last_updated|default(0,true)|as_timestamp()|default(0,true) ) > time }}'
- service: persistent_notification.create
data_template:
title: 'Watchdog: {{ entity_id }}'
message: >
{%- if states[entity_id].last_updated %}
Entity {{ entity_id }} has not been updated in {{ ( now()|as_timestamp() - states[entity_id].last_updated|default(0,true)|as_timestamp()|default(0,true) )|int }}
seconds which is longer than the max allowed of {{ time }}.
{%- else %}
Entity {{ entity_id }} does not seem to exist.
{%- endif %}
notification_id: 'Watchdog:{{entity_id}}'
Posting here in case anyone wants to use this code to do something similar.