Skip to content

Instantly share code, notes, and snippets.

@RoboMagus
Last active October 29, 2024 09:13
Show Gist options
  • Save RoboMagus/70e26758a19a1744dcb51a89674536e1 to your computer and use it in GitHub Desktop.
Save RoboMagus/70e26758a19a1744dcb51a89674536e1 to your computer and use it in GitHub Desktop.
HomeAssistant postponed / pending Notifications
- id: send_pending_notifications
alias: Send pending notifications when arriving home
description: 'With 5 minute delay'
trigger:
- platform: state
entity_id: binary_sensor.someone_at_home
to: 'on'
for: 00:05:00
condition:
# Don't repeat in the next 10 minutes
- '{{ ( as_timestamp(now()) - as_timestamp(state_attr("automation.send_pending_notifications_when_arriving_home", "last_triggered")) |int(0) ) > 600 }}'
# Only when there are pending notifications
- condition: numeric_state
entity_id: sensor.pending_notifications_when_away
above: 0
action:
- alias: "Set variables"
variables:
notifications: "{{ state_attr('sensor.pending_notifications_when_away', 'list') | default([], true) }}"
# 15s delay to allow more than one person to arrive home.
- delay: 15
- repeat:
while: "{{ repeat.index <= notifications|length}}"
sequence:
- alias: "Set variables"
variables:
index: "{{ repeat.index -1 }}"
notification: "{{ notifications[index] }}"
# Use 'script.turn_on' to fire-and-forget, instead of waiting for the result!
- service: script.turn_on
target:
entity_id: script.notify_those_at_home
data:
variables:
title: "{{ notification.title }}"
message: "{{ notification.message }}"
data: "{{ notification.data }}"
notify_when_away: false
# Clear pending notifications
- event: set_entity_value
event_data:
entity_id: "sensor.pending_notifications_when_away"
state: "0"
attr:
list: ""

HomeAssistant postponed / pending Notifications

This is a set of scripts / automations / other snippets for your Home Assistant configuration that enable you to postpone notifications and then have them automatically pushed to your phone when e.g. arriving home.

Some possible use cases for this set of snippets include:

  • Sending any user at home a notification when the laundry is done.
    • If no one is home, it does not make sense to send it out. But as soon as the first person arrives home, they can be notified about it.
  • Sending any user at home a notification to empty the robot vacuum dustbin.
  • A reminder to take out the thrash on pick up day.
  • etc...

Snippet summary

The set of files below has 2 main snippets of interest:

  • Send pending notifications
    • An automation that fires 5 minutes after someone arrives at home. This will fire all pending notifications and clear the queue.
  • Append pending notification
    • A script that takes the same arguments as a notification, but instead of sending it, it stores is in the pending notifications queue.

These are then used by, in my case, a wrapper script:

  • Notify people at home
    • A drop-in replacement for a notify_mobile_<device> service call that will notify any family member at home, or if none are at home, appends the notification to the queue.

Other files include the definition of the variable that stores the pending notifications queue, and a wrapper script for notifying a specific person (me / the wife). This just makes it all so much easier if one of us would get a new phone at some point.

Requirements

  • Home Assistant (duh)
  • At least some knowledge about configuration in YAML
  • A phone to notify
template:
- trigger:
- platform: event
event_type: set_entity_value
event_data:
entity_id: sensor.pending_notifications_when_away
sensor:
- name: Pending notifications when away
unique_id: pending_notifications_when_away
unit_of_measurement: ""
icon: mdi:format-list-group
state: "{{ trigger.event.data.state }}"
attributes:
list: "{{ trigger.event.data.attr.list }}"
# A wrapper script for handling storing of pending notifications
# Has the same interface as regular notification channels and can thus be used as a drop-in replacement!
append_pending_notification:
alias: "Append pending notification"
icon: mdi:notification-clear-all
mode: parallel
max: 10
fields:
title:
name: Title
description: Notification title
required: true
message:
name: Message
description: Notification message
required: true
data:
name: Data
description: Additional notification data
required: false
advanced: true
default: {}
sequence:
- alias: "Set variables"
variables:
appending_notification:
title: "{{ title|default(None, true) }}"
message: "{{ message|default('', true) }}"
data: "{{ data|default({}) }}"
timestamp: "{{ now() }}"
pending_notifications: "{{ (state_attr(pending_notification_entity, 'list') | default([], true)) + [appending_notification]}}"
- event: set_entity_value
event_data:
entity_id: "sensor.pending_notifications_when_away"
state: "{{ pending_notifications|length }}"
attr:
list: "{{ pending_notifications }}"
# A wrapper notification script that is a drop-in replacement for 'notify.mobile_<device>'
# This makes it much easier to adjust the configuration when a family member gets a new phone!
#
notify_me:
alias: "Notify Me"
icon: mdi:notification-clear-all
mode: parallel
max: 10
fields:
title:
name: Title
description: Notification title
required: true
message:
name: Message
description: Notification message
required: true
data:
name: Data
description: Additional notification data
required: false
advanced: true
default: {}
sequence:
- service: notify.mobile_app_s10
data:
title: "{{ title }}"
message: "{{ message }}"
data: "{{ data |default({}) }}"
# Keep track of how many notifications are sent to each device:
# - service: counter.increment
# target:
# entity_id: counter.notification_counter_me
notify_those_at_home:
alias: "Notify people at home"
icon: mdi:notification-clear-all
mode: parallel # Multiple notifications can be sent at the same time!!
max: 10
fields:
title:
name: Title
description: Notification title
required: true
message:
name: Message
description: Notification message
required: true
data:
name: Data
description: Additional notification data
required: false
advanced: true
default:
sequence:
- choose:
# Send to ME if i'm at home
- conditions: "{{ is_state('binary_sensor.me_at_home', 'on') }}"
sequence:
- service: script.notify_me
data:
title: "{{ title }}"
message: "{{ message }}"
data: "{{ data |default({}) }}"
# Notify wife if she's at home
- conditions: "{{ is_state('binary_sensor.wife_at_home', 'on') }}"
sequence:
- service: script.notify_wife
data:
title: "{{ title }}"
message: "{{ message }}"
data: "{{ data |default({}) }}"
# Add similar blocks for each family member...
# ...
# When all from home, append to pending notifications:
- conditions: "{{ is_state('binary_sensor.someone_at_home', 'off') }}"
sequence:
- service: script.append_pending_notification
data:
title: "{{ title }}"
message: "{{ message }}"
data: "{{ data |default({}) }}"
default: []
@Passific
Copy link

Neat! This is perfect! I've been looking for this a long time...
This is a very smart design, and if I read the documentation correctly, the template sensor state should be restored on HA restart, so notifications should not be lost. What a must!

I noticed a small typo: (scripts_append_pending_notification.yaml:32), I think pending_notification_entity is in fact sensor.pending_notifications_when_away
I also had trouble with the sensor without the -, but that was my fault to think it was misaligned.
Thank you for your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment