So, I could use a little help. Here’s all the code for my actionable notification, on a Pixel 11 Pro, simple, and it works well:
alias: 'Pill Reminder Rosuvastatin: Daily Notification'
description: Sends an actionable pill reminder at 11:00 PM every night.
triggers:
- trigger: time
at: '23:00:00'
actions:
- action: notify.mobile_app_pixel_11_pro
data:
title: Medication Reminder
message: It is time to take the Rosuvastatin
data:
tag: pill-reminder
actions:
- action: TAKEN
title: Taken
- action: SNOOZE
title: Snooze (10m)
- wait_for_trigger:
- trigger: event
event_type: mobile_app_notification_action
event_data:
action: TAKEN
- trigger: event
event_type: mobile_app_notification_action
event_data:
action: SNOOZE
timeout: 00:30:00
- choose:
- conditions:
- condition: template
value_template: '{{ wait.trigger.event.data.action == ''TAKEN'' }}'
sequence:
- action: notify.mobile_app_pixel_11_pro
data:
message: clear_notification
data:
tag: pill-reminder
- conditions:
- condition: template
value_template: '{{ wait.trigger.event.data.action == ''SNOOZE'' }}'
sequence:
- delay: 00:10:00
- action: notify.mobile_app_pixel_11_pro
data:
message: clear_notification
data:
tag: pill-reminder
- action: automation.trigger
target:
entity_id: automation.pill_reminder_daily_notification
So, I would like to set this up when I scan an NFC tag on the bottle, if it’s before the time for the notification, it will cancel the notification for that day, and if its after the notification, it will ‘complete’ the notification, show it being taken.
I have an NFC tag set up in HA, with ID, and named the tag the medication, and stopped there. Nothing attached to that tag, yet.
Is there a fairly easy way to work this request in?
Thanks!!
Easiest way is a toggle helper. A second automation with a Tag trigger for the bottle turns it on and clears the notification with your pill-reminder tag, and the 23:00 one only sends if it’s off. Then reset it every morning.
I think it’s going to work better if you set up a second automation triggered on the tag scan which fires an Event that matches the one for the wait for trigger.
description: 'Fire "TAKEN" event when tag is scanned'
mode: single
triggers:
- trigger: tag
tag_id: ## YOUR TAG ID ##
conditions: []
actions:
- event: mobile_app_notification_action
event_data:
action: TAKEN
In the existing automation you would add a condition that checks the last_triggered attribute of the new tag-based automation to confirm that it hasn’t been triggered in same day.
alias: 'Pill Reminder Rosuvastatin: Daily Notification'
description: Sends an actionable pill reminder at 11:00 PM every night.
triggers:
- trigger: time
at: '23:00:00'
conditions:
- alias: Check if the bottle tag was already scanned today
condition: template
value_template: |
{% set last = (states('tag.EXAMPLE') | as_datetime).date() %}
{{ now().date() > last }}
actions:
- action: notify.mobile_app_pixel_11_pro
data:
title: Medication Reminder
message: It is time to take the Rosuvastatin
data:
tag: pill-reminder
actions:
- action: TAKEN
title: Taken
- action: SNOOZE
title: Snooze (10m)
- wait_for_trigger:
- trigger: event
event_type: mobile_app_notification_action
event_data:
action: TAKEN
- trigger: event
event_type: mobile_app_notification_action
event_data:
action: SNOOZE
timeout: 00:30:00
- choose:
- conditions:
- condition: template
value_template: '{{ wait.trigger.event.data.action == ''TAKEN'' }}'
sequence:
- action: notify.mobile_app_pixel_11_pro
data:
message: clear_notification
data:
tag: pill-reminder
- conditions:
- condition: template
value_template: '{{ wait.trigger.event.data.action == ''SNOOZE'' }}'
sequence:
- delay: 00:10:00
- action: notify.mobile_app_pixel_11_pro
data:
message: clear_notification
data:
tag: pill-reminder
- action: automation.trigger
target:
entity_id: automation.pill_reminder_daily_notification
FWIW, you may also want to adjust the existing automation by adding an action sequence for the case where the Wait times out.
Nice looking idea! I’ll put that in, Didgeridrew.
I don’t know exactly where I’d put that second section, the conditions one. I’m real good at editing yaml, thanks to HA, not super at creating code, actually.
Estanza, I’ll play with that option too.
It goes in the automation you posted… I’ve updated it in my previous post. Just make sure to change the automation.EXAMPLE to the entity ID of the new tag scan automation.
So I added that in, and scanned the tag at 1030 pm, but the reminder still popped up at 11pm. So it didn’t recognize that it was scanned.
Well, upon further research, I failed to put the tag automation in example, just like you said to do. My fault. I’ll change times and try it again.
That didn’t work. I set the time to notify at 1:19 am, scanned the tag at 1:16, and it still popped up the notification at 1:19am. Hmm.
Double check that the tag ID is correct and that the tag-based automation ran.
Beyond that, you’ll need to take a look at the traces of the automations, or download the json files and share them here so we can see what actually happened.
I don’t know why I didn’t suggest this in the first place… but it would make more sense to use the tag entity’s state rather than the automation’s “last_triggered” timestamp. A tag entity’s state is the datetime string of when it was last scanned which survives restart, so that should be usable as follows:
alias: Check if the bottle tag was already scanned today
condition: template
value_template: |
{% set last = (states('tag.EXAMPLE') | as_datetime | default(as_datetime(0),true)).date() %}
{{ now().date() > last }}
Again, make sure to replace tag.EXAMPLE with the entity ID of your actual tag entity.
So what exactly should be the example in tag.example? I tried the #### id number of the scan automation, that’s throwing errors. would it be the actual alias of the scan?
- id: "1790994391522"
alias: NFC Scan-Rosu pill
description: Fire "TAKEN" event when tag is scanned
......
And will the scan state reset the next day, like Estanza mentioned?
Ok, got it working. Thanks, you guys. Here’s the complete code. 1st, the reminder:
alias: 'Pill Reminder Rosuvastatin: Daily Notification'
description: Sends an actionable pill reminder at 11:00 PM every night.
triggers:
- trigger: time
at: '23:01:00'
conditions:
- alias: Check if the helper date does NOT equal today's date string
condition: template
value_template: >
{% set last = states('input_datetime.last_rosuvastatin_taken') %} {% set
today = now().strftime('%Y-%m-%d') %} {{ last != today }}
actions:
- action: notify.mobile_app_pixel_11_pro
data:
title: Medication Reminder
message: It is time to take Rosuvastatin
data:
tag: pill-reminder
actions:
- action: TAKEN
title: Taken
- action: SNOOZE
title: Snooze (10m)
- wait_for_trigger:
- trigger: event
event_type: mobile_app_notification_action
event_data:
action: TAKEN
- trigger: event
event_type: mobile_app_notification_action
event_data:
action: SNOOZE
timeout: '00:30:00'
- choose:
- conditions:
- condition: template
value_template: >-
{{ wait.trigger is not none and wait.trigger.event.data.action ==
'TAKEN' }}
sequence:
- action: input_datetime.set_datetime
target:
entity_id: input_datetime.last_rosuvastatin_taken
data:
date: '{{ now().strftime(''%Y-%m-%d'') }}'
- action: notify.mobile_app_pixel_11_pro
data:
message: clear_notification
data:
tag: pill-reminder
- conditions:
- condition: template
value_template: >-
{{ wait.trigger is not none and wait.trigger.event.data.action ==
'SNOOZE' }}
sequence:
- action: notify.mobile_app_pixel_11_pro
data:
message: clear_notification
data:
tag: pill-reminder
- delay: '00:10:00'
- action: automation.trigger
target:
entity_id: automation.pill_reminder_daily_notification
data:
skip_condition: true
mode: restart
And then, the tag scan automation:
alias: 'NFC Scan: Rosu Pill'
description: Records the date the pill was taken and clears notifications.
triggers:
- trigger: tag
tag_id: XXXXXXee8ea240-13fXXXXX
actions:
- action: input_datetime.set_datetime
target:
entity_id: input_datetime.last_rosuvastatin_taken ## Helper name
data:
date: '{{ now().strftime(''%Y-%m-%d'') }}'
- event: mobile_app_notification_action
event_data:
action: TAKEN
- action: notify.mobile_app_pixel_11_pro
data:
message: clear_notification
data:
tag: pill-reminder
mode: single
And, the helper I used, is the date only helper. Not date/time. Create a helper, pick Date and or time, then just select the date only, name it whatever you want, but it’ll go into the automation. You can see mine is labeled last-rosuvastatin-taken.
Works well for the past two days, after running it many times, and letting it run as well.
The restart on the reminder is needed to clear things up and start fresh after 11 pm.