The Open and Shut Case: Door and Window Sensors
Your smart home has a brain and hands; now it gets awareness of what is happening around it. Zigbee door and window sensors are simple, a main unit and a magnet, but they let your home react to any opening or closing: turning on lights, sending alerts, or sounding an alarm. In this lesson you will pair, mount, and test a sensor, then build automations from it, including a basic security alert.
How the sensor works
Each sensor is two parts: a main unit and a smaller magnet. You mount them side by side on a door or window, one on the frame and one on the moving part. While they are together, the sensor reads closed; when the door opens and the two parts separate, it reads open. That single open-or-closed signal is surprisingly powerful, because you can attach any automation to it. Alignment matters: if the two halves do not line up when closed, the sensor cannot tell the difference.
Where you can use it
It is not just for doors. Because it only cares about two things moving apart, you can stick it on a window, a cabinet, the fridge, a closet, or a mailbox, anything that opens and closes. This turns ordinary objects into sensors your home can react to, and Home Assistant keeps a history log so you can see, for example, how many times the fridge was opened today.
Automations and conditions
The sensor's open event becomes a trigger. A natural first automation: when the front door opens, turn on a light. You can make it smarter with a condition, an extra check that must be true for the action to run, such as only after sunset, so the welcome light comes on in the evening but not in daylight. Trigger, optional condition, action: that pattern drives everything you will build this month.
From convenience to security
The same sensor can guard your home. Combined with an away mode, you can make opening the door while you are out flash a light red, send you a notification, and play a warning sound on the Pi, all from one small sensor. Away mode is set up as a helper in Home Assistant, a simple on-or-off value your automations can check. Because it is self-hosted, this security behavior is entirely yours, with no outside company involved.
Working through it
Pair the sensor. In ZHA choose Add Device, put the sensor in pairing mode by holding the small reset button on the back with a pin, and name it for its location, like front door sensor.
Mount it aligned. Peel and stick the main unit on the frame and the magnet on the moving part, making sure the two halves line up when the door is closed.
Test the state. Open and close the door and watch the state flip between open and closed instantly in Home Assistant. Check the history log too.
Build a welcome-light automation. Create an automation triggered by the sensor changing from closed to open, with the action to turn on your bulb or plug.
Add a condition. Add a condition so it only runs after sunset, giving you an automatic welcome light in the evening.
Automation: light on when the door opens after dark
alias: Welcome light on door open
trigger:
- platform: state
entity_id: binary_sensor.front_door_sensor
from: "off" # closed
to: "on" # opened
condition:
- condition: sun
after: sunset # only in the evening
action:
- service: light.turn_on
target:
entity_id: light.entryway_bulb
The trigger fires when the door sensor changes from closed to open, the condition limits it to after sunset, and the action turns on the entry light. The visual editor builds this same configuration for you.
Common mistakes and troubleshooting
The sensor never reads closed. Align the two halves so they sit close together when the door is shut; a gap keeps it reading open.
The sensor will not pair. Hold the small reset button with a pin to enter pairing mode, then start Add Device in ZHA.
The welcome light comes on in daylight. Add an after-sunset condition so the automation only runs in the evening.
The security alert fires when you are home. Gate it behind an away-mode helper so it only triggers when you are out.
Next: Next you will control RGB smart bulbs and build lighting scenes.