Alert when sensors stop updating

Thank you so much. been fighting for ages! :smile: I

did need to change the following:

  • entity_id: sensor.wohnzimmer_no_activity
    platform: state
    to: ‘True’

Just wanted to share my solution to this - I made a template sensor for a lot of wireless sensors so I can graph the update time and do easy automations based on time (e.g. restart addons, send me Telegram alerts, etc)

Here are two examples with MQTT RTL_433 sensors, but it should work for any sensor.

sensor:

  - platform: template
    sensors:
      outside_front_sensor_age:
        friendly_name: "Outside Front Sensor Age"
        entity_id: sensor.time
        value_template: >-
          {% if states.sensor.outside_front_temperature.last_changed > states.sensor.outside_front_humidity.last_changed %}
            {{ (states.sensor.time.last_changed - states.sensor.outside_front_temperature.last_changed).total_seconds() | round(0) }}
          {% else %}
            {{ (states.sensor.time.last_changed - states.sensor.outside_front_humidity.last_changed).total_seconds() | round(0) }}
          {% endif %}
        unit_of_measurement: "Seconds"

  - platform: template
    sensors:
      outside_rear_sensor_age:
        friendly_name: "Outside Rear Sensor Age"
        entity_id: sensor.time
        value_template: >-
          {% if states.sensor.outside_rear_temperature.last_changed > states.sensor.outside_rear_humidity.last_changed %}
            {{ (states.sensor.time.last_changed - states.sensor.outside_rear_temperature.last_changed).total_seconds() | round(0) }}
          {% else %}
            {{ (states.sensor.time.last_changed - states.sensor.outside_rear_humidity.last_changed).total_seconds() | round(0) }}
          {% endif %}
        unit_of_measurement: "Seconds"

(the “if” lets me combine both readings from each sensor into one, since any update tells me my sensor is alive)

End result I can do graphs:

And easy single automation can tell me which sensor has what problem in human readable text:

#System Status Alerts

  - alias: 'Telegram Me Check Sensor Alert'
    trigger:
      # Low Battery Alerts
      # Binary Sensor Alerts (on = alarm)
      - entity_id: 
          # Acurite Temp & Humidity Sensors
          - binary_sensor.outside_front_sensor_battery_low
          - binary_sensor.outside_rear_sensor_battery_low
          - binary_sensor.attic_sensor_battery_low
        platform: state
        from: 'off'
        to: 'on'
        # Reduce flapping
        for:
          hours: 12
      # Monitor sensor age - not reporting in
      - platform: numeric_state
        entity_id:
          # Acurite Temp & Humidity Sensors Age Expired
          - sensor.outside_front_sensor_age
          - sensor.outside_rear_sensor_age
          - sensor.attic_sensor_age
        # Seconds for timeout
        # 900 = 15 min
        # 1800 = 30 min
        # 3600 = 1 hour
        # 7200 = 2 hour
        # 10800 = 3 hour
        # 21600 = 6 hour
        # 43200 = 12 hour
        # 86400 = 24 hour
        above: 43200
    action:
      service: notify.telegram_alert_me
      data_template:
        message: |
          Warning - Check Sensor:
          {{ trigger.to_state.attributes.friendly_name }}

Result of this automation, I get neat concise messages that tell me which sensor has what problem by just listing all of them in a single automation:
image
image

I also used this to make a watchdog for RTL_433 addon when the SDR locks up by condition “AND”-ing all the sensors being >5 min old:

  - alias: "RTL433 to MQTT Watchdog"
    trigger:
      - platform: time_pattern
        minutes: '/5'
    condition:
      # Acurite Temp & Humidity Sensors Age Expired
      - condition: numeric_state
        entity_id: sensor.outside_front_sensor_age
        above: 600
      - condition: numeric_state
        entity_id: sensor.outside_rear_sensor_age
        above: 600
      - condition: numeric_state
        entity_id: sensor.attic_sensor_age
        above: 600
    action:
      - service: hassio.addon_restart
        data:
          addon: f3248fb7_rtl4332mqtt
      - service: notify.telegram_alert_me
        data:
          message: |
            Warning - Multiple sensors were unresponsive!
            Watchdog attempted to restart RTL433 to MQTT.
2 Likes

