Hi,
I wanted to create an automation which will send me a push notification when one (or more) of the sensors haven’t been updated in 24 hours, indicating need for battery replecament. I’ve tried forcing ChatGPT to give me solution, but none of them is working. (5 minutes instead of 24h for debugging purposes):
alias: Notify when sensors haven't updated for 5 minutes
description: >-
Send a push notification to a mobile device if any sensor in the list hasn't
communicated in 5 minutes.
triggers:
- minutes: /1
trigger: time_pattern
conditions: []
actions:
- variables:
sensors:
- sensor.living_room_temperature
- sensor.office_temperature
- choose:
- conditions:
- condition: template
value_template: >
{% set stale_sensors = sensors | select('is_defined', states.get) | select('is_not', none)
| selectattr('last_updated', 'is_not', none)
| select('lt', (now() - timedelta(minutes=5)).isoformat()) | list %}
{{ stale_sensors | length > 0 }}
sequence:
- device_id: 3a5c03b5761eed7cf87179e98545c1de
domain: mobile_app
type: notify
title: Sensor Battery Alert
message: Please check or replace the batteries.
mode: single
It says:
Error: In ‘template’ condition: TemplateRuntimeError: No test named ‘is_defined’.
ChatGPT also tried using for-loop, but I found out that for loops are extremely limited and that’s probably why it’s not working.
I got the value_template working without loop:
value_template: >
{% set last_updated = states.sensor.temperature_sensor.last_updated %}
{% if last_updated is not none %}
{{ (now() - last_updated).total_seconds() > 86400 }}
{% else %}
false
{% endif %}
Thanks for the answer! Triggers are working! But not trigger.to_state.name:
Error: Error rendering message: UndefinedError: 'dict object' has no attribute 'to_state'
Full code:
alias: Notify when sensors haven't updated for 24 hours
description: >-
Send a push notification to a mobile device if any sensor in the list hasn't
communicated in 24 hours.
triggers:
- trigger: template
value_template: >-
{{
(now()-states['sensor.living_room_temperature'].last_reported).total_seconds()
> 86400 }}
- trigger: template
value_template: >-
{{
(now()-states['sensor.office_temperature'].last_reported).total_seconds()
> 86400 }}
actions:
- device_id: 3a5c03b5761eed7cf87179e98545c1de
domain: mobile_app
type: notify
title: Sensor Battery Alert
message: Please check or replace the batteries. {{ trigger.to_state.name }}
mode: single
Did you try to manually run the automation? That won’t work:
If that’s not the issue, it may be that your device action doesn’t support the trigger variable. Try using the entity-based action in my example instead.
Right, I did trigger it manually indeed. I tried some other times and entities and it seems to be triggering, but {{ trigger.to_state.name }} part is missing. Notification just looks like this: