Hi,
I have a topic that received some values for each 10s.
How can I write automation that reads the last message received time and compare with the current to get how long the time has passed?
My idea is to generate an alarm (telegram message) when a topic hasn’t received a message in the past 30m.
Assuming the message actually updated the entity, the entity has a lastchanged/updated and you could compare this to ‘now’?
You might want to consider using the expire_after
option.
Using expire_after
, if the sensor receives no updates after 30 minutes, its state changes to unavailable
. You can make a simple automation, with a State Trigger, that sends you a notification when the sensor’s state changes to unavailable
.
something like that?
alias: Grow tend - Alarm when no reading was received
description: ''
trigger:
- platform: state
entity_id: sensor.ph
attribute: expire_after
to: unavailable
condition: []
action:
- service: telegram_bot.send_message
data:
message: 🚨 ATTENTION!! 🚨 NO NEW MESSAGES WERE RECEIVED IN THE LAST 30s!!
target: my_target
mode: single
No, expire_after
is a configuration option for an MQTT Sensor (as shown in the documentation). Ensure you add it to the sensor’s configuration and then remove attribute: expire_after
from your automation’s State Trigger.
It’s there:
sensor:
- platform: mqtt
state_topic: 'sensors/hydroponics-kit/ph'
name: 'PH'
value_template: "{{ value_json }}"
unit_of_measurement: 'PH'
expire_after: 30
So the value of this sensor will become unavailable
that’s right?
The time unit for expire_after
is seconds. If you want it to expire after 30 minutes you need to set it to 1800
which is 30 x 60.
- platform: mqtt
state_topic: 'sensors/hydroponics-kit/ph'
name: 'PH'
value_template: "{{ value_json }}"
unit_of_measurement: 'PH'
expire_after: 1800
The automation triggers when the sensor’s state
value changes to unavailable
.
alias: Grow tend - Alarm when no reading was received
description: ''
trigger:
- platform: state
entity_id: sensor.ph
to: unavailable
condition: []
action:
- service: telegram_bot.send_message
data:
message: 🚨 ATTENTION!! 🚨 NO NEW MESSAGES WERE RECEIVED IN THE LAST 30s!!
target: my_target
mode: single
Thank you very much!
- platform: template
name: User1 weight
id: weight_user1
unit_of_measurement: 'kg'
icon: mdi:weight-kilogram
accuracy_decimals: 2
What if my data is coming from ESPHome? Basically I want an alert if no data for last 3 hrs etc
This 2 year-old topic was originally about an MQTT Sensor which offers an expire_after
option. I am much less familiar with the configuration possibilities of an ESPHome device so you can either look through its documentation for something equivalent to expire_after
or wait for someone else (more experienced with ESPHome) to answer your question.