Advanced medication reminder

Hello,
This is a blueprint for medication reminder.
You set a time as a trigger (or an input_datetime), a mobile_app device to receive the notification and a input_boolean to handle history and notification state.
You will receive a notification on your mobile:

You can check history:

You can customize all sentences, and add a custom action like a speaker notification.

Changelog:

  • 1.0 Initial version
  • 1.1 Add timeout to notify again when no answer
  • 1.2 Fix duplicate notification
  • 1.3 iOS and Android versions
  • 1.4 fix log error when no answer
  • 1.5 fix for broken android notifications

Android

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

iOS

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

see last blueprint yaml file

42 Likes

Would love to see implemented an option to add a second time for those that take medicine more than once a day.

6 Likes

Maybe with that Add input_timetable integration by amitfin · Pull Request #41385 · home-assistant/core · GitHub when it will be merged
For now you can call the blueprint two times, or add an other trigger like this:

trigger:
  - platform: time
    at: !input reminder_time
  - platform: time
    at: !input reminder_time_2

and in input:

input:
    reminder_time:
      name: Reminder time
      description: At what time do you want to be reminded
      selector:
        time: {}
    reminder_time_2:
      name: Second reminder time
      description: At what time do you want to be reminded
      selector:
        time: {}
1 Like

This looks wonderful. Sadly I’m not good enough with blueprints at the moment to adapt it to using a telegram bot. Also I would add a counter for skipped meds and send a notification to anonther person if not taken more than once (or so…)

1 Like

Thank you for a great blueprint.

Is it possible to add a option for getting notified every two days instead of every day?

2 Likes

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.

5 Likes

Thank you for that. That was quick and simple. However is there a way to make it repeat the remind until the medicine has been marked as taken or a set time has passed? Like repeat every 15 minutes for an hour?

3 Likes

Can someone please help me create the input boolean for this? I’d really appreciate it.

yes it’s already in it

from the UI or with yaml : Input Boolean - Home Assistant

you can add a custom action in the blueprint. I use it for tts notify but you can use telegram

you can add a weekday condition : Conditions - Home Assistant

Sorry. I wasn’t clear in what I was asking. Is there a way to change it from being marked as skipped if you don’t respond to just have it automatically repeat if you don’t say remind later?

edit: I updated the blueprint to handle timeout on notification, and ask again

Very nice blueprint! I’m using iOS and I only receive a “simple” notification (no selections for Taken, Ask Later, or Skip). Is there something I need to change?

Thanks!

1 Like

It’s weird because I use a simple action like https://companion.home-assistant.io/docs/notifications/actionable-notifications#building-actionable-notifications
Do you an other blueprint where it works ?

Sorry for not following up earlier. This was a dumb mistake on my part for not realizing that I needed to long-press the notification to see the actions. All the tutorials I had viewed showed the actions appearing on their own - apparently this changed in a recent iOS update.

2 Likes

No problem :smile:

Shouldn’t the input_boolean be reset to off at some time before the next reminder is sent?

It’s the first action, when the automation is started, no need to turn it off as we stay in the loop