Send notification when there is no mqtt sensor update for more then 30 minutes

I like to make an automation that gives a notification on whatsapp, if there is no mqtt sensor update for more then 30 minutes.

The sensor is sending every 15min the current value. Most of the time it is the same value. So I don’t know if the sensor is down or not.

The whatsapp notification works already, but I don’t find a way to test the mqtt sensor update past time.

Is this possible to do?

Yes, quite simply, in the automation, check whether sensor becomes unavailable for xx seconds/minutes.

I’ve done this as follows:

trigger: state
entity_id: your.sensor
to: unavailable
for:
hours: 00
minutes : 00
seconds: 15

I believe he wants to trigger when a value is “stuck”.

You should be able to trigger with a template trigger on the last_updated value.

Or the last_reported value, which should be updated even if the state remains the same.

{{ (now() - states['ENTITY_ID_HERE']['last_reported']).total_seconds() > 1800 }}

So with a template trigger:

triggers:
  - trigger: template
    value_template: "{{ (now() - states['ENTITY_ID_HERE']['last_reported']).total_seconds() > 1800 }}"
1 Like

@ wimjanse, the automation is not working. If put the sensor down, de mqtt value is still the same.

@troon Is the “last_reported” value something that I have to make myself?

No, see here:

Here’s an example of the difference:

last_reported shows when the printer was last queried, at xx:37 each hour; the state hasn’t actually changed for a couple of days, but I restarted HA this morning so that is the time reflected in last_updated.

1 Like

So, I have to use last_updated , to know if there is a new value arrived or not.

The automation is running and I have to wait now… to see if it is working.
I’ll be back…

While I’am, waiting, can I see the last_updated value of the sensor in the developer tools?

I had 1000 sec in the template and I think those are already past, nothing happens.

My automation look like:

alias: whatsapp regenput down
description: ""
triggers:
  - trigger: template
    value_template: >-
      "{{ (now() -
      states['sensor.regenwaterput']['last_updated']).total_seconds() > 1000 }}"
conditions: []
actions:
  - action: notify.whatsapp
    metadata: {}
    data:
      message: Regenput sensor werkt niet
mode: single

Check the automation trace to see if it ran and why it stopped if it did run.

Of course — see my screenshot above.

I still think you should be using last_reported or you’ll get false alarms if the reported value doesn’t change between updates.

If I run the automation by hand, by pushing run action then I get the notification.

do I have to add Trigger every minute of every hour in the automation?

No.

Take a look at the attribute value in Developer Tools / Template; also look at the whole template in there.

The trigger will fire when the template changes from false to true.

EDIT: Found the problem — remove the quotes around the template. You only need those around single-line templates like mine in the post above.

alias: whatsapp regenput down
description: ""
triggers:
  - trigger: template
    value_template: >-
      {{ (now() -
      states['sensor.regenwaterput']['last_reported']).total_seconds() > 1000 }}
conditions: []
actions:
  - action: notify.whatsapp
    metadata: {}
    data:
      message: Regenput sensor werkt niet
mode: single

Your version was watching a string that looked like a template but wasn’t evaluating it.

@ baudneo, Where/how can I find a screen to see the last_reported. I don’t find it

You can only see it on Developer Tools / Template. Paste in:

{{ states['sensor.regenwaterput']['last_reported'] }}

I have a strange date/time in the template. I have to look to the sensor program .
It gives a date and time from yesterday.

2025-01-14 12:44:15.230340+00:00

Many thanks to all. Now I can test and look what is happening.

It will only trigger when it is less than 1000 seconds and becomes more than 1000 seconds.

Hi, >I’am back

If I ckeck {{states['sensor.regenwaterput']['last_reported']}} I get the time but with 1 hour difference of the time now();

How is that possible?
In Europe we have winter and summer time
or
is it the difference beteen GMT and brussels time ( there is 1 hour difference)
How to solve? Just add 1 hour or can I add a time zone ?

Now I see if the mqtt value is the same like before, the last_reported or the last_updated time is not changing . It is changed if the value is also changed.

How can I detect then if my sensor is not down? I send every 15 min the value to the mqtt server in HA. Mostly it is the same value. But Last_reported or Last_updated is only changing if the value is changed.