Creating A Window/Door Notification in Home Assistant

Create automated reminders that alert you if you've left a door or window open when you go out for the day!

Creating A Window/Door Notification in Home Assistant

Intro

Not that anyone likes to admit to ever having done this but have you ever came home after being out for hours, only to realise you’ve left your garage door or house window wide open for the whole world to just come inside and help themselves to anything?

So, if you're ever guilty of this (I know I definitely am!), why not send yourself notifications whenever any of your doors/windows are left open and no one is home? Which means that by the time you get the notification, you won't be too far away from your home so you can quickly turn back and make sure to lock up properly.

This can also be used for your fridge/freezer door, or anything else you may not want to leave open - let's get started!

Scroll right the the bottom for the YAML code 👇🏼

How It's Done

Requirements

  • At least one door contact sensor
  • A phone connected to Home Assistant with the Companion app and location tracking enabled

Groups

This is a pretty simple automation to achieve - we essentially want to run this automation whenever we leave our house, check if windows or doors are open and then if they are, send us a notification that tells us exactly which doors/windows are open with some template magic.

The first thing we want to do before creating an automation though, is to create a group. This allows us to group together all of the sensors we want to alert on - we could alert on all doors, but you probably don't want to be notified if you left an inside door open for example.

Head over to Settings, Devices and Services and click on the helpers tab. Select create helper, and select Group from the list and then select Binary Sensor:

Next, give the group a name and then add all of the doors and windows you would like to monitor into the group. Make sure to leave the "All entities" unchecked:

The Automation

Next, head over to Settings, Automations and Scripts and create a new empty automation with a name:

We are going to use the "state" of our home zone as the trigger, which contains the number of people in that zone. So if Sarah and myself are both home, the state will be 2. If just I am home, the state will be 1:

For the trigger, set "Numeric state" as the type, then select your home zone as the entity. In the "Below" box, enter 1. This is so that when no one is home, the automaton will run.

For the condition, select state as the type, then select the group you created earlier as the entity. In the state box, enter "on". This means that the automation will continue running so long as any members of the group are on, aka open:

As for the Action, select "Call Service" from the drop-down and then search for "notify" and select the notification service that matches with your mobile device:

Then hit the three dots in the corner of the action (not automation) and select "Edit in YAML". We need to change to YAML because templates are not yet supported in the UI editor.

Paste in the following template (making sure to keep the service set to your device):

service: notify.your_mobile_device
data:
  title: You left the doors open dummy
  message: >-
    You left these doors/windows open:
    {{ expand('binary_sensor.your_group_name')
       | selectattr('state', 'eq', 'on') 
       | map(attribute='name') 
       | list 
       | join (', ')
    }}

You need to make sure and replace "binary_sensor.your_group_name" with the name of your group. To find this, open a new tab (don't leave your automation just yet!) and go to Developer tools and then states. Search for the name of your group in the filter column:

Copy the value from the entity column, in my case it is "binary_sensor.external_doors_and_windows". Paste this into the above template.

Your action should look something like this:

I know this template looks kinda intimidating, but essentially what it's doing is getting the members of the group who's state is on, aka, open.

If you'd like to know more about templates, check out my video here where we go into much more depth.

Finally, hit the save button on the automation and now when you go out, you should get a notification that looks like this:

And that's all there is too it!

The Code

Here is the code for the automation for those who prefer YAML:

alias: Notify - You left the door open!
description: ''
trigger:
  - platform: numeric_state
    entity_id: zone.home
    below: '1'
condition:
  - condition: state
    entity_id: binary_sensor.your_group_name
    state: 'on'
action:
  - service: notify.your_notification_device
    data:
      title: You left the doors open dummy
      message: >-
        You left these doors/windows open: {{
        expand('binary_sensor.your_group_name')
           | selectattr('state', 'eq', 'off') 
           | map(attribute='name') 
           | list 
           | join (', ')
        }}
mode: single