Alert when sensors stop updating

Yes I would be interested! If you can share that would be great!

Take a moment to think about what you have created:

  • The trigger occurs only when sensor_wohnzimmer_temp changes state (or one of its attributes changes).

  • The condition is looking for the situation when the sensor has not changed its state for over 30 minutes.

The automationā€™s condition is evaluated only when the sensor changes state (so your value_template will never produce a true result).

I suggest you create a Template Binary Sensor. Set its entity_id to sensor.time so that the Template Binary Sensor is evaluated every minute. If you are not already using sensor.time you will need to define it in your configuration.yaml file.

  - platform: template
    sensors:
      wohnzimmer_no_activity:
        entity_id: sensor.time
        value_template: >
          {{ (utcnow() - states.sensor.sensor_wohnzimmer_temp.last_updated).seconds // 60 > 30 }}

Use the Template Binary Sensor to trigger your automation.

- id: sensor_warning_test
  alias: Sensor Warning Test
  trigger:
  - entity_id: sensor.wohnzimmer_no_activity
    platform: state
    to: 'on'
  action:
  - data:
      message: Sensor Wohnzimmer has not been updated for 30 Minutes!
      title: Sensor-Warning!
    service: notify.alles

EDIT

Simplified the template.

5 Likes

Thatā€™s it, thanks a lot!

Very interested in seeing what you developed. Thanks

Janitor is a small and standalone tool for monitoring the availability of IOT devices. It supports currently MQTT and Ping and alerts via Telegram.

The source code and instructions to install and use are available on Github:

1 Like

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"
5 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?