Here’s a piston i wrote to control my bathroom light and fan. It uses a motion sensor in the bathroom in conjunction with a door sensor on the bathroom door to make a fairly accurate educated guess as to what’s going on and respond accordingly.
The conditions are:
- Turn on the light when someone enters the room - Between midnight and 7am turn on to 30%
- Turn off after 1 minute when someone leaves the room (and the door remains open behind them)
- The light will remain on for 20 minutes if the door is closed without motion (i.e. someone is in the shower and the motion sensor can’t see them)
- The fan will turn on 3 minutes after the light, only if the door is closed
- If the fan is on, and the door opens, then closes again within 20 seconds the lights will turn off after 90 seconds, the fan will continue for 20 minutes
- If any motion is detected after the above bullet’s door close, the system will revert and the light will remain on
- Before turning off the lights, the piston will flash them by dimming to 50% and back to 100% 3 times (or until motion is detected) before turning off the lights after waiting a bit so it doesn’t leave anyone in the dark without them knowing it’s going to happen.
Usage:
If you want to change the timeouts for the fan or lights, check all the places where the warnDelay and offDelay variables are set in the piston and update accordingly. Other than that, you should just be able to add your devices and have it.