Skip to content

Instantly share code, notes, and snippets.

@haberda
Created August 21, 2023 21:09
Show Gist options
  • Save haberda/dca059b766572fe68b05c6d48f608288 to your computer and use it in GitHub Desktop.
Save haberda/dca059b766572fe68b05c6d48f608288 to your computer and use it in GitHub Desktop.
Reminder home assistant package
## Requires MQTT to be configured prior to use
## UI requires custom cards: auto-entities, config-template-card, template-entity-row
automation:
- alias: Reminder - Send next reminder
id: 'Send next reminder'
description: ""
trigger:
- platform: time
at: sensor.next_reminder
condition: []
action:
- repeat:
while:
- condition: template
value_template: "{{ states('sensor.next_reminder') | as_datetime < now()}}"
sequence:
- service: |
{{ 'notify.' + state_attr('sensor.next_reminder','recipient') }}
data:
title: Reminder
message: "{{ state_attr('sensor.next_reminder', 'description') }}"
data:
ttl: 0
priority: high
- service: script.turn_on
data:
variables:
reminder_number: "{{ state_attr('sensor.next_reminder', 'number') }}"
user: "{{ state_attr('sensor.next_reminder', 'user') }}"
target:
entity_id: script.close_reminder
- wait_for_trigger:
- platform: state
entity_id:
- sensor.next_reminder
attribute: number
timeout:
hours: 0
minutes: 0
seconds: 10
milliseconds: 0
continue_on_timeout: false
mode: single
counter:
reminder_number:
name: Reminder Number
initial: 1
minimum: 1
step: 1
icon: mdi:ticket-confirmation
input_select:
filter_reminders:
name: Filter reminders
options:
- All
- Open
- Closed
initial: Open
icon: mdi:filter
reminder_recipient:
name: Reminder recipient
options:
- notify1
- notify2
- persistent_notification
icon: mdi:filter
input_text:
reminder_description:
name: Reminder description
initial: ""
icon: mdi:message-alert
input_datetime:
reminder_tracker:
name: Reminder tracker set time
has_date: true
has_time: true
script:
next_reminder:
alias: Find next reminder
description: Set the next reminder so the automation can send it
variables:
reminder_list: "{{ expand(states.sensor) | selectattr('attributes.send_time','defined') | selectattr('attributes.status','eq','open') | selectattr('entity_id', 'match',
'sensor.reminder_*') | map(attribute='entity_id') |list }}"
sorted_reminder_list: "{{ expand(reminder_list) | sort(attribute='state', reverse=false) | map(attribute='entity_id') |list }}"
reminder: '{{ sorted_reminder_list[0] }}'
sequence:
- condition: template
value_template: "{{ sorted_reminder_list | length > 0 }}"
- alias: Set next reminder
service: mqtt.publish
data_template:
topic: "reminder_tracker/next_reminder"
retain: true
payload: >-
{
"send_time": "{{ state_attr(reminder, 'send_time') }}",
"status": "open",
"description": "{{ state_attr(reminder, 'description') }}",
"time": "{{ utcnow() | as_local }}",
"number": "{{ state_attr(reminder, 'number') }}",
"user": "{{ state_attr(reminder, 'user') }}",
"recipient": "{{ state_attr(reminder, 'recipient') }}"
}
- alias: Create sensor for next reminder
service: mqtt.publish
data_template:
topic: "homeassistant/sensor/reminder_tracker/next_reminder/config"
retain: true
payload: >-
{
"name": "next reminder",
"unique_id": "next_reminder",
"state_topic": "reminder_tracker/next_reminder",
"json_attributes_topic": "reminder_tracker/next_reminder",
"value_template": "{{ '{{ ' + 'value_json.send_time' + ' }}' }}",
"icon": "mdi:bug",
"device": {
"identifiers": ["reminder_tracker"], "name": "reminder Tracker"
},
"device_class": "timestamp"
}
close_reminder:
alias: Close reminder
description: Close an existing reminder.
icon: mdi:close-circle-outline
fields:
reminder_number:
name: reminder Number
description: The reminder number to close
example: 42
selector:
text:
user:
name: User
description: The user that closed this reminder
example: Arthur Dent
default: ""
selector:
text:
variables:
reminder_num: "{{ int(reminder_number) }}"
reminder: "sensor.reminder_{{ reminder_num }}"
time: "{{ utcnow() }}"
sequence:
- alias: Close reminder
service: mqtt.publish
data_template:
topic: "reminder_tracker/{{ reminder_num }}"
retain: true
payload: >-
{
"send_time": "{{ state_attr(reminder, 'send_time') }}",
"status": "closed",
"description": "{{ state_attr(reminder, 'description') }}",
"time": "{{ as_datetime(time) | as_local }}",
"number": "{{ reminder_num }}",
"user": "{{ user }}",
"recipient": "{{ state_attr(reminder, 'recipient') }}"
}
- delay:
hours: 0
minutes: 0
seconds: 1
milliseconds: 0
- service: script.next_reminder
open_reminder:
alias: Open reminder
description: Open a new reminder.
icon: mdi:alert-circle-outline
fields:
reminder_number:
name: reminder Number
description: The reminder number to open
example: 42
default: ""
selector:
text:
reminder_description:
name: reminder Description
description: Short description of the reminder
example: Something's broken!
default: ""
selector:
text:
user:
name: User
description: The user that opened this reminder
example: Arthur Dent
default: ""
selector:
text:
variables:
reminder_num: "{{ reminder_number }}"
reminder_desc: "{{ reminder_description }}"
time: "{{ utcnow() }}"
sequence:
- alias: Skip opening empty reminders
condition: template
value_template: "{{ reminder_desc != '' }}"
- alias: Open reminder
service: mqtt.publish
data_template:
topic: "reminder_tracker/{{ reminder_num }}"
retain: true
payload: >-
{
"send_time": "{{ states('input_datetime.reminder_tracker')| as_datetime | as_local }}",
"status": "open",
"description": "{{ reminder_desc }}",
"time": "{{ as_datetime(time) | as_local }}",
"number": "{{ reminder_num }}",
"user": "{{ user }}",
"recipient": "{{ states('input_select.reminder_recipient') }}"
}
- alias: Create sensor for reminder
service: mqtt.publish
data_template:
topic: "homeassistant/sensor/reminder_tracker/{{ reminder_num }}/config"
retain: true
payload: >-
{
"name": "reminder {{ reminder_num }}",
"unique_id": "reminder_{{ time | as_timestamp }}",
"state_topic": "reminder_tracker/{{ reminder_num }}",
"json_attributes_topic": "reminder_tracker/{{ reminder_num }}",
"value_template": "{{ '{{ ' + 'value_json.send_time' + ' }}' }}",
"icon": "mdi:bug",
"device": {
"identifiers": ["reminder_tracker"], "name": "reminder Tracker"
},
"device_class": "timestamp"
}
- alias: Increment counter
service: counter.increment
target:
entity_id: counter.reminder_number
- alias: Reset description
service: input_text.set_value
target:
entity_id: input_text.reminder_description
data:
value: ""
- delay:
hours: 0
minutes: 0
seconds: 1
milliseconds: 0
- service: script.next_reminder
mqtt:
sensor:
- state_topic: "reminder_tracker/#"
name: reminder_tracker
type: entities
title: Reminders
entities:
- entity: input_text.reminder_description
name: Message
- entity: input_select.reminder_recipient
- entity: input_datetime.reminder_tracker
name: Reminder date and time
- type: custom:config-template-card
entities:
- counter.reminder_number
- input_text.reminder_description
row:
type: button
entity: script.open_reminder
name: ' '
icon: ' '
action_name: Submit
tap_action:
action: call-service
service: script.open_reminder
service_data:
user: ${ user.name }
reminder_number: ${ states['counter.reminder_number'].state }
reminder_description: ${ states['input_text.reminder_description'].state }
- type: divider
- type: custom:auto-entities
card:
type: entities
show_header_toggle: false
show_empty: false
filter:
template: >
{% set reminders = expand(states.sensor) |
selectattr('attributes.send_time','defined') |
selectattr('attributes.status','eq', 'open') | selectattr('entity_id',
'match', 'sensor.reminder_*') | map(attribute='entity_id') |list %}
{% set reminders = reminders | map('regex_replace', '^.+_(\\d+)$',
'\\1') | map('int') | sort | map('string') | map('regex_replace',
'^(\\d+)$', 'sensor.reminder_\\1') | list %}
[ {% for reminder in (reminders | reverse) %}
{
"type": "custom:config-template-card",
"entities": "[{{ reminder }}]",
"row": {
"type": "custom:template-entity-row",
"entity": "{{ reminder }}",
"name": "{{ state_attr(reminder, 'description') }}",
"active": {{ is_state_attr(reminder,'attributes.status','open') }},
"icon": "{{ "mdi:alert-circle-outline" if is_state_attr(reminder,'attributes.status','open') else "mdi:close-circle-outline" }}",
"state": "{{ state_attr(reminder, 'user') }}",
"secondary": "{{ '#' + state_attr(reminder, 'number') + ' - ' }} Will send on {{
float(state_attr(reminder, 'send_time') | as_timestamp) |
timestamp_custom('%m/%d/%y at %-0I:%M %p') }}",
"hold_action":
{
"action": "call-service",
"service": "script.close_reminder",
"service_data": {
"reminder_number": {{ state_attr(reminder, "number") }},
"user": "${ user.name }",
},
"confirmation": {
"text": "Dismiss reminder #{{ state_attr(reminder, 'number') }}?",
}
}
}
},
{% endfor %} ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment