Watchdog Automation / Script for checking on sensors

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.

6 Likes

Thanks for sharing!
I can see a problem here - it won’t survive HA restarts properly because of last_updated being set to HA start time.
Not a big deal for some but in my use case I had to rely upon separately stored times.

True on not surviving HA restarts - though if the device was malfunctioning, the alert would appear X seconds after the restart. HA restarts are going to cause a problem with any type of watchdog though as an update could be missed while HA is restarting.

For my case, I’m more concerned about watching unreliable sensors (eg. Wifi based and disconnected from wifi for some reason, or, in my case, MySensors based and the MySensors network is down).

Yeah, it all depends on how often you check your sensors.
My PIRs being checked every 24 hours (as a measure against low battery/loss of RF coverage) because there is no point to do it more often.

On the other hand, my key temperature sensors have 4 mins expiry interval so when they become unknown, an automation reports them. And obviously I don’t bother about restarts here :wink:

Nothing to see here…

I’m reviving this old thread because I was trying to modify the script here without much success (I’m only about 3 months into HA).

I have noticed that on my phone these notifications would only show up if I specifically open the Home Assistant app. I have also noticed that using the service notify.mobile_app_***, I would get almost instant notifications on my phone, regardless if I open the app or not.

So how could I replace the persistent_notification.create service with notify.mobile_app_***? I have tried several times without much success.

Thank you!

Untested - but to use companion app notifications instead of persistent notifications, I’d suggest trying something like this (don’t copy and paste - I might have messed up indentation):

     - service: notify.mobile_app_<your_device_id_here>
       data: 
          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 %}

Thanks for this! Very useful for a newbie like me.

1 Like