Hallway lights automation

In this post, I will explain how I transformed my apartment’s standard hallway lights into smart, automated lights controlled by Home Assistant. By integrating a pair of motion sensors and a SONOFF ZigBee Mini Smart Switch, my ordinary lights have been upgraded and now can automatically switch on and off in response to movement. This post will explain how the automation works, demonstrating how typical, non-smart lights can seamlessly integrate into a smart home setup.
How does it work?
The hallway of my apartment features an automated lighting system that combines motion sensors, smart switches, and home automation software, accommodating its unique Z-shaped layout.
Two strategically placed Sonoff Zigbee motion sensors cover the hallway’s main corridors, as shown in the Picture Elements Card from my Home Assistant (below), where the hallway layout is depicted with a red line marking the path from the apartment’s entry to the living room.
The motion sensors, represented by an icon of a person crossed out, are positioned at both ends of this line.

These sensors play a crucial role in the automation system by detecting movement along the hallway and sending a signal to Home Assistant using Zigbee protocol. The automation within Home Assistant then controls the hallway lights based on factors such as the time of day and existing light conditions, ensuring lights are only on when needed for energy efficiency.
The hallway is equipped with downlights connected to a 2-way switch system, with one switch near the entry door and another near the living room door. This latter switch is crucial for the automation as it contains live and neutral wires, allowing the installation of a SONOFF ZigBee Mini Smart Switch.
This smart switch, integrated with the Home Assistant platform via Zigbee2MQTT, is the central component of the automation system. It enables remote control of the hallway lights, acting as a bridge between the manual switch and Home Assistant software.
When Home Assistant’s automation decides to activate the lights, based on the motion sensors and other conditions, it sends a command to the smart switch, which then closes the circuit loop of the hallway downlights. This automated control can always be overridden by the manual switch if necessary.
Devices
This is the list of all devices that are part of the hallway downlights automation:
- SONOFF ZigBee Mini Smart Switch
- 2 x SONOFF SNZB-03 ZigBee Motion Sensor
- Raspberry Pi + SONOFF Universal Zigbee 3.0 USB Dongle Plus Gateway
Sonoff Zigbee Mini wiring
Here it comes the most complex section of the whole setup: how to do the electrical wiring of the Sonoff Zigbee Mini Smart Switch. However, before we proceed with the wiring process, it is paramount to adhere to a set of safety guidelines. These guidelines are crucial for ensuring a successful installation and for your safety and the protection of your property.
Safety guidelines
Remember, safety is the foremost priority when dealing with electrical installations therefore it’s essential to follow these fundamental guidelines:
- Should you feel unsure about handling basic electrical tasks or have any safety reservations, consider hiring an electrician instead of attempting the installation yourself.
- Before buying the ZigBee mini Smart switch, verify if the electric box you’re going to modify is equipped with a neutral wire. This might involve unscrewing and partially pulling out a wall panel to check. If the electric box lacks a neutral wire, you might need to buy the SONOFF ZBMINIL2 Zigbee Smart Light Switch instead.
- Always turn off the power to the circuit before working on any wiring. For added safety, use a voltage tester to confirm there’s no electrical current in the wires.
- To avoid confusion with wire connections, consider labelling them using electrical tape.
- When reinserting wires into the switch box, do so carefully and neatly to minimize any risk of fire.
The original wiring of the hallway lights
The hallway has 2 two-way light switches which allow turning the light on and off from either switch at any time. The circuit diagram is as follows:

The new wiring using the Sonoff ZigBee Mini
The ZigBee Mini has been installed inside one of the electrical boxes, in particular the one at the end of the hallway. This box has live and neutral wires along with the output line to the ceiling lights. The final wiring is shown in this diagram:

This is how it looks
The Sonoff ZigBee mini is small enough to fit inside the mounting box as shown in the first image. However, the 2-way switch was using the space where the Zigbee now stands so it was necessary to use a gang plate spacer to allow for some extra space in the electrical box.
The second image shows the depth of the 2-way switch (grey part of the outlet plate). The image on the right shows the gang plate spacer which gives an extra 12mm.



ZigBee Mini working
Once the ZBMini has been properly installed in the mounting box you can continue using the 2-way switches as usual. The difference now is that they will send their signal to the ZigBee mini which will turn on or off the lights.
The Sonoff ZigBee is by default using edge mode and it reacts for every switch operation no matter if the external switch turns “On” or “Off”, that is the switch is flipped for continuous “On” or “Off after being pressed every time. You can read more about the edge mode here.
Home Assistant
Zigbee2MQTT
After the ZigBee mini is powered, it will enter the paring mode in the first use and an LED signal indicator will flash. The device will exit the pairing mode if it’s not paired in the next few minutes. The device can be put again in paring mode by long pressing the manual switch on the case for 5s until the LED signal indicator flashes again.
In the case of the motion sensors, they will also enter into pairing mode once the battery insulation sheet is removed.
To add the devices to Zigbee2MQTT is as simple as clicking on “Permit join (All)” and waiting until the devices appear in the list.

I renamed the 3 devices so they have a “Friendly name” in Home Assistant:

Entities and devices
This is where the Home Assistant magic happens, as soon as Zigbee2MQTT identifies the devices they also appear in Home Assistant including their own set of entities. There are also dashboards for each device showing the entities associated, automation where the device is used, a logbook, and so on. The image below shows the device dashboard for the motion sensor placed at the end of the hallway.

When clicking on the blue plus symbol in the Automation dashlet, you can see the whole list of possible states and create an automation based on them:

Automation
A set of repeatable actions
that can be set up to run automatically. Automations are made of three key components: triggers, conditions and actions
Everything comes together in this part, the automation. I’ve named it “Hall lights when movement detected” and the goal is to achieve this:
When entering the hallway (from any door), turn on the hallway lights depending on various conditions and turn them off after exiting. The conditions to turn them on are:
- Always from sunset to sunrise (excluding the hours when I’m sleeping)
- During the day when walking through the entry of the hallway as there’s no light from the outside
- TODO: during the day if the outside light is very dim (on a gray cloudy day)
With this goal in mind, let’s explore the different parts of the automation.
Triggers (When)
As mentioned, the automation should be triggered if any of the sensors detect movement:

Conditions (if)
During the day, we want only the hallway lights on when walking around the entrance of the apartment, as there’s almost no light from the outside reaching that area. That part of the corridor is where the Hall entry sensor is located
30 minutes before sunset the lights will be turned on if motion is detected in any part of the hallway.
Last, there’s a state called “Sleep time” (activated when sleeping) that prevents the lights from being switched on during that time.

Actions (then do)
The Hallway lights will be turned on and remain as such while any of the sensors detect movement.
When the sensors are no longer detecting movement, the automation will wait a further 1 minute and 30 sec before turning off the lights.
The reason for this is that the sensors need some time to reset themselves before starting to detect motion again. During that time, if you walk in the hallway the sensors won’t detect anything so it’s best to keep the lights on until the sensors are functional again.

Where to buy
All the devices to complete this setup are available to purchase on Amazon:



