Home Assistant Alerts

This is a written walk through of my recent Alerts video, so if you haven’t checked that out, you can find it over on Youtube at 

Alerts in Home Assistant are simply prebuilt automations that can deliver notifications. And they allow you to quickly build an automation that triggers on a condition, and then can send notifications at either dynamic or static intervals until that condition has cleared. They can also send a message that the condition has cleared so you will know when the condition is no longer an issue.

The only issue with Alerts is they cannot be setup via the UI as some Assistant version 2022.03, so we are going to need to YAML it. These days I like to use the Studio Code Server Add-on for editing the YAML, but you can choose a different method. The only requirement is that you have the ability to edit your Home Assistant YAML files. 

If you want to download the files you can grab a zip  

Or you can find them over on GitHub at https://github.com/thejeffreystone/SlackerLabVideoExamples/tree/main/2022-04-Home_Assistant_Alerts

Enabling Alerts

First we need to enable the Alerts by making a small change to our configuration.yaml file. You can do this one of two ways. 

First you can add a single file to how’s all your alerts but adding this to your configuration.yaml file:

alert: !include alerts.yaml

That requires that you create a file named alerts.yaml as well. 

Or you could define your alerts right in your configuration.yaml:

Like this:

    name: Garage Open 
    message: Someone left the garage open. You Should Close it.
    done_message: Garage is closed
    entity_id: binary_sensor.garage_door
    state: "on"
    repeat: 5 # This will repeat every 5 minutes
    can_acknowledge: false
    skip_first: False
      - mobile_app_jeffreys_iphone_8
	name: ...

Either way will work, so its up to you. As for what kind of alerts you want to add, lets look at some examples: 

Alerting the Good Stuff

A basic alert looks like this:

  name: Garage Open After Dark
  message: Someone left the garage open. You Should Close it.
  done_message: Garage is closed
  entity_id: binary_sensor.garage_door
  state: "on"
  repeat: 5 # This will repeat every 5 minutes
  can_acknowledge: false
  skip_first: False
    - mobile_app_jeffreys_iphone_8

The first line is the entity_id of the alert. Then since its YAML we have to indent the next lines. 

Name is the name you would see in your Lovelace dashboard. 

Message is the message you want the alert to send out. 

done_message is optional and if included when the condition clears it will the be message you get. 

Every alert should be monitoring a single entity looking for a specific state. So in this case entity_id is the name of the entity you want this alert to monitor and stateunder it is the state to watch for. So in this case when the binary sensor monitoring the garage door is on, this alert will trigger. 

Repeat is the number of minutes you want this notification to repeat until cleared. And this could be constant like this, meaning every 5 minutes send an notification. Or it could be dynamic where you have multiple options like

  - 5
  - 10
  - 15

Which means that notifications would go out after 5 minutes, then 10 minutes after that, and then 15 minutes after that. And once it hits that last 15 it will repeat every 15 minutes. 

can_acknowledge is supposed indicate that you can silence the notifications even when the condition hasn’t cleared, but I have yet to see how to make that happen. 

skip_first allows you to skip the first notification which would come as soon as the alert triggers. 

And lastly notifiers is a list of notifications services you want to send the alert too. This can be a list as well, but it does have to be notification services. 

Alerts Limitations

One of the limitations of alerts though is you cannot monitoring multiple conditions like, the Sun is down and the garage is open. In those cases you would need to create a template sensor or or use something like an automation and an input_boolean which is my typical go to pattern.

So you could create an input boolean called garage_open_after_dark and then use an automation to determine when to turn that input boolean on. 

For example, an automation like this:

- id: close_garage_lights_out
  alias: Close Garage at lights out
  initial_state: true
    - platform: time
      at: "22:30:00"
    - condition: state
      entity_id: binary_sensor.garage_door
      state: "on"
    - service: input_boolean.turn_on
      entity_id: input_boolean.garage_after_dark

This automation would go in your automations.yaml file, and triggers at 10:30 pm each night. And we use a condition to check if the garage is open. If it is then we turn on our input boolean. 

