---
title: "Forgot to lock up? Automatic locking and a notification"
description: "A door sensor and a smart lock lock the front door after it has stayed closed for 10 minutes. A second automation sends a notification if it is unlocked while the tracked people are away from home."
canonical: "https://realhomeautomations.com/en/projects/forgot-to-lock-auto-lock-notification/"
language: en-US
updated: 2026-11-23T16:24:03+00:00
---

# Forgot to lock up? Automatic locking and a notification

A door sensor and a smart lock lock the front door after it has stayed closed for 10 minutes. A second automation sends a notification if it is unlocked while the tracked people are away from home.

> Canonical HTML page: https://realhomeautomations.com/en/projects/forgot-to-lock-auto-lock-notification/

- **Difficulty:** Intermediate
- **Build time:** 45–90 minutes
- **Reading time:** 7 minutes

## Tools required

- A smart lock that Home Assistant sees as a lock (ours is the Aqara Smart Lock U200 over Matter)
- Thread border router and Matter Server (the U200 runs on Thread)
- You need a Zigbee hub or Home Assistant with a Zigbee adapter. (for the door sensor)
- Home Assistant Companion on the phone (with notifications allowed)
- A mechanical key that unlocks (tested before you start the rule)


## What readers will learn

- Check the state of the lock and the sensor in Developer tools → States
- Build an automation that locks when the door has been closed for 10 minutes
- Build a notification for when the lock is unlocked and the tracked people are away from home
- Turn off automatic locking and avoid locking yourself out


## Skill requirements

- Add Matter and Zigbee devices in Home Assistant
- Edit an automation in YAML
- Read an automation's trace (Traces)

## Project kit

- [Smart Door and Window Sensor Sonoff SNZB-04PR1 – Zigbee 3.0 Protocol, CR2477 Battery Powered, Real-Time Opening Alerts, eWeLink &#038; Home Assistant](https://realhomeautomations.com/en/product/smart-door-and-window-sensor-sonoff-snzb-04pr1-zigbee-3-0-protocol-cr2477-battery-powered-real-time-opening-alerts-ewelink-home-assistant/index.md)

You leave in a hurry, pull the door shut and on the way you wonder whether you locked it. Two automations take that question away: one locks the door after it has stayed closed for some time, the other writes to you if an unlocked door has been left with nobody at home.

 ## How it works

 The door sensor says "open" or "closed". The lock says "locked" or "unlocked". Home Assistant compares the two. If the door has been closed for 10 minutes and the lock is still unlocked, it sends a lock command. The sensor stops the rule from locking onto an open door.

 The second automation locks nothing. It watches the people you track in Home Assistant ( `person.*`). If everyone is away from home for a chosen time and the lock is unlocked, you get a notification on your phone. You decide: you go back or you lock from the lock's app.

 Many smart locks also have their own automatic locking. The Aqara U200, which we use, has it among its features. Here we do it in Home Assistant, because that way the condition is the state of your own door sensor and you can change it.

 ## Before you turn on automatic locking

 An automatic lock can lock you out. Check three things before you start the rule:

- You have a mechanical key that really unlocks, and another person or a safe place to get it from. Try it at least once.
 - The door opens from the inside without a key, a code or a phone. That depends on your lock and cylinder. Read the manual and test it with the door open.
 - You know how to turn the rule off (see “Limitations”) before guests or tradespeople catch you off guard.

 ## Step 1: Add the lock and the sensor

 The lock is a Matter device. In Home Assistant it is added through the Matter integration and its Matter Server. The U200 runs on Thread, so you also need a Thread border router in range. Our sensor is a Sonoff SNZB-04P through ZHA. The shop has the [Sonoff SNZB-04PR1](https://realhomeautomations.com/bg/produkt/smart-door-and-window-sensor-sonoff-snzb-04pr1-zigbee-3-0-protocol-cr2477-battery-powered-real-time-opening-alerts-ewelink-home-assistant/) (Zigbee 3.0, CR2477 battery); we haven't tested it.

 Fix the sensor so that, with the door closed, the distance between it and the magnet is under 20 mm and the markings face each other. Check the spot first, then stick it.

 Check in **Developer tools → States**: `lock.*` the lock's state should switch between `locked` and `unlocked`, and the sensor (`binary_sensor.*` with class `opening`) should be `on` when the door is open and `off` when it is closed. If it doesn't match, don't go on.

 ## Step 2: Lock after the door has been closed

 Open **Settings → Automations & scenes → Create automation → Create new automation**, the three-dot menu → **Edit in YAML** and paste the example. Replace `lock.front_door` and `binary_sensor.front_door_opening` with your own.

 ```
alias: Front door auto-lock after close
description: Locks when the door has been closed for at least 10 minutes and the lock is unlocked.
mode: single
triggers:
  - trigger: time_pattern
    minutes: "/1"
  - trigger: state
    entity_id: binary_sensor.front_door_opening
    to: "off"
    for:
      minutes: 10
conditions:
  - condition: state
    entity_id: binary_sensor.front_door_opening
    state: "off"
  - condition: state
    entity_id: lock.front_door
    state: "unlocked"
  - condition: template
    value_template: >-
      {{ (as_timestamp(now()) - as_timestamp(states.binary_sensor.front_door_opening.last_changed)) >= 600 }}
actions:
  - action: lock.lock
    target:
      entity_id: lock.front_door
```

 The example values are 10 minutes (600 seconds) and a check every minute. If you change 10 minutes, change the number 600 in the template too, otherwise the two places will drift apart.

 The two triggers do different jobs. The sensor fires the rule exactly 10 minutes after the door closes. The check every minute catches the case where the lock is unlocked and the door has been closed for a long time: then the sensor doesn't change state and wouldn't fire anything by itself.

 This leads to a quirk: if you unlock the lock without opening the door, the rule locks it again within a minute. It doesn't wait another 10 minutes. The condition looks at when the sensor changed its state, not at when you unlocked.

 ## Step 3: A notification when the door is unlocked and nobody is home

 You need the Home Assistant Companion app on your phone, signed in to your Home Assistant and with notifications allowed. Find the service name in **Developer tools → Actions**: search for `notify.mobile_app`.

 ```
alias: Alert - everyone away and front door unlocked
description: Notifies when all tracked people are away from home and the front door is unlocked.
mode: single
triggers:
  - trigger: template
    value_template: >-
      {{ is_state('person.vkashti_1', 'not_home')
         and is_state('lock.front_door', 'unlocked') }}
    for:
      minutes: 25
actions:
  - action: notify.mobile_app_telefon_1
    data:
      title: The front door is unlocked!
      message: Everyone has been away from home for over 25 minutes, and the front door is still unlocked.
```

 If you track more than one person, add another `is_state('person.…', 'not_home')` together with `and` and another notification. 25 minutes is our value, not a rule. Choose a time that doesn't send you a notification on every short outing.

 The template trigger fires when the condition becomes true and waits `for`. Restarting Home Assistant or reloading the automations resets this wait. A notification that should have come may not come.

 ## Check

- **First test with a short time.** For a trial, change 10 to 1 minute and 600 to 60 (in both places). Stay inside and keep the key. Unlock, close the door and wait. The lock should lock, and in **Settings → Automations & scenes** → the automation → **Traces** you should see a run with `lock.lock`.
 - **Edge case: open door.** Unlock, open the door and leave it open longer than the time. The lock must not lock. The sensor condition should stop the trace with `failed_conditions`.
 - **After a restart.** Restart Home Assistant. Check that the lock and the sensor show correct states again before you restore the normal values.
 - **The notification.** Reduce 25 minutes to 1 and leave the home with your phone. Then restore the value.

 ## If it doesn't work

- **The trace is always `failed_conditions`.** The rule is evaluated every minute, so this is normal when the lock is locked. If the lock is unlocked and the trace still fails, open it and see which condition didn't pass.
 - **The lock doesn't lock, and the conditions pass.** Check its state: `jammed`, `unavailable` or `open` are not `unlocked`. Move the Thread border router closer or check that the Matter Server is running.
 - **It locks onto an open door.** The sensor says "closed", and the door isn't. Most often it is a shifted magnet or a distance over 20 mm. Stop the automation and fix the installation.
 - **The notification doesn't arrive.** Check the name of `notify.mobile_app_*` and whether `person.*` has a location. State `unknown` is not `not_home`.

 ## Limitations

- **Batteries.** The sensor uses a CR2477 (up to 5 years with normal use, according to Sonoff). The lock has a rechargeable battery (up to 6 months per the shop description), and its keypad runs on 4 AAA batteries. Set up a low-battery notification and don't wait for the first failure.
 - **Locking yourself out.** If you go out and come back after more than 10 minutes, you'll find the door locked. Carry a key or a phone, or extend the time.
 - **A sensor that stops responding.** State `unavailable` is not `off`, so the rule doesn't lock. But battery-powered Zigbee devices are shown as unavailable with a delay. Until then Home Assistant may remember the last state, and it may not be true.
 - **The notification doesn't lock.** It only says that something isn't right. Nothing is closed remotely until you do it.
 - **Connection.** Matter and Zigbee work on the local network, so locking doesn't depend on the internet. The notification goes through the phone's push service and may be late or not arrive without a connection.
 - **How to turn it off.** In **Settings → Automations & scenes** turn off the automation's switch. The lock keeps working with a key, a fingerprint or a code. Turn it back on when you want.

 ## Sources

 The two automations and the models are from Marto's Home Assistant as of 19.10.2026. The examples are cleaned of names and phones, and the identifiers are examples. The descriptions of the sensor, the lock and the integrations follow the documentation. The edge cases from the “Check” section (open door, restart) have not been specially tested by us. They are described as checks that you do.

- [Sonoff SNZB-04P: battery, distance to the magnet, installation](https://help.sonoff.tech/docs/snzb-04p)
 - [Home Assistant: the Matter integration, Thread and local control](https://www.home-assistant.io/integrations/matter/)
 - [Home Assistant: the Lock integration and lock states](https://www.home-assistant.io/integrations/lock/)
 - [Home Assistant: triggers (time pattern, template, for)](https://www.home-assistant.io/docs/automation/trigger/)
 - [Home Assistant: conditions](https://www.home-assistant.io/docs/automation/condition/)
 - [Home Assistant: the Person integration](https://www.home-assistant.io/integrations/person/)
 - [Home Assistant Companion: notifications](https://companion.home-assistant.io/docs/notifications/notifications-basic/)