- alias: Bedroom temperature sensor has old data
trigger:
platform: template
value_template: '{{ ((as_timestamp(now()) - as_timestamp(states.sensor.soverom_temperature.last_updated)) / 60) > 5 }}'
action:
- service: notify.pushover
data:
title: "Sensor warning"
message: "Temperaturmåling på soverommet er {{ ((as_timestamp(now()) - as_timestamp(states.sensor.soverom_temperature.last_updated)) / 60) |int }} minutter gammel"
If I put '{{ ((as_timestamp(now()) - as_timestamp(states.sensor.soverom_temperature.last_updated)) / 60) > 5 }}'in the dev template editor, it says True. (It said False before I pulled the batteries from the thermometer.)
it won’t work because now() does not create a listener for home assistant to react upon. That trigger will only fire when sensor.soverom_temperature updates. It will not fire when the time changes.
You need to move that to a condition with a trigger that fires every minute or come up with another approach to the template using an entity_id that updates every minute.
Ok, then it’s not the trigger that wrong it’s the condition, I would put it like you have it in the test template version i.e. states.sensor.soverom_temperature.last_updated
value_template for numeric_states is ment to pull out the value, not return a true/false. You’re using it wrong. I understand what you want as well and unfortunately, it’s not possible unless you make a template sensor and trigger off that.
then do this trigger. (you won’t need to math it out now).
- alias: Bedroom temperature sensor has old data
trigger:
- platform: state
entity_id: binary_sensor.soverom_temperature
state: 'on'
for:
minute: 1
action:
- service: notify.pushover
data:
title: "Sensor warning"
message: "Temperaturmåling på soverommet er 1 minutter gammel"
This will only fire once and never again until it drops below 5 and then above for another minute.
If you want it to fire continuously:
- alias: Bedroom temperature sensor has old data
trigger:
- platform: time
minutes: '\1'
seconds: 00 #this is needed
condition:
- condition: template
value_template: >
{% if is_state('binary_sensor.soverom_temperature','on') %}
{% if states.binary_sensor.soverom_temperature.last_updated is defined %}
{{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.soverom_temperature.last_updated)) / 60 }}
{% else %}
False
{% endif %}
{% else %}
False
{% endif %}
action:
- service: notify.pushover
data:
title: "Sensor warning"
message: "Temperaturmåling på soverommet er {{ ((as_timestamp(now()) - as_timestamp(states.binary_sensor.soverom_temperature.last_updated)) / 60) | int }} minutter gammel"
EDIT: Editing this because I had a miss-understanding on the original intent. For anyone that stumbles upon this, this will check an verify that the temperature is above 5. If it has been above 5 for 1 minute, it will send the message. OP is just trying to find out if the sensor has changed in the last five minutes.
That returns the state from the sensor. There are multiple ways to grab a state.
anyways, the error about the state is because I made a typo. this should be the automation:
- alias: Bedroom temperature sensor has old data
trigger:
- platform: state
entity_id: binary_sensor.soverom_temperature
to: 'on'
for:
minute: 1
action:
- service: notify.pushover
data:
title: "Sensor warning"
message: "Temperaturmåling på soverommet er 1 minutter gammel"
That’s what I thought, but I’m not interested in the state/temperature, as you see in the original posting. I’m interested in detecting if a sensor doesn’t send data anymore (e.g. when the battery is dead). So I’m not sure why I would need to check if states('sensor.soverom_temperature') is larger than 5?
I still don’t think you understand what is happening here. That’s the template sensor. All that template sensor is doing is creating a binary_sensor that will be true or false if the value is above 5.
condition:
condition: numeric_state
entity_id: sensor.temperature
above: 17
below: 25
# If your sensor value needs to be adjusted
value_template: {{ float(state.state) + 2 }}
The problem is that I use now(), not the numeric_state as such.