I’m trying to do an automation that send me a telegram notify every 24 hours after the toothbrush enter the ‘off’ state after a usage.
This is what I have done:
- id: '1700661861451'
alias: Toothbrush Alert on Inactivity
description: 'If no change in toothbrush status is detected for 24 hours, an alert is sent'
trigger:
platform: state
entity_id: binary_sensor.toothbrush
to: 'off'
for:
hours: 24
action:
- service: counter.increment
entity_id: counter.toothbrush_timer
- service: telegram_bot.send_message
data:
title: 'Casa nonna'
message: 'Toothbrush: no data for {{ states.counter.toothbrush_timer.state | int * 24 | string }} hours'
- id: 'reset_toothbrush_timer'
alias: Reset Toothbrush Timer on State Change
trigger:
platform: state
entity_id: binary_sensor.toothbrush
action:
- condition: state
entity_id: binary_sensor.toothbrush
state: 'on'
- service: counter.reset
entity_id: counter.toothbrush_timer
The problem is that the counter increments only after the first 24 hours with toothbrush in ‘off’ state, so if the toothbrush isn’t used for 48 hours I didn’t receive any notification.
I am doing this to monitor the health of my grandmother who has alzheimer; I brush my teeth regularly and I don’t need a reminder
Do you mean you receive a notification only once, after 24h, and not a second time after 48h? Yes, that’s what that automation does; the “for” will “wait” 24h before triggering once.
If you want a notification every 24h, you’ll probably have to use a timer pattern trigger.
I’d do this by adding a trigger for 24h since the automation triggered, plus a condition that the toothbrush change to off is still more than 24h ago. Then you don’t need the timer.
state_attr('automation.ENTITY_ID', 'last_triggered) shows the last time that the action: was run, not just when the trigger: section fired.
- id: '1700661861451'
alias: Toothbrush Alert on Inactivity
description: 'If no change in toothbrush status is detected for 24 hours, an alert is sent'
trigger:
- platform: state
entity_id: binary_sensor.toothbrush
to: 'off'
for:
hours: 24
- platform: state
entity_id: automation.toothbrush_alert_on_inactivity
attribute: last_triggered
for:
hours: 24
- platform: event
event_type: automation_reloaded
- platform: homeassistant
event: start
condition:
- condition: state
entity_id: binary_sensor.toothbrush
state: 'off'
for:
hours: 24
action:
- service: telegram_bot.send_message
data:
title: 'Casa nonna'
message: >
Toothbrush: no data for
{{ ((now() - states['binary_sensor.toothbrush']['last_changed']).total_seconds() / 3600)|int(0) }} hours
Ah yes, needs a “restart” trigger to get it going. With the two triggers I’ve just added, it works for me — although this means you’ll get a notification whenever you restart or reload automations if the toothbrush is over 24h without use.