Skip to content

Instantly share code, notes, and snippets.

@danlieberman
Created August 20, 2013 22:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danlieberman/6288341 to your computer and use it in GitHub Desktop.
Save danlieberman/6288341 to your computer and use it in GitHub Desktop.
/**
* 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"
}
}
}
@jnovack
Copy link

jnovack commented Aug 21, 2013

A rudimentary diff to allow the timer to reset if it detects motion between the time of inactivity and the scheduled shutoff time.

41 -    }
41 +    } else {
42 +         state.inactiveAt = null
43 +    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment