[DVCSMP-2595] Set scheduled executions for Lights off with no motion SA to overwrite

This commit is contained in:
Aaron Miller
2017-04-20 13:16:08 -05:00
parent 03a79b8bb5
commit 5b7a7097b8

View File

@@ -15,66 +15,66 @@ definition(
) )
preferences { preferences {
section("Light switches to turn off") { section("Light switches to turn off") {
input "switches", "capability.switch", title: "Choose light switches", multiple: true input "switches", "capability.switch", title: "Choose light switches", multiple: true
} }
section("Turn off when there is no motion and presence") { section("Turn off when there is no motion and presence") {
input "motionSensor", "capability.motionSensor", title: "Choose motion sensor" input "motionSensor", "capability.motionSensor", title: "Choose motion sensor"
input "presenceSensors", "capability.presenceSensor", title: "Choose presence sensors", multiple: true input "presenceSensors", "capability.presenceSensor", title: "Choose presence sensors", multiple: true
} }
section("Delay before turning off") { section("Delay before turning off") {
input "delayMins", "number", title: "Minutes of inactivity?" input "delayMins", "number", title: "Minutes of inactivity?"
} }
} }
def installed() { def installed() {
subscribe(motionSensor, "motion", motionHandler) subscribe(motionSensor, "motion", motionHandler)
subscribe(presenceSensors, "presence", presenceHandler) subscribe(presenceSensors, "presence", presenceHandler)
} }
def updated() { def updated() {
unsubscribe() unsubscribe()
subscribe(motionSensor, "motion", motionHandler) subscribe(motionSensor, "motion", motionHandler)
subscribe(presenceSensors, "presence", presenceHandler) subscribe(presenceSensors, "presence", presenceHandler)
} }
def motionHandler(evt) { def motionHandler(evt) {
log.debug "handler $evt.name: $evt.value" log.debug "handler $evt.name: $evt.value"
if (evt.value == "inactive") { if (evt.value == "inactive") {
runIn(delayMins * 60, scheduleCheck, [overwrite: false]) runIn(delayMins * 60, scheduleCheck, [overwrite: true])
} }
} }
def presenceHandler(evt) { def presenceHandler(evt) {
log.debug "handler $evt.name: $evt.value" log.debug "handler $evt.name: $evt.value"
if (evt.value == "not present") { if (evt.value == "not present") {
runIn(delayMins * 60, scheduleCheck, [overwrite: false]) runIn(delayMins * 60, scheduleCheck, [overwrite: true])
} }
} }
def isActivePresence() { def isActivePresence() {
// check all the presence sensors, make sure none are present // check all the presence sensors, make sure none are present
def noPresence = presenceSensors.find{it.currentPresence == "present"} == null def noPresence = presenceSensors.find{it.currentPresence == "present"} == null
!noPresence !noPresence
} }
def scheduleCheck() { def scheduleCheck() {
log.debug "scheduled check" log.debug "scheduled check"
def motionState = motionSensor.currentState("motion") def motionState = motionSensor.currentState("motion")
if (motionState.value == "inactive") { if (motionState.value == "inactive") {
def elapsed = now() - motionState.rawDateCreated.time def elapsed = now() - motionState.rawDateCreated.time
def threshold = 1000 * 60 * delayMins - 1000 def threshold = 1000 * 60 * delayMins - 1000
if (elapsed >= threshold) { if (elapsed >= threshold) {
if (!isActivePresence()) { if (!isActivePresence()) {
log.debug "Motion has stayed inactive since last check ($elapsed ms) and no presence: turning lights off" log.debug "Motion has stayed inactive since last check ($elapsed ms) and no presence: turning lights off"
switches.off() switches.off()
} else { } else {
log.debug "Presence is active: do nothing" log.debug "Presence is active: do nothing"
}
} else {
log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): do nothing"
} }
} else {
log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): do nothing"
}
} else { } else {
log.debug "Motion is active: do nothing" log.debug "Motion is active: do nothing"
} }
} }