I have an automation that checks whether several sensors have been updated recently. If one of the sensors has not been updated, I want the message to identify the specific sensor that triggered the automation.
The automation is of form:
alias: No Recent Sensor Update
trigger:
- platform: template
value_template:
"{{ ((as_timestamp(now()) - as_timestamp(states.sensor.ambientweather_f007th_1_15_temperature.last_updated))
/ 60) > 30 }}"
...
...
- platform: template
value_template:
"{{ (as_timestamp(now()) - as_timestamp(states.sensor.ambientweather_f007th_2_17_temperature.last_updated))
> 30 }}"
condition: []
action:
- service: notify.notify
metadata: {}
data:
title: Sensor Offline/Unavailable for >30 minutes
message: "{{ trigger.entity_id }}"
mode: single
This gives an error of form:
AttributeError: 'NoneType' object has no attribute 'lower'
which I believe is due to the fact that trigger.entity_id
doesn’t exist.
So how do I tell which of the sensors triggered the automation when the sensor is embedded in a value_template?
(I know there are kludgey ways where I could recheck the conditions in the ‘action’ statement to see which one matches but I would like to avoid that)
Troon
(Troon)
March 28, 2024, 2:18pm
2
You can’t without re-evaluating. Much easier way:
trigger:
- platform: state
entity_id:
- sensor.ambientweather_f007th_1_15_temperature
- sensor.ambientweather_f007th_2_17_temperature
for: "00:30:00"
Then you can use trigger.entity_id
.
Slight nuance: this will trigger if the state hasn’t changed for 30 minutes. Your template version also watches the sensor attributes because you’re using last_updated
rather than last_changed
(ref ).
An alternative would be to use trigger ID s:
trigger:
- platform: template
id: sensor.ambientweather_f007th_1_15_temperature
value_template:
"{{ ((as_timestamp(now()) - as_timestamp(states['sensor.ambientweather_f007th_1_15_temperature']['last_updated'])) / 60) > 30 }}"
...
...
- platform: template
id: sensor.ambientweather_f007th_2_17_temperature
value_template:
"{{ (as_timestamp(now()) - as_timestamp(states['sensor.ambientweather_f007th_2_17_temperature']['last_updated'])) > 30 }}"
Then trigger.id
is the entity ID.
Your second trigger is only waiting 30s not 30m, by the way.
Thanks.
To save a little typing, is it possible to reference the name of the sensor using the trigger.id
variable within the value_template. Or said another way this would allow me to type the name of the sensor only once. Something like:
trigger:
- platform: template
id: sensor.ambientweather_f007th_1_15_temperature
value_template:
"{{ ((as_timestamp(now()) - as_timestamp(states('trigger.id').last_updated)) / 60) > 30 }}"
...
...
- platform: template
id: sensor.ambientweather_f007th_2_17_temperature
value_template:
"{{ (as_timestamp(now()) - "{{ ((as_timestamp(now()) - as_timestamp(states('trigger.id').last_updated)) / 60) > 60 }}"
I am asking because it doesn’t work for me so perhaps I just have the syntax wrong.