Get a push notification if a door or window is left open when you leave the house
In this post we're going to create an Automation in Home Assistant that notifies you if a door or window has been left open after you've left the house.
I use this to help increase my home security by making sure that I've not made it easy for someone to rob me by leaving a door or window wide open when I go out.
To make this work you'll need the following:
A working Home Assistant installation
The Home Assistant Mobile application installed on your mobile phone
At least one Person entity with a device tracker turned on
As always, get started by creating a new Automation in Home Assistant. This can be done under the Configuration area and then Automations.
Trigger
This automation is triggered when the person entity goes from home to not_home. I use a State trigger type
I have multiple people living in my house, so I have previously created a group called residents which contains the device trackers for everyone. This means that this automation will only trigger if everyone leaves the house. I don't want to be notified if a door or window is open if someone is still at home.
Screenshot of Trigger in Home Assistant
Here is the full trigger block in YAML
platform: state
entity_id: group.residents
from: home
to: not_home
Conditions
The conditions of this automation are where the real work is done. We want this automation to only complete if one of the doors or windows that we care about is in the open state.
This is why you will need a contact sensor paired with Home Assistant for every door or window that you care about from a security point of view.
Enter all the contact sensor entities that you want to monitor in a list, separated by commas. You may not want to pick all your door and window contact sensors here, as it may not be important to you if you've left a top floor window slightly ajar when you leave the house.
I use a State condition type to check if any of the states of the sensors is set to on. Contact Sensors are really confusing, their state is set to on when the door is opened. You can validate this yourself by opening one of the doors and then looking at that contact sensor in the developer tools.
Sonoff Contact Sensor for an open door showing on state, but contact is false.
Here is the full condition block in YAML.
condition: state
entity_id:
- binary_sensor.back_garden_door_contact_sensor_contact
- binary_sensor.front_door_contact_sensor_contact
- binary_sensor.kitchen_window_contact_sensor_contact
state: 'on'
Action
The action of this automation is very simple. I use the notify integration to send a push notification to the Home Assistant app installed on my mobile phone.
Screenshot of Action in Home Assistant automation
You can create multiple actions here to send this notification to multiple people.
Full Automation in YAML
Here is the full automation, including the action, in YAML format.
alias: 'Notify: Doors/Windows left open when we''ve left'
description: ''
trigger:
- platform: state
entity_id: group.residents
from: home
to: not_home
condition:
- condition: state
entity_id: >-
binary_sensor.back_garden_door_contact_sensor_contact,binary_sensor.front_door_contact_sensor_contact,binary_sensor.front_garden_outer_door_contact_sensor_contact,binary_sensor.gym_window_contact_sensor_contact,binary_sensor.kitchen_window_contact_sensor_contact
state: 'on'
action:
- service: notify.mobile_app_alan_s_pixel_4
data:
message: You've left a door or window open!
mode: single