I have a traditional wall switch installed in my kitchen but it's a bit difficult to turn it on/off because it has limited space to access the switch. So I want to automate the light on/off to make it easy for family members. However instead of turning the light on for a fix period of time is not smart enough. I want a system that continuous extends the timer if there is someone walking in the kitchen.

Systems/Devices List

What won't be covered in this topic?

  • Zigbee USB dongle & zigbee2mqtt installation and configuration
  • wire and physical installation of the wall switch

Installation Steps

(1) Installation of Zigbee Wall Switch

  • Replace the classical wall switch with Tuya Zigbee wall switch. If you installed properly (without integrating with HA), you shall be able to toggle the light on/off manually.
  • Pair the Tuya switch with zigbee2mqtt by long press the switch for 5-10 seconds. After a few seconds, you should see the device popup in Zigbee2MQTT's web. Just change the name to whatever you want, I simply change mine to "wall_switch_kitchen".
TUYA Zigbee Wall Switch (TS0011) in Zigbee2MQTT's UI
  • You should be able to toggle the light on/off from HA's UI. Customize is required if you are using custom Lovelace UI.

(2) Installation of Motion Sensor

  • Long press the reset hole for 5 seconds to join the Aqara motion sensor into Home Assistant (via Zigbee2MQTT).
  • After a few seconds, you should see the device popup in Zigbee2MQTT's web. Just change the name of this device, mine is "motion_sensor_kitchen".
  • I recommend to test the motion sensor by create some movements and see the state of the sensor by goto "Developer Tools" --> "Template" and type below template syntax (change the sensor name to your sensor's name).

{{ states.binary_sensor.motion_sensor_kitchen_occupancy.state }}

  • If there is a movement the state shall be "on". And it shall be "off" for no activity detected.
  • Don't expect the near real time detection from this kind of sensor. It sends "on" immediately if detected a big enough movement in 1-3 meters distance. But the clear signal may take 1 min after "on" (2 min for Sonoff SNZB-03). Setting "occupancy_timeout <seconds>" in /share/zigbee2mqtt/devices.yml is not help, it will just send the clear signal at specified configuration but if there is any movement during 1 min period, it won't send any "on" signal anyway.

Now you can simple play with basic automation such as

  • When the state of motion sensor changed to "on", just turn the light on (by calling service "switch.turn_on" for entity_id: wall_switch_kitchen

Note on Choosing Aqara or Sonoff Zigbee Motion Detection Sensor:

  • I prefer to use Aqara RTCGQ11LM rather than Sonoff SNZB-03 because Aqara ships with illuminance level which is good for this use case. So that we don't need to rely Sun or external illuminance sensor.
  • Automation can be triggered the light on when the room is dark enough.

(3) Setup the Timer

  • Timer integration gives us dynamic duration variable (via state). You can check out this document for more information about HA's Timer.
  • We need to setup the timer in configuration.yaml of HA. Just naming the timer and give it a default duration (5 min in below example).
  • Don't forget to restart the HA before creating the automation in step (4).
    duration: '00:05:00'

(4) Create an Automation if a movement was detected

  • Trigger
  • Condition (only when the room is dark enough). You may need to adjust the number based on your environment.
  • Action (1) --> Turn on the Zigbee wall switch (the light).
  • Action (2) --> Start the Timer with default duration (5 min).

It is really simple configuration without any geek modification and effectiveness solution. When the light is Off, someone was walking into the kitchen. This automation will turning on the Light and start the 5 minutes timer. HA will counting down every second. Next, we will just need to create the second automation to handle state of this timer and just turn off the light.

If the motion sensor detected a movement during 5 min duration, this automation will try to turn On the light (which already On, so nothing happen) and run "timer.start" which works like restart the timer again. So the 5 min duration will be extended automatically.

(5) Create an Automation if the Timer finished

  • If you trigger the light On (walked into the kitchen) and walk out from the room. The light will be automatically turning Off by this automation.
  • Trigger:
  • Condition
  • Action

After save the automation and restart the automation. You should try to test the solution in all use cases to make sure that there is nothing went wrong. Using "Developer Tools" --> "Template" can save a lot of debugging time.

Debugging the Timer variable is straight forward as shown below:

{{ states.timer.light_timer_kitchen }}
{{ states.timer.light_timer_kitchen.state }}
{{ states.timer.light_timer_kitchen.state == "idle" }}

If everything went fine as expected, there is another scenario that you can optionally implement. What if a family member turned On the light by pressing the Tuya wall switch. The light will be On forever until manually turn off. In my case, I want to use the same Timer to turn the light Off automatically. I will need to create the 3rd automation to handle this scenario.

(6) Restart the Timer if anyone turn on the light

  • Trigger
  • No Condition
  • Action

This automation will be called by both scenarios
(1) light On by motion sensor or
(2) manually On the light via Tuya wall switch

For scenario (1), it's just calling timer.start twice in the same time which has no major impact.

For scenario (2), even turnig the light On manually, it will be turning Off by this automation.

Thank you for your reading. Hope you can apply my solution to your use case. Any feedback or suggestion, please direct to my twitter account @auntispam

Buy me a coffee