Then we can use that input boolean as the entity our alert monitors. 

  name: Garage Open After Dark
  message: >
    {{ [
        'I have noticed the garage is still open.', 
        'Why is the garage still open?',
        'Someone forgot to close the garage.'
        ] | random }}
    {{ [
        'The night is dark and full of terrors, so closing it seems like a good option.', 
        'Were you waiting for me to close it?',
        'I am not sure we can fit anymore moths in there.',
        'Stop what you are doing and close it., Please.'
        ] | random }}

  done_message: Garage is closed
  entity_id: input_boolean.garage_after_dark
  state: "on"
  repeat: 5
  can_acknowledge: false
  skip_first: False
    - mobile_app_jeffreys_iphone_8

In this case I have added some snark to the message and some jinja to randomized the notification. 

But you could just use a normal message. The input boolean though does give us the ability to easily turn off this alert, which is why I typically use an input boolean instead of a template sensor.

Test to Speech Notifications

While I typically use alerts to send notifications to our mobile devices via the Home Assistant companion app, you can use a text to speech notification as well. The only issue here is your Text to Speech integrations are notifications services. But we can make one. 

For that we need to jump into the configuration.yaml file and create a notify section if you don’t already have one. 

  - name: living_room # The name
    platform: tts
    tts_service: tts.google_translate_say # The TTS service
    # tts_service: tts.cloud_say # Nabu Casa TTS
    # tts_service: tts.amazon_polly_say #for those rocking the Amazon Polly
    media_player: media_player.ha_blue #media player

Then under notify: we just need to define our notification service. Unfortunately this is going to be speaker based. So unless you have a whole house system that plays in all rooms, if you have multiple speakers you will need one for each speaker that you want to send notifications too. 

name is the name you want to call this notification service

platform will be TTS. 

Then tts_service will be your Text to Speech service. I have included three common examples, but you could also use the the Amazon Media player one as well. 

Then to use this notification service we just need to add it to the list of notifiers for any alert you want Text to speech to be used. Like this:

    - mobile_app_jeffreys_iphone_8
    - living_room # A TTS Service?</code>

Then after you have your configuration.yaml changes and your alerts defined as well as any automations needed for your alert criteria, restart Home Assistant. From then on Home Assistant will notify you anytime one of those alert entities is in the state defined in your alerts.

Let me know if you think Alerts will be useful, and of course if you have questions hit me up in the comments or over on Twitter @thejeffreystone.

Until Next Time, Go Automate the Boring Stuff.

1 thought on “Home Assistant Alerts”

  1. Jeff,

    You can acknowledge the alerts by adding an actions: block to either the notify group or the alert block. Once you have that you can setup an automation with the action as a trigger, for example you can disable further notifications by calling the service alert.turn_off. Here’s a working example (Android companion app) for my garage notification.

    (alert config from alerts.yaml)

    name: Garage Door Alert Active
    entity_id: binary_sensor.sec_gar_door_alert_active
    state: “on”
    – 15
    can_acknowledge: true
    skip_first: true
    title: “Security – Garage Door Open”
    message: >
    Garage Door open for {{ relative_time(states.binary_sensor.sec_gar_door_alert_active.last_changed) }}
    Alert(s) on: {{ states.sensor | selectattr(‘entity_id’, ‘in’, [‘sensor.gar_sin_door’, ‘sensor.gar_dou_door’]) | selectattr(‘state’, ‘eq’, ‘Open’) | map(attribute =’name’) | list | join(‘,’) }}
    Triggered: {{ as_timestamp(states.binary_sensor.sec_gar_door_alert_active.last_changed) | timestamp_custom(‘%A %I:%M%p (%d-%b-%Y)’) }}
    done_message: “Garage Door Alert RESOLVED at {{ as_timestamp(states.binary_sensor.sec_gar_door_alert_active.last_changed) | timestamp_custom(‘%A %I:%M%p (%d-%b-%Y)’) }}”
    – action: gar_door_alert
    title: “Silence Alert”
    – not_info

    (notify group config)

    – name: not_info
    platform: group
    – service: mobile_app_pixel_2
    ttl: 0
    priority: high

    (automation to turn off further notifications)

    – id: ‘1650768108615’
    alias: ‘Acknowledge Garage Door Alert #alert’
    description: ”
    – platform: event
    event_type: mobile_app_notification_action
    action: gar_door_alert
    condition: []
    – service: alert.turn_off
    data: {}
    entity_id: alert.gar_door_alert_active
    mode: single

    This isn’t documented well, but I found the clue that got me here from this post: https://community.home-assistant.io/t/trigger-id-with-repeat-until/322884/15

    Thanks for educating us new users.

Leave a Reply