Advanced medication reminder

Yes, you choose Condition (Bedingung in your screenshot), then choose Template as the type.

Then put in this code:

{{ (as_timestamp(now()) - as_timestamp(state_attr("automation.tag_medication",   "last_triggered") | default(0)) | int > 86400) }}

Save the automation and set up a tag with name ‘medication’ or some thing else.

Create a separate automation with name ‘Tag medication’ (the entity name must match what you put in the code above). This automation should trigger when the tag is scanned but otherwise do nothing in conditin and action.

The need for this second automation is to reliably source when a tag is scanned, as this event is not saved elsewhere AFAIK.

Then after reloading, your can prevent the reminder to be sent to our phone, if you scanned the tag within the last 24 hours. This of course can be set to another time frame by changing the value 86400.

I use 17:00 (5 pm) as reminder time and thus have 61200 in the pre-condition (60 * 60 * 17).

Further improvement of the code not done would be to log the scanning of the tag as a ‘Medication taken’. Also after the automation has begun its run, scanning of the tag will, as of now, not stop the reminder loop.

fix for android notification broken Fix for broken android notifications by ryanneufeld · Pull Request #8 · Aohzan/hass-blueprints · GitHub

Is it possible to notify more than one device?

my partner and I (and sometimes a neighbour) have to coordinate administering medications to someone in our care. We want to make sure that everyone is up to date and that the drugs don’t get administered twice.

you can use the optional_action parameter (I couldn’t handle two device to answer)

ah, bummer - it would be perfect if any of the recipients could update the status.

As a workaround: would it achieve the same if I provide the users a button that sets the helper boolean back to false? Or to true - not sure now.

Thanks for the Blueprint! It seems to be working well for me.
One question that I have is I’d like to be able to add a calendar entry at the time that I press Taken.
In my blueprint I added an action to create a calendar entry. However I’m a little lost as to how tell it to use the time that I pressed Taken as the start of the calendar entry.
I’m sure there is a variable that I can use, I’m not sure how to go about finding it.

Thanks so much!

Here’s Automation so far

alias: Morgan's Morning Med Reminder
description: ""
use_blueprint:
  path: Aohzan/medication_reminder_android.yaml
  input:
    reminder_time: "07:00:00"
    notify_device: 90f00fa94658684ef6cb540b825734fd
    input_boolean: input_boolean.morgan_meds
    notification_title: Morning Medication Reminder
    notification_message: It's time to take your morning medications.
    notification_action_later: Delay 30 Minutes
    optional_action:
      - if:
          - condition: state
            entity_id: input_boolean.morgan_meds
            state: "on"
        then:
          - service: google.create_event
            data:
              summary: Morning Meds Taken
              description: Morning Meds Taken
              start_date_time: ?????
              end_date_time: ????? +1 hour
            target:
              entity_id: calendar.morgan_c_simmons

the best solution would be to edit the blueprint on your side, and add your event creation after the line 153: hass-blueprints/medication_reminder_android.yaml at 87209ae2617b8523e5779a02214a19a946252ed6 · Aohzan/hass-blueprints · GitHub

First of all thank you for this great blueprint! I started using it recently and it works great. What I was missing though is a way of confirming the reminder (medication taken) without my phone. I have a small Zigbee button next to it and wanted a click to also confirm.

This is what I added:
new input

  confirm_button:
      name: Confirm button
      description: Physical button to confirm the task is done
      selector:
        device:
          integration: zha
          multiple: false

and then a new entry in the wait_for_trigger

- platform: event
          event_type: zha_event
          event_data:
            device_id: !input confirm_button

and finally a copy of the “taken” path which confirms on a click and also clears the notification

- conditions: '{{ wait.trigger and wait.trigger.event.data.command and wait.trigger.event.data.command == ''single''
          }}'
        sequence:
        - service: input_boolean.turn_on
          target:
            entity_id: !input input_boolean
        - device_id: !input notify_device
          domain: mobile_app
          type: notify
          title: !input notification_title
          message: "clear_notification"
          data:
            tag: !input input_boolean
        - service: logbook.log
          data:
            name: !input notification_title
            message: !input logbook_message_taken
            entity_id: !input input_boolean

Sorry the indentation got screwed up a bit here because of copy & paste but it works for me. What do you think about this? Anything that can be improved? If someone thinks its a valid improvement I can create a PR for the original blueprint

All the changes and the full code can be found on Github here, in case someone else is looking for similar updates .

dear all,

I adapted this great blueprint to make it able to use calendar entries. i setup a special medication calendar. which this blueprint searches as soon as an item triggers in this calendar on a keyword. Also the title and message can be derived from the calendar item. See below the blueprint code;

blueprint:
  name: Advanced medication reminder calendar (iOS)
  description: Get advanced notification and history for medication reminder via calendar
  domain: automation
  input:
    reminder_calendar:
      name: Calendar
      description: Which calendar to use
      default: calendar.
      selector:
        text: {}
    calendar_keyword:
      name: Keyword
      description: Calendar keyword to look for
      selector:
        text: {}
    notify_device:
      name: Notification
      description: Device needs to run the official Home Assistant app to receive
        notifications
      selector:
        device:
          integration: mobile_app
          multiple: false
    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
          multiple: false
    notification_title:
      name: Notification title (Optional)
      description: 'Default: Medication reminder'
      default: "{{ trigger.calendar_event.summary }}"
    notification_message:
      name: Notification message (Optional)
      description: "Default: calendar description"
      default: "{{ trigger.calendar_event.description }}"
    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
    logbook_message_no_answer:
      name: Logbook message for no action after timeout (Optional)
      description: 'Default: No answer time out'
      default: No answer time out
    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://raw.githubusercontent.com/Aohzan/hass-blueprints/main/blueprints/medication_reminder_ios.yaml

variables:
  kw: !input calendar_keyword

trigger:
  - platform: calendar
    event: start
    offset: "00:00:00"
    entity_id: !input reminder_calendar
condition:
  - condition: template
    value_template: "{{ kw in trigger.calendar_event.summary }}"
mode: restart
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
        tag: !input input_boolean
    - wait_for_trigger:
        platform: event
        event_type: ios.notification_action_fired
        event_data: {}
      timeout:
        minutes: !input ask_later_wait_time
    - choose:
      - conditions: '{{ wait.trigger and wait.trigger.event.data.actionName == ''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 and wait.trigger.event.data.actionName == ''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
      - conditions: '{{ wait.trigger and wait.trigger.event.data.actionName == ''skip''
          }}'
        sequence:
        - 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
      default:
      - device_id: !input notify_device
        domain: mobile_app
        type: notify
        message: clear_notification
        data:
          tag: !input input_boolean
      - service: logbook.log
        data:
          name: !input notification_title
          message: !input logbook_message_no_answer
          entity_id: !input input_boolean

2 Likes

Testing it out starting today. Loops promising!

Especially the remind me later option! (sometimes i’m at work and i take my meds a bit later)

Will post more updates in a few days :slight_smile:

I did figure out how to get it to work, thanks to your suggestion. It may be a bit messy, but I actually created a modified blueprint for myself, and a seperate one for my spouse. Here is the useful snippets of code:

    - wait_for_trigger:
        platform: event
        event_type: mobile_app_notification_action
        event_data:
          tag: !input input_boolean
      timeout:
        minutes: !input ask_later_wait_time
    - choose:
      - conditions: '{{ wait.trigger and 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
        - service: google.create_event
          data:
            summary: !input logbook_message_taken
            description: !input logbook_message_taken
            start_date_time: "{{ now() }}"
            end_date_time: "{{ now() + timedelta(minutes=30) }}"
          target:
            entity_id: calendar.mycalender          

It works great out of the box! thanks!.

I have one small remark though. It keeps track of medication use. But you can’t really do much with this data because the boolean data is just off or on. Sure you can look at the logbook and see what the latest status is. But it would be nice to have the last triggered state as a separate variable state too.

This so you can create nice logs with it instead of just On or Off.

You can add a lovelace entity card with the last updated state

Hello everybody,
here I am again, I would like to ask how to implement the following function:
I would like to count the remaining pills in the box, I created a boolean input where I set a minimum value of 0 and a maximum of 12 (number of pills) and every time the drug alert notification arrives, when I press on taken, it subtracts 1 from me variable, and when there are 2 pills left, it sends me a notification to buy more, who can help me with the realization of this modification, I would be really grateful. thank you

with the optional action, you can call the service to call the service input_number.decrement to decrement the input_number, after that do an automation to send an alert with the wanted threshold

1 Like

Thanks for the answer, I did some tests but I can’t figure it out, could you help me again? thank you


hello, I made several attempts, so it could work? thank you

1 Like

hello, I think I almost succeeded but I have a problem with input boolean because it turns off when I press on notification so it doesn’t decrement

‘alias: Advanced medication reminder (Android)
description: “”
use_blueprint:
path: Aohzan/medication_reminder_android.yaml
input:
input_boolean: input_boolean.farmaco
reminder_time: “20:20:00”
notify_device: 8cf9bac8590f6384f
ask_later_wait_time: 1
optional_action:
- if:
- condition: state
entity_id: input_boolean.farmaco
state: “on”
then:
- service: counter.decrement
data: {}
target:
entity_id: counter.quantitafarmaco
- if:
- condition: numeric_state
entity_id: counter.quantitafarmaco
below: 1
then:
- service: counter.reset
data: {}
target:
entity_id: counter.quantitafarmaco’

maybe you can juste create a side automation, triggered by the input_boolean to state true, that will decrement the counter ?

1 Like