Created
August 20, 2013 22:50
-
-
Save danlieberman/6288341 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Off Without Motion | |
* | |
* Author: Dan Lieberman | |
*/ | |
preferences { | |
section("Detect movement on..."){ | |
input "motion1", "capability.motionSensor", title: "Where?" | |
} | |
section("And when there's been no movement for..."){ | |
input "minutes1", "number", title: "Minutes?" | |
} | |
section("Turn off light(s)..."){ | |
input "switches", "capability.switch", multiple: true | |
} | |
} | |
def installed() | |
{ | |
subscribe(motion1, "motion", motionHandler) | |
schedule("0 * * * * ?", "scheduleCheck") | |
} | |
def updated() | |
{ | |
unsubscribe() | |
subscribe(motion1, "motion", motionHandler) | |
unschedule() | |
schedule("0 * * * * ?", "scheduleCheck") | |
} | |
def motionHandler(evt) { | |
log.debug "$evt.name: $evt.value" | |
if (evt.value == "inactive") { | |
if (!state.inactiveAt) { | |
state.inactiveAt = now() | |
} | |
} | |
} | |
def scheduleCheck() { | |
log.debug "schedule check, ts = ${state.inactiveAt}" | |
if (state.inactiveAt) { | |
def elapsed = now() - state.inactiveAt | |
def threshold = 1000 * 60 * (minutes1.toDouble()) | |
if (elapsed >= threshold) { | |
log.debug "turning off lights" | |
switches.off() | |
state.inactiveAt = null | |
} | |
else { | |
log.debug "${elapsed / 1000} sec since motion stopped" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A rudimentary diff to allow the timer to reset if it detects motion between the time of inactivity and the scheduled shutoff time.