Skip to content

Instantly share code, notes, and snippets.

@RoboMagus
Last active February 19, 2024 20:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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: []
@Rustymage
Copy link

Rustymage commented Dec 4, 2023

This is exactly what I need but I have no idea where these go, would you be able to explain where these go please? I'm a fast learner, I think 🤔

@RoboMagus
Copy link
Author

I've got most of my config in different files split up and stored in their respective folders. I.e. all files starting with prefix scripts_ are located in the scripts directory, prefix automations_ in the automations directory, etc..
The structure that I use for template entities is a bit more complicated, so to get that included easily without too much explanation, I'd just copy / paste that snipet directly into your config.

So your with the yaml files located in their directories as described above, your config.yaml should have these lines added:

automation: !include_dir_merge_list automations

script: !include_dir_merge_named scripts

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 }}"

Hope this helps, if not just let me know 😉

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