Reminder automation for replacement medical equipment (e.g. diabetes)

This automation is maybe interesting to any parent that needs to attend his/her kid for recurring medication/medical task. This is the way I did it and I am open to suggestions.

My kid is using an insulin pump and the infusion set needs to be replaced by either father or mother about every 3 days to avoid skin problems. We noticed that either both parents forgot about it or that the kid would not say how long ago it was replaced. (Diabetes is a burden, and replacement is sometimes a bit painful, but definitely a nuisance). The app of his insulin pump does not send out reminders unfortunately.

As technical-nerd parent, I created a HA automation that will send notifications at suitable times to both parents when the replacement of the infusion set is about to be due. It contains actionable buttons to reset and start a new timer.

Step plan of this automation:

  1. Create a date time helper with a suitable name. This contains the date and time that the infusion set was replaced the last time.
  2. Run the official HA app on your phone (Android in my case)
  3. Alter the automation to your needs

How the automation works in general:

Trigger:
At suitable times for you so you have the opportunity to be notified and attend your kid: e.g. 7:00, 18:00, 20:00

Condition:
The infusion set should be older than 64 hours, and therefore be marked for replacement due

Actions:

  • Set some variables for easy handling
  • Fire notifications to phones to inform that replacement is about to be due/past due and wait for response/trigger
  • If “replaced” button was pushed by either father or mother, reset the time date helper to now
  • Dismiss all the notifications after timeout or if action was taken (Prevents old notifications)

Note: run this in “restart” mode to avoid multiple instances.

Below is the code:

alias: Infusieset vervangen
description: Stuurt een reminder dat infusieset moet vervangen
trigger:
  - platform: time
    at: "07:00:00"
  - platform: time
    at: "16:00:00"
  - platform: time
    at: "18:00:00"
  - platform: time
    at: "20:00:00"
condition:
  - condition: template
    value_template: >-
      {{ ((now().timestamp() -
      as_timestamp(states('input_datetime.start_infusieset'))) /3600) |
      round(0) > 64 }}
    alias: Sensor is ouder dan 64 uur (minder dan 8 uur te gaan)
action:
  - alias: Definieer variabelen voor de notification acties
    variables:
      action_option1: "{{ 'OPTION1_' ~ context.id }}"
      action_option2: "{{ 'OPTION2_' ~ context.id }}"
      tremain: >-
        {{ 72 - ((now().timestamp() -
        as_timestamp(states('input_datetime.start_infusieset'))) /3600) }}
      strdatetime: >-
        {{ (as_timestamp(states.input_datetime.start_infusieset.state) +
        259200 ) | timestamp_custom('%d %b %H:%M') }}
      notify_message: >-
        {% if tremain > 0 %}Over {{ tremain | round(0) }} uur vervangen {% elif
        tremain>-1 %}Set is zojuist verlopen {% else %}Sinds {{ -tremain |
        round(0) }} uur geleden verlopen{% endif %} ({{ strdatetime }}).
      notify_title: >-
        {% if tremain > 0 %}Infusieset gauw vervangen!{% else %}Infusieset al
        vervangen?{% endif %}
  - service: notify.mobile_app_telefoon_bart
    data:
      data:
        tag: nieuweinfusieset
      message: clear_notification
    alias: Verwijder notificatie telefoon Bart
  - service: notify.mobile_app_nienke_pixel_6a
    metadata: {}
    data:
      message: clear_notification
      data:
        tag: nieuweinfusieset
    alias: Verwijder notificatie telefoon Nienke
  - service: notify.mobile_app_telefoon_bart
    metadata: {}
    data:
      message: "{{ notify_message }}"
      title: "{{ notify_title }}"
      data:
        tag: nieuweinfusieset
        visibility: public
        icon_url: >-
          https://play-lh.googleusercontent.com/Ui_sCQn24WVnFkV5FwozEA6FYVVRhiC8TfJA-C8AhYmCmyf6XiuXrhgnbaV2YOu5rw
        actions:
          - action: "{{ action_option1 }}"
            title: Nieuwe Set Starten
          - action: "{{ action_option2 }}"
            title: Snooze
  - service: notify.mobile_app_nienke_pixel_6a
    data:
      message: "{{ notify_message }}"
      title: "{{ notify_title }}"
      data:
        tag: nieuweinfusieset
        visibility: public
        icon_url: >-
          https://play-lh.googleusercontent.com/Ui_sCQn24WVnFkV5FwozEA6FYVVRhiC8TfJA-C8AhYmCmyf6XiuXrhgnbaV2YOu5rw
        actions:
          - action: "{{ action_option1 }}"
            title: Nieuwe Set Starten
          - action: "{{ action_option2 }}"
            title: Snooze
  - wait_for_trigger:
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_option1 }}"
        context: {}
      - platform: event
        event_type: mobile_app_notification_action
        event_data:
          action: "{{ action_option2 }}"
        enabled: false
    timeout:
      hours: 1
      minutes: 59
      seconds: 58
      milliseconds: 0
    alias: Wacht op trigger
  - alias: Perform the action
    choose:
      - conditions:
          - condition: template
            value_template: "{{ wait.trigger.event.data.action == action_option1 }}"
        sequence:
          - service: input_datetime.set_datetime
            data:
              timestamp: "{{ now().timestamp() }}"
            target:
              entity_id: input_datetime.start_infusieset
        alias: >-
          Als er een nieuwe infusieset is geplaatst, moet de timestamp worden
          gereset
  - service: notify.mobile_app_nienke_pixel_6a
    metadata: {}
    data:
      message: clear_notification
      data:
        tag: nieuweinfusiesetmelle
    alias: Verwijder notificatie telefoon Nienke
  - service: notify.mobile_app_telefoon_bart
    data:
      data:
        tag: nieuweinfusiesetmelle
      message: clear_notification
    alias: Verwijder notificatie telefoon Bart
mode: restart

Hope this is of help to anyone else. Have fun!