From 5b7a7097b84a455191eff0eb0964a0c48667ee4b Mon Sep 17 00:00:00 2001 From: Aaron Miller Date: Thu, 20 Apr 2017 13:16:08 -0500 Subject: [PATCH] [DVCSMP-2595] Set scheduled executions for Lights off with no motion SA to overwrite --- ...hts-off-with-no-motion-and-presence.groovy | 82 +++++++++---------- 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/smartapps/naissan/lights-off-with-no-motion-and-presence.src/lights-off-with-no-motion-and-presence.groovy b/smartapps/naissan/lights-off-with-no-motion-and-presence.src/lights-off-with-no-motion-and-presence.groovy index bf739f1..abfe38d 100644 --- a/smartapps/naissan/lights-off-with-no-motion-and-presence.src/lights-off-with-no-motion-and-presence.groovy +++ b/smartapps/naissan/lights-off-with-no-motion-and-presence.src/lights-off-with-no-motion-and-presence.groovy @@ -15,66 +15,66 @@ definition( ) preferences { - section("Light switches to turn off") { - input "switches", "capability.switch", title: "Choose light switches", multiple: true - } - section("Turn off when there is no motion and presence") { - input "motionSensor", "capability.motionSensor", title: "Choose motion sensor" - input "presenceSensors", "capability.presenceSensor", title: "Choose presence sensors", multiple: true - } - section("Delay before turning off") { - input "delayMins", "number", title: "Minutes of inactivity?" - } + section("Light switches to turn off") { + input "switches", "capability.switch", title: "Choose light switches", multiple: true + } + section("Turn off when there is no motion and presence") { + input "motionSensor", "capability.motionSensor", title: "Choose motion sensor" + input "presenceSensors", "capability.presenceSensor", title: "Choose presence sensors", multiple: true + } + section("Delay before turning off") { + input "delayMins", "number", title: "Minutes of inactivity?" + } } def installed() { - subscribe(motionSensor, "motion", motionHandler) - subscribe(presenceSensors, "presence", presenceHandler) + subscribe(motionSensor, "motion", motionHandler) + subscribe(presenceSensors, "presence", presenceHandler) } def updated() { - unsubscribe() - subscribe(motionSensor, "motion", motionHandler) - subscribe(presenceSensors, "presence", presenceHandler) + unsubscribe() + subscribe(motionSensor, "motion", motionHandler) + subscribe(presenceSensors, "presence", presenceHandler) } def motionHandler(evt) { - log.debug "handler $evt.name: $evt.value" - if (evt.value == "inactive") { - runIn(delayMins * 60, scheduleCheck, [overwrite: false]) - } + log.debug "handler $evt.name: $evt.value" + if (evt.value == "inactive") { + runIn(delayMins * 60, scheduleCheck, [overwrite: true]) + } } def presenceHandler(evt) { - log.debug "handler $evt.name: $evt.value" - if (evt.value == "not present") { - runIn(delayMins * 60, scheduleCheck, [overwrite: false]) - } + log.debug "handler $evt.name: $evt.value" + if (evt.value == "not present") { + runIn(delayMins * 60, scheduleCheck, [overwrite: true]) + } } def isActivePresence() { - // check all the presence sensors, make sure none are present - def noPresence = presenceSensors.find{it.currentPresence == "present"} == null - !noPresence + // check all the presence sensors, make sure none are present + def noPresence = presenceSensors.find{it.currentPresence == "present"} == null + !noPresence } def scheduleCheck() { - log.debug "scheduled check" - def motionState = motionSensor.currentState("motion") + log.debug "scheduled check" + def motionState = motionSensor.currentState("motion") if (motionState.value == "inactive") { - def elapsed = now() - motionState.rawDateCreated.time - def threshold = 1000 * 60 * delayMins - 1000 - if (elapsed >= threshold) { - if (!isActivePresence()) { - log.debug "Motion has stayed inactive since last check ($elapsed ms) and no presence: turning lights off" - switches.off() - } else { - log.debug "Presence is active: do nothing" - } - } else { - log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): do nothing" + def elapsed = now() - motionState.rawDateCreated.time + def threshold = 1000 * 60 * delayMins - 1000 + if (elapsed >= threshold) { + if (!isActivePresence()) { + log.debug "Motion has stayed inactive since last check ($elapsed ms) and no presence: turning lights off" + switches.off() + } else { + 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 is active: do nothing" + log.debug "Motion is active: do nothing" } -} \ No newline at end of file +}