jcallaghan / home-assistant-config

My Home Assistant configuration & documentation.
https://www.jcallaghan.com/
MIT License
173 stars 8 forks source link

Refactor critical notifications to use alert integration ⚠📲 #257

Closed jcallaghan closed 3 years ago

jcallaghan commented 3 years ago

I use automations to send a number of notifications when an event such as a door or temperature is too low. While this works for one notification making it loop is a little tricky (less so with the new automation) but the alert integration is built for just this use-case. The alert is also persistent is annoying and likely will lead to the event being corrected, particularly when TTS is used.

Example alert

The following example will cause the alert to activate when binary_sensor.fridge_door_contact is on. The alert will activate after 2 minutes (as skip_first is true) and repeat every 5 minutes. This is due to the repeat pattern I've provided.

  kitchen_fridge_door:
    name: 'Fridge door left open'
    entity_id: binary_sensor.fridge_door_contact
    state: 'on'
    repeat:
      - 2
      - 5
    can_acknowledge: true
    skip_first: true
    title: '⚠️Fridge Door'
    message: 'The fridge door has been left open for {{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.fridge_door_contact.last_updated)) | int //60 }} minutes.'
    done_message: 'The fridge door has been closed.'
    notifiers:
      - alert_kitchen
    data:
      apns_headers:
        apns-collapse-id: 'alert_acknowledgement'
      push:
        category: 'alert_acknowledgement'
        sound: 'Alert_WalkieTalkie_Haptic.caf'
      action_data:
        entity_id: alert.kitchen_fridge_door

Automation to refactor

jcallaghan commented 3 years ago

HomeAssistant Mobile App notification documentation - https://companion.home-assistant.io/docs/notifications/notifications-basic

Introduction | Home Assistant Companion Docs
The mobileapp notify platform accepts the standard title, message and target parameters used by the notify platform. The mobile\app notify platform supports targets as services. As long as you granted notifications permissions during setup, you will find all your devices listed as targets for the notify service with names prefixed notify.mobileapp followed by the Device ID of you device. This can be checked in the App Configuration menu of the sidebar and defaults to the name specified in the General>About within the iOS settings app or under About>Phone in Android settings (with spaces and non alphanumeric characters replaced by underscores). A requirement of the notify platform is that you must specify at least message
jcallaghan commented 3 years ago

HomeAssistant Mobile App documentation - https://companion.home-assistant.io/docs/core/core

Feature overview | Home Assistant Companion Docs
The Home Assistant Companion App provides a convenient way to view and control your Home Assistant instance however it also extends the power of your instance by allowing your device to act as a data source. The Home Assistant Companion App adds numerous sensors (such as battery and network status among others), creates a device_tracker entity to allow location updates to be sent from the device and also provides action shortcuts to trigger scripts or automations.
jcallaghan commented 3 years ago

Disable alert via actionable notification

Additional to the alert integration I've also leveraged an actionable alert which allows me to disable the alert and stop it from repeating.

iOS configuration

To achieve this I create a generic notification category in my iOS configuration.

image

ios:
  push:
    categories:
      - name: Alert
        identifier: 'alert_acknowledgement'
        actions:

          - identifier: 'alert_acknowledgement_disable'
            title: 'Disable alert?'
            authenticationRequired: false
            destructive: true
            behavior: 'default'
            activationMode: 'background'

Notification group

Then in my alert group, I provide the alert entity_id in the action data that is sent back to Home Assistant with the actionable alert (category).

platform: group
name: alert_kitchen_fridge_door
services:

  - service: mobile_app_james_iphone
    data_template:
      # title: ''
      # message: ''
      data:
        apns_headers:
          apns-collapse-id: "alert_kitchen_fridge_door"
        push:
          category: 'alert_acknowledgement'
          action_data:
            entity_id: alert.kitchen_fridge_door

Disable alert automation

I then have an automation that is triggered when the ios.notification_action_fired is fired for this actionable alert ALERT_ACKNOWLEDGEMENT_DISABLE.

alias: 'System - Alert acknowledgement action'
id: 'dfaca9d5-fc80-4455-b54d-0b363b721aaa'
description: 'Disables alert via actionable alert.'
initial_state: true
mode: 'single'

trigger:

  # Trigger on actionable alert event.
  - platform: event
    event_type: ios.notification_action_fired
    event_data:
      actionName: ALERT_ACKNOWLEDGEMENT_DISABLE

condition: []

action:

  # Disable the alert.
  - service: alert.turn_off
    data_template:
      entity_id: "{{ trigger.event.data.action_data.entity_id }}"

  # Acknowledge the alert has been disabled.
  - service: notify.mobile_app_james_iphone
    data_template:
      title: '🆘 Alert disabled'
      message: "Alert '{{ trigger.event.data.action_data.entity_id }}' has been disabled."

Screenshots of Alert flow

image

image

image

image

image

jcallaghan commented 3 years ago

Shared on Twitter.

jcallaghan commented 3 years ago

message and done_message

If you add a message and done_message to your alert notify.group configuration then these will be passed to the notification service unless you explicitly define a message with your service.

notify: 
  platform: group
  name: alert_kitchen
  services:

    - service: mobile_app_james_iphone
      data_template:

    - service: html5_notification

    - service: alexa_media
      data:
        target: ["media_player.alexa_kitchen","media_player.alexa_james_desk","media_player.alexa_hall_dot"]
        data:
          type: 'announce'
          method: 'all'
jcallaghan commented 3 years ago

HomeAssistant alert integration documentation https://www.home-assistant.io/integrations/alert

Home Assistant
Alert
Instructions on how to setup automatic alerts within Home Assistant.
jcallaghan commented 3 years ago

Smart notification routing

One consideration with the notify.group is that I cannot see a way to route notifications based on who is home or where there was last activity, or even respecting quite hours. It is likely this is possible with further templating but I've not tried that yet.

jcallaghan commented 3 years ago

Warning Lovelace card

Add alert entities to my warning card on my main Lovelace dashboard #207.

jcallaghan commented 3 years ago

Great examples here https://github.com/scstraus/home-assistant-config/blob/29656bfe3ab7647b5a242665626e66f3b4989add/alerts.yaml

GitHub
scstraus/home-assistant-config
My HomeAssistant Configuration (Home Assistant Supervised, Ubuntu Server 18.04.1 LTS, 2011 Mac Mini) - scstraus/home-assistant-config
jcallaghan commented 3 years ago

Shared more on Twitter.

robbiet480 commented 3 years ago

You should make these notifications critical so that they bypass Do Not Disturb and low volume, in addition to making them appear on CarPlay.

jcallaghan commented 3 years ago

https://twitter.com/Robbie/status/1290734087313874944

Twitter
Robbie Trencheny on Twitter
“@jamescallaghan @home_assistant Now make them critical alerts so they break through DND/volume limits on iOS!”
jcallaghan commented 3 years ago

Examples

GitHub
jamesawebb/homeassistant
Home assistant configuration files. Contribute to jamesawebb/homeassistant development by creating an account on GitHub.
GitHub
rbflurry/Home-Assistant-Config
Contribute to rbflurry/Home-Assistant-Config development by creating an account on GitHub.
GitHub
reefab/homeassistant-config
Contribute to reefab/homeassistant-config development by creating an account on GitHub.
GitHub
honkerst/homeassistant
Contribute to honkerst/homeassistant development by creating an account on GitHub.
jcallaghan commented 3 years ago

Don't forget the really helpful relative_time function that presents the time in a friendly format.

{{ as_timestamp(states.binary_sensor.front_door_contact.last_changed) | timestamp_local }}
{{ relative_time(states.binary_sensor.front_door_contact.last_changed) }}

image

jcallaghan commented 3 years ago

This is truly inspiring @basesnow

image

https://twitter.com/Basesnow/status/1291773453700390912

Twitter
Colin McCormick on Twitter
“@jamescallaghan @home_assistant Love actionable notifications! My HA garage door notification with live stream, snooze options, and cover toggle is light years ahead of the old iOS notification I was getting with HomeKit.”
basesnow commented 3 years ago

Thanks! here is the yaml