Smart Lighting Automation with Motion and Door Sensors

I’ve been experimenting with creating a smart lighting system in my home, and I wanted to share my experiences and some tips that might help others who are working on similar projects. My setup involves using a combination of door sensors, motion detectors, and smart lights to automate the lighting in my house. Here’s how I approached it:

The Goal

I wanted to create a system where the lights would turn on when I enter a room and turn off after a period of inactivity. The challenge was to ensure that the lights didn’t turn off immediately when I closed the door, as that would disrupt my workflow. Instead, I wanted the system to wait for a minute and check if there was any motion before deciding to turn the lights off.

The Setup

I used an EverSpring SM103 door sensor and an EZ Motion 3-in-1 motion sensor. Initially, I set up two scenes:

  1. Scene 1: Turn the lights on when the door sensor is triggered.
  2. Scene 2: Turn the lights off when the door sensor is not tripped, using Luup code to check for motion after a delay.

The Problem

Despite setting up the scenes, the lights would always turn off immediately after closing the door. This was frustrating because it defeated the purpose of the automation. After some research and troubleshooting, I realized that the issue was with the logic in the Luup code. The delay wasn’t being respected correctly, causing the lights to shut off without waiting for the motion sensor to trigger.

The Solution

I revised the Luup code to ensure that the system waits for the specified period before checking the motion sensor. Here’s the corrected code snippet:

lua
local sensorDeviceNo = 6 – Motion Sensor device number
local period = 60 – Seconds
local SS_SID = “urn:schemas-micasaverde-com:device:ComboDevice:1” – Motion Sensor Service ID

function checkLastTrip()
local lastTrip = luup.variable_get(SS_SID, “LastTrip”, sensorDeviceNo) or os.time()
if (os.difftime(os.time(), tonumber(lastTrip)) >= period) then
return true – Turn off the light
else
return false – Do nothing
end
end

This revised code ensures that the system waits for 60 seconds after the door closes before checking for motion. If motion is detected within that time, the lights remain on; otherwise, they turn off.

Tips for Others

  1. Test Each Component Individually: Before setting up complex scenes, ensure that each sensor and device is working correctly on its own.
  2. Use Debugging Tools: Luup provides excellent debugging tools that can help you identify issues in your code.
  3. Start Simple: Begin with basic automations and gradually add complexity as you become more comfortable with the system.
  4. Document Your Work: Keep a record of your code and any changes you make. This will help you troubleshoot issues in the future.

Conclusion

Creating a smart lighting system with motion and door sensors is a fantastic way to enhance your home’s automation. While there may be some hurdles along the way, with patience and persistence, you can create a system that works perfectly for your needs. If you’re working on a similar project, feel free to reach out for advice or share your experiences in the comments below!

Happy automating! :rocket: