Files
hass-blueprints/blueprints/automation/medication-reminder.yaml

86 lines
2.4 KiB
YAML

blueprint:
name: Dynamic Medication Reminder via NFC
description: >
Tracks medication intake via NFC scan.
- If no person is selected, the scanner is assumed to be the taker.
- If a person is selected manually (e.g. a child without mobile),
the medication schedule resets for them, but the scanner gets the reminder.
- Sends reminders again after a flexible interval (hours or days).
domain: automation
input:
medication_name:
name: Medication Name
selector:
text:
nfc_tag_id:
name: NFC Tag ID
selector:
text:
reminder_delay:
name: Reminder Interval
selector:
number:
min: 1
max: 168
reminder_unit:
name: Time Unit
selector:
select:
options:
- Hours
- Days
person_selector:
name: input_select for manual person selection
selector:
entity:
domain: input_select
trigger:
- platform: event
event_type: tag_scanned
event_data:
tag_id: "{{ inputs.nfc_tag_id }}"
variables:
scanned_user_id: "{{ trigger.context.user_id }}"
manual_person: "{{ states(inputs.person_selector) | lower }}"
# Determine the "person" whose medication schedule should be reset
person: >
{% if manual_person != "no selection" %}
{{ manual_person }}
{% else %}
{{ state_attr('user.' ~ scanned_user_id, 'friendly_name') | lower }}
{% endif %}
# Calculate the delay in seconds
delay_seconds: >
{% if inputs.reminder_unit == 'Days' %}
{{ (inputs.reminder_delay | int) * 86400 }}
{% else %}
{{ (inputs.reminder_delay | int) * 3600 }}
{% endif %}
datetime_entity: "input_datetime.medication_{{ person }}"
# Determine notification target: always the scanner's device
notify_service: >
{% set scanner_device = "notify.mobile_app_" ~ state_attr('user.' ~ scanned_user_id, 'device_name') | lower %}
{{ scanner_device }}
action:
- service: input_datetime.set_datetime
target:
entity_id: "{{ datetime_entity }}"
data:
timestamp: "{{ now().timestamp() }}"
- delay:
seconds: "{{ delay_seconds }}"
- service: "{{ notify_service }}"
data:
message: "{{ inputs.medication_name }} is due again for {{ person | title }}."
- service: input_select.select_option
target:
entity_id: !input person_selector
data:
option: "No selection"