Alert when sensors stop updating

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

I do this for both sensors and zwave devices. This is a template for a zwave device that has a temperature sensor. When I add a new device like this I use sed to create the new package.

First, there is a sensor that calculates the latency. Second, there is a binary_sensor that determines if the latency exceeds a limit. When that binary_sensor fires it can run an automation, generate an alert,etc. With this sensor, whenever is gets a power fluctuation it stops reporting temperature and I’ve determine that if I repush the zwave configuration it starts reporting again - so I run an automation automatically to do it, Most of my config is now templated like this and it makes is easy to change things.


sed 's/multisensor_1/basement/g' zwave_template_aoetec_multisensor_package.yaml | sed 's/50/50/g' > /mnt/c/github/ha_sage/packages/zwave_template_basement_package.yaml


# multisensor_1
sensor:
  - platform: template
    sensors:
      zwave_multisensor_1_latency:
        unit_of_measurement: secs
        value_template: "{{ as_timestamp(now(), 0) | int(0) - as_timestamp(state_attr('zwave.multisensor_1','receivedTS') | string | truncate(19,True,'',0) | as_datetime, 0)| int(0) }}"
        availability_template: "{{ as_timestamp(state_attr('zwave.multisensor_1','receivedTS') | string | truncate(19,True,'',0) | as_datetime,0)| int(0) > 0 }}"
      zwave_multisensor_1_temp_latency:
        unit_of_measurement: secs
        value_template: "{{ ((as_timestamp(now(), 0) | int(0)) - as_timestamp(states.sensor.multisensor_1_temperature.last_updated,0) | int(0)) }}"
        availability_template: "{{ as_timestamp(states.sensor.multisensor_1_temperature.last_updated,0) | int(0) > 0 }}"

binary_sensor:
  - platform: template
    sensors:
      zwave_multisensor_1_online:
        value_template: "{{ (( state_attr('zwave.multisensor_1','wake_up_interval')|int(0) * 2) - (states('sensor.zwave_multisensor_1_latency') | int(0))) > 0}}"
      zwave_multisensor_1_temp_online:
        value_template: "{{ (( state_attr('zwave.multisensor_1','wake_up_interval')|int(0) * 2) - (states('sensor.zwave_multisensor_1_temp_latency') | int(0))) > 0}}"

recorder:
  include:
    entities:
      - binary_sensor.zwave_multisensor_1_online
      - binary_sensor.zwave_multisensor_1_temp_online
      - zwave.multisensor_1

You can trigger a notification using a restartable automation. Just set a delay has the first action that longer than the interval between updates and if a new update is not received, the next action is fired which sends you a notification.

alias: Notify if CumulusMX data not updating
description: ''
trigger:
  - platform: mqtt
    topic: CumulusMX/Interval
condition: []
action:
  - delay:
      hours: 0
      minutes: 2
      seconds: 0
      milliseconds: 0
  - service: notify.mobile_app_dave_phone
    data:
      message: CumulusMX Data not received
      title: CumulusMX Down
mode: restart
2 Likes

this simple automation works for me with no extra tamplate sensors.

as I use it for my rtl4332mqtt sensors, so I always include a time as atribute in any update I publish in rtl4332mqtt. so I can simply utilize that atribute.

alias: 'sensor not updating notifications '
description: ''
trigger:
  - platform: state
    entity_id:
      - sensor.boysroom433
      - sensor.office433
      - sensor.playroom433
      - sensor.library433
      - sensor.outside433
    attribute: time
    for:
      hours: 0
      minutes: 10
      seconds: 0
condition: []
action:
  - service: notify.whatsapp
    data:
      message: >-
        {{ trigger.from_state.attributes.friendly_name }} not updated for 10
        minutes
mode: parallel

As I said I use this on the “last seen” attribute, which change which each received package from the device. So the temperature doesn’t need to change for it to trip properly.

Hey there, so I am struggling to understand the purpose of the if/else logic in your 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 %}

Could you explain why you’re comparing two values from presumably the same physical device? If something where hung I would assume both sensors of the device would be hung at the same time

Yes. Because it reports separate values, there are 3 possible cases.

Temperature changed
Humidity changed
Temperature AND Humidity changed

I want to capture the “last changed” as whichever most recently changed for the purposes of debugging and validation.

Psudocode:

If ( temperature_changed "more recently than" humidity_changed ); then
    return ( now - when temperature changed )
else
    // This means humidity changed more recently than temperature
    return ( now - when humidity changed )

Note: “changed” is not the same as “updated”. I was using “changed” because in some cases if the incoming MQTT data is corrupted (yay wireless sensors) and I receive a value outside of the sensor’s min/max range possible like -80F or +300F, I throw it out and copy the last valid data when it brings the MQTT value in. But that process still counts as an “update”. By using the “last changed” value I can tell if it’s stuck spewing garbage data (which sometimes happens with low batteries), and in my use the odds of neither temp nor humidity changing by even 0.1F or 1% over some minutes are basically zero so it gives me a good measure.

Note 2: I think now states.sensor.time.last_changed can be replaced by now() but it used to be you had to use a separate sensor in templates long ago.

I tried to use this seemingly clear and simple code for my purposes, but it doesn’t seem to work

are there any prerequisites for this to operate as expected?

Genius! That’s so slick. Thanks.