I found this blueprint after checking out some other reminders which were not working or maintained anymore. This approach is great!
However, I missed the opportunity to have a reminder sent once every week or every second week on a specific weekday.
This I have tried to add. A few tests seem proving. However, I’m not an expert yet in yaml and HA-scripting, thus my code may have room for optimization:
blueprint:
name: Advanced medication reminder
description: Get advanced notification and history for medication reminder
domain: automation
input:
reminder_time:
name: Reminder time
description: At what time do you want to be reminded
selector:
time: {}
frequency:
name: Reminder frequency
description: How often do you want to be reminded
selector:
select:
options:
- Daily
- Weekly
- Odd weeks
- Even weeks
weekday:
name: Reminder weekday
description: If weekly, on which day do you want to be reminded
selector:
select:
options:
- Monday
- Tuesday
- Wednesday
- Thursday
- Friday
- Saturday
- Sunday
pre_condition:
name: Optional conditions before reminder
description: For example skip reminder, if tag has been scanned earlier today
selector:
action: {}
default: []
notify_device:
name: Notification
description: Device needs to run the official Home Assistant app to receive
notifications
selector:
device:
integration: mobile_app
input_boolean:
name: Dedicated input_boolean
description: Create and set here a input_boolean to handle history and state
of the automation
selector:
entity:
domain: input_boolean
notification_title:
name: Notification title (Optional)
description: 'Default: Medication reminder'
default: Medication reminder!
notification_message:
name: Notification message (Optional)
description: 'Default: It''s time to take your medication'
default: It's time to take your medication
notification_action_taken:
name: 'Notification action: Taken (Optional)'
description: 'Default: Taken'
default: Taken
notification_action_later:
name: 'Notification action: Ask later (Optional)'
description: 'Default: Ask later'
default: Ask later
notification_action_skip:
name: 'Notification action: Skip (Optional)'
description: 'Default: Skip'
default: Skip
ask_later_wait_time:
name: Wait time before next reminder
description: Minutes before notify again after a Ask later action.
default: 30
selector:
number:
min: 5.0
max: 1440.0
unit_of_measurement: minutes
step: 1.0
mode: slider
logbook_message_remind:
name: Logbook message for remind (Optional)
description: 'Default: Reminder sent'
default: Reminder sent
logbook_message_taken:
name: Logbook message for action Taken (Optional)
description: 'Default: Medication taken'
default: Medication taken
logbook_message_later:
name: Logbook message for action Ask later (Optional)
description: 'Default: Postpone reminder'
default: Postpone reminder
logbook_message_skip:
name: Logbook message for action Skip (Optional)
description: 'Default: Reminder skipped'
default: Reminder skipped
optional_action:
name: Optional action
description: Run an action like notify a speaker at the same time that the mobile
notification
selector:
action: {}
default: []
source_url: https://community.home-assistant.io/t/advanced-medication-reminder/300137
variables:
frequency: !input 'frequency'
weekday: !input 'weekday'
weekmap:
"Monday": 1
"Tuesday": 2
"Wednesday": 3
"Thursday": 4
"Friday": 5
"Saturday": 6
"Sunday": 7
trigger:
- platform: time
at: !input 'reminder_time'
mode: restart
condition:
- condition: template
value_template: >
{% set day_index = weekmap[weekday] if weekday in weekmap %}
{% if frequency == 'Daily' -%}
{{ true }}
{% elif frequency == 'Weekly' -%}
{{ as_timestamp(now())|timestamp_custom ('%u') | int == day_index }}
{% elif frequency == 'Even weeks' -%}
{{ ((as_timestamp(now())|timestamp_custom ('%W') | int % 2) == 0) and
(as_timestamp(now())|timestamp_custom ('%u') | int == day_index) }}
{% elif frequency == 'Odd weeks' -%}
{{ ((as_timestamp(now())|timestamp_custom ('%W') | int % 2) == 1) and
(as_timestamp(now())|timestamp_custom ('%u') | int == day_index) }}
{%- endif %}
- condition: and
conditions: !input 'pre_condition'
action:
- service: input_boolean.turn_off
target:
entity_id: !input 'input_boolean'
- alias: Notify until the medication has been take
repeat:
while:
- condition: state
entity_id: !input 'input_boolean'
state: 'off'
sequence:
- service: logbook.log
data:
name: !input 'notification_title'
message: !input 'logbook_message_remind'
entity_id: !input 'input_boolean'
- choose:
- conditions: '{{ true }}'
sequence: !input 'optional_action'
- device_id: !input 'notify_device'
domain: mobile_app
type: notify
title: !input 'notification_title'
message: !input 'notification_message'
data:
actions:
- title: !input 'notification_action_taken'
action: taken
- title: !input 'notification_action_later'
action: later
- title: !input 'notification_action_skip'
action: skip
- wait_for_trigger:
platform: event
event_type: mobile_app_notification_action
- choose:
- conditions: '{{ wait.trigger.event.data.action == ''taken'' }}'
sequence:
- service: input_boolean.turn_on
target:
entity_id: !input 'input_boolean'
- service: logbook.log
data:
name: !input 'notification_title'
message: !input 'logbook_message_taken'
entity_id: !input 'input_boolean'
- conditions: '{{ wait.trigger.event.data.action == ''later'' }}'
sequence:
- service: logbook.log
data:
name: !input 'notification_title'
message: !input 'logbook_message_later'
entity_id: !input 'input_boolean'
- delay:
minutes: !input 'ask_later_wait_time'
default:
- service: input_boolean.turn_on
target:
entity_id: !input 'input_boolean'
- service: logbook.log
data:
name: !input 'notification_title'
message: !input 'logbook_message_skip'
entity_id: !input 'input_boolean'
Basically, I have added three more inputs, some variables and a condition block after the trigger code.
The first two inputs are for choosing if the reminder is to be sent daily, weekly or in odd/even weeks. If a (bi)weekly reminder is chosen, a weekday may be selected next.
The third input pre_condition
is meant to skip an automation run-though from another source, eg. if a tag has been scanned.
I plan to setup a condition like this:
{{ (as_timestamp(now()) - as_timestamp(states.automation.tag_colorcard_blue.attributes.last_triggered | default(0)) | int > 86400) }}
This would skip the reminder from running at the selected day/time if a NFC tag was scanned within the previous 24 hours.
It should be noted, that my addition is assuming Monday as the first day in the week. It is trivial to alter the code for Sunday first.