There are multiple ways to turn lighting on and off depending on motion.
But I wanted something that would turn off the lights after the motion stopped, but was not dependent on how the light was turned on. And including the situation where the light was turned but there was never any motion.
So here’s my app that only turns switches off. It assumes the switch is turned on manually or by a different smartapp
/**
- Lighting Off On Timer After No Motion Detected - regardless of how it was turned on or whether there was any motion
- States are
- 0 - light is off - wait for light to be turned on by anything, set state = 1
- 1 - light is on waiting for time to expire (set timer, but if motion, set state = 2)
- 2 - light is on with motion; when motion stops, set state = 1 and start timer
- When time is up, if state is still 1, then turn off switch, and set state = 0
*/
definition(
name: “Lighting Off On Timer After No Motion Detected”,
author: “King”,
description: “Lighting Off On Timer After No Motion Detected - regardless of how it was turned on or whether there was any motion”,
category: “Convenience”,
iconUrl: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png”,
iconX2Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”,
iconX3Url: “https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png”)
preferences {
input(name: “motion1”, type: “capability.motionSensor”, title: “When there’s no motion on…”, required: true, multiple: false, description: “this motion sensor”)
input(name: “delay1”, type: “number”, title: “For at least…”, required: true, description: “this number of minutes”)
input(name: “switch1”, type: “capability.switch”, title: “Turn these off …”, required: true, multiple: true, description: “these switches”)
}
def installed() {
initialize()
}
def updated() {
unsubscribe()
initialize()
}
def initialize() {
subscribe(switch1, “switch.on”, switchOnHandler)
subscribe(switch1, “switch.off”, switchOffHandler)
subscribe(motion1, “motion.active”, motionOnHandler)
subscribe(motion1, “motion.inactive”, motionOffHandler)
state.phasenumber = ‘0’
log.info(“Lighting Off On Timer After No Motion Detected V21.9.20.1039”)
}
def switchOnHandler(evt) {
state.phasenumber = ‘1’
def MinutesDelay = 60 * delay1 as int
runIn(MinutesDelay, timerDone)
log.debug "Light is on - no motion: - state is $state.phasenumber; wait for $delay1 minutes "
}
def switchOffHandler(evt) {
state.phasenumber = ‘0’
log.debug “Light switched off: - state is $state.phasenumber”
}
def motionOnHandler(evt) {
state.phasenumber = ‘2’
log.debug “Motion detected: - state is $state.phasenumber wait for Motion to stop”
}
def motionOffHandler(evt) {
state.phasenumber = ‘1’
def MinutesDelay = 60 * delay1 as int
runIn(MinutesDelay, timerDone)
log.debug "Light is on - no motion: - state is $state.phasenumber; wait for $delay1 minutes "
}
def timerDone() {
if (state.phasenumber == ‘1’) {
state.phasenumber = ‘0’
switch1.off()
log.debug “Switching light off: - state is $state.phasenumber”
}
}