Get notified if a door or window is opened whilst you’re away

In this post we’re going to create an Automation in Home Assistant that notifies you if a door or window gets opened whilst everyone is out of the house.

I use this to help increase my home security by alerting me if someone opens a door or window when we’re out. This could be expected because we’ve given a key to a friend or family member – but maybe it’s not expected and it’s someone breaking in!

To make this work you’ll need the following:

  1. A working Home Assistant installation
  2. Some Contact Sensors paired with Home Assistant
  3. The Home Assistant Mobile application installed on your mobile phone
  4. 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 a door or window that we care about goes from closed to open. You can add multiple door and window contact sensor entities into the entity list if you separate them with a comma.

Screenshot of trigger showing contact sensors state changing from off to on

I use a State trigger type to check if any of the states of the sensors goes from off 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.
Sonoff Contact Sensor for an open door showing on state, but contact is false.

Here is the trigger block in YAML:

platform: 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
from: 'off'
to: 'on'

Conditions

The conditions of this automation are where the real work is done. I use a state condition to check that everyone has been away for at least two minutes.

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.

I also specify that they have to have left for at least two minutes. There were times in the past where my phone GPS misrepresented where I was and the device tracker thought I was out when I was actually home. The two-minute delay gives the phone a chance to correct itself before triggering any away automation.

Here is the full condition block in YAML.

condition: state
entity_id: group.residents
state: not_home
for: '00:02:00'

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.

Screenshow showing action area of home assistant automation, notifying my mobile phone.

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 are opened whilst we are out'
description: ''
trigger:
  - platform: 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
    from: 'off'
    to: 'on'
condition:
  - condition: state
    entity_id: group.residents
    state: not_home
    for: '00:02:00'
action:
  - service: notify.mobile_app_alan_s_pixel_4
    data:
      message: Door or window has been opened whilst you are out!
mode: single