This is exactly what I’m trying to do to alert me to climate sensors not reporting in a timely manner as I’ve got heating automation tied to them. I’m trying to implement the first chunk of code to create the sensors but am getting this error: TemplateError(‘UndefinedError: ‘None’ has no attribute ‘last_changed’’) while processing template…
I saw references online about this error being due to the system calling to a entity that’s not created yet? Not sure what’s happening and any help would be greatly appreciated.

Sorry to ask but im coming from a entry point of view and i gotta ask: by stand alone that means this in itself is its own ‘program’ ?

ive been using homeassistant for almost two years now and only have had it installed on (currently) a rpi4 with ssd, and the main homeassistant image.

for that reason, i have no idea what container or docker is or are, and know little about what i would need to install this, but it looks like exactly what im looking for.

i have hacs, installed appdaemon but dont use it and have no idea what it does.

any advice on setting that up on my system would be awesome, i seen the github and understand ‘build into a single binary janitor file’, and thats where my understanding stops.

thanks!

Standalone means indeed that it does not need home assistant itself to work, it runs on its own on the operating system. I am not familiar with the rpi homeassistant image but I would imagine that you would need to download the source code of janitor, build a binary as described in the README and then copy and run that binary to your homeassistant machine or wherever you want to run it.

I’m not really sure what the point of all this complexity is.

You can do just this:

trigger:
  - platform: state
    entity_id: sensor.living_room_temperature_1
    for: '00:10:00'
  - platform: state
    entity_id: sensor.outside_temperature
    for: '00:10:00'
  - platform: state
    entity_id: sensor.bedroom_temperature
    for: '00:10:00'
  - platform: state
    entity_id: sensor.kitchen_temperature
    for: '00:10:00'
action:
 - service: notify.telegram
   data_template:
     message: "{{ trigger.from_state.attributes.friendly_name }} has not updated since 10 min"
4 Likes

Genius! Thanks. I’m using this now to monitor my xiaomi zigbee temperature sensors; they control my heating system so I have automations configured to check battery level, minimum, maximum, and now update frequency. I changed the “for:” to 4 hours though because these zigbee sensors do not send any update if the temperature is constant. This is good enough as an early warning system that something is up.

I’m using the same setup. But never had issues with one “disappearing”.

Maybe there’s an issue in your Zigbee setup? :thinking:

Anyway, in case you’re using Zigbee2MQTT you might just want to activate the ‘last seen’ field and compare it to the local time every hour or so. Home Assistant will only react on temperature changes via ‘for’ while the sensor might have send an update in the meantime, but the value hasn’t changed.

The last seen shows also up in the device list, for further investigations:

Btw if you’re using Temperature based radiator valves to heat, this might be interesting for you:

1 Like

What if the temperature doesn’t change for 10 mins? Surely that cannot be uncommon?

1 Like

Thanks for the link, in my own experience the last 2 years I find the xiaomi sensors are quick enough to detect other sources of heat in our home (eg fireplace) and the home assistant climate control responds to that appropriately. I also run open-window (and open-door) detection with xiaomi contact sensors, and these work well.

I’m using a conbee 2 and deconz - the only reasons a xiaomi climate sensor will drop off the network is if a crucial router has been manually switched off (several light bulbs in a room), or if the battery goes dead. Unfortunately deconz doesn’t seem to do a great job of reporting the battery level with xiaomi devices, and I’ve had two climate devices run out of battery (after 2 + years of activity) “silently” whilst their battery life was still reporting 30% or 40%. When they run out of battery they basically stop reporting temperature and humidity data, and the above automation detects this.

this all seems a little bit complicated.
New Idea:
Check Sensor state and just use the for part.
than you get a trigger every time when the sensor don´t update OR the value is the same.
So you have to check if the last_changed event is also older than your threshold.
With that solution you don´t need a time_date sensor.
But: I haven´t tried that. Will do in the next days.

1 Like

Any chance you have a solution for my tellstick-sensors that don’t have the “last_changed” attribute? :slight_smile:

I have the same problem but my sensors only have unit_of_measurment and friendly_name, not that helpful :frowning:

EVERYTHING has a last_changed attribute, you won’t see it in the states part of developer tools, but you WILL if you paste it in to template editor:

{{ states.sensor.my_sensor_name.last_changed }}

Everything has a last_changed property. Hence you wont see it in the list of attributes.

Petro explained it well here: https://community.home-assistant.io/t/last-updated-state-and-last-changed-in-lovelave/101701/2

oh it actually is there! it doesnt seem to recognize my timezone though? I have time_zone: Europe/Stockholm in my configuration. Is there any other place I have missed?

2021-12-15 11:03:27.091115+00:00, should be +01:00

mabye it doesnt matter for this automation?

Kindof solved it but dont know if its the right approach

{{ as_timestamp(states.sensor.vardagsrum_temperature.last_changed) | timestamp_custom('%D %H:%M:%S',True) }}

this gives me the wrong date format though: 12/15/21 12:03:27 instead of 2021-12-15 12:03:27.091115+00:00

EDIT.

forget that I think @tom_l solved this in his original post here
or mabye even better I just saw that @RubenKelevra seems to have the easiest solution so I’m tryin that one. Hope it works!

I cannot figure out what is happening in these state changes/updates :frowning:

I have a sensor integrated thought MQTT integration. It works well, but I wanted to add notification we sensors stop updating. Based on this thread, I’ve created automation, comparing last_updated with utcnow() and if its older than 10 minutes it sens notification. Looks ok, but I’ve got strange behavior:

20:24:52 - automation triggered, payload:
sensor has not updated since 10 minutes
sensor has updated exactly 902 seconds ago
now local   : 2022-01-07 20:24:52.172910+01:00
last_updated: 2022-01-07 20:09:49.879704+01:00
last_changed: 2022-01-07 18:49:08.860460+01:00

Is there any scheduler working each 900 seconds? Why 900 if I’ve set it for 600 (10 minutes)?

after ~5 minutes it is the same:

sensor has not updated since 10 minutes
sensor has updated exactly 903 seconds ago
now local   : 2022-01-07 20:29:55.277688+01:00
last_updated: 2022-01-07 20:14:52.162965+01:00
last_changed: 2022-01-07 18:49:08.860460+01:00

so now it looks like sensor was updated BEFORE first occurrence of automation trigger - so why it was triggered?
Strange.

What’s even stranger, situation is the same, when I’ve change timeout to 31minutes:

sensor not updated for 31 minutes
sensor updated exactly 2162 seconds ago
---
now local   : 2022-01-08 01:13:05.612959+01:00
last_updated: 2022-01-08 00:37:03.321747+01:00
last_changed: 2022-01-08 00:37:03.321747+01:00

and second occurance:

sensor not updated for 31 minutes
sensor updated exactly 2162 seconds ago
---
now local   : 2022-01-08 01:18:08.189436+01:00
last_updated: 2022-01-08 00:42:05.602653+01:00
last_changed: 2022-01-08 00:37:03.321747+01:00

both triggered with 5 minutes delay (strange, but let’s skip it), but showing different last_updated historical values. Any idea why?

Hi, I would like to ask what the code will look like if I want the notification to appear if the sensor is not updated for 12 minutes? I don’t know “60 > 30” at the end.

Mine automation looks like follows, it’s simple and should work, but as I mentioned above - something is wrong :frowning:

- id: '1641575869039'
  alias: sensors not updating
  description: triggered by state not changed for 31min but only if additionaly last_upd$
    not changed for 31min -> sends notification
  trigger:
  - platform: state
    entity_id: sensor.gleba01_moisture
    for: 00:31:00
  - platform: state
    entity_id: sensor.gleba02_moisture
    for: 00:31:00
  condition:
  - condition: template
    value_template: '{{ (utcnow() - trigger.from_state.last_updated).seconds > 31*60}}'
  action:
  - service: notify.pawel_mail
    data_template:
      # multiple lines literals are a bit tricky in YAML [https://stackoverflow.com/ques$
      #   HA uses single quotes '' and double line end
      message: '{{ trigger.from_state.attributes.friendly_name }} not updated for
        31 minutes

        updated exactly {{(utcnow() - trigger.from_state.last_updated).seconds}} seconds$

        state:  {{ trigger.from_state.state }}

        ---

        now local   : {{now()|as_local}}

        last_updated: {{ trigger.from_state.last_updated|as_local }}

        last_changed: {{ trigger.from_state.last_changed|as_local }}

        ---

        UTC now         : {{utcnow()}}

        UTC last_updated: {{ trigger.from_state.last_updated }}

        UTC last_changed: {{ trigger.from_state.last_changed }}

        '
  mode: single