Created
September 29, 2015 15:34
-
-
Save jimmyjames/3e9281d85342681857b3 to your computer and use it in GitHub Desktop.
first-smartapp.groovy
This file contains 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
definition( | |
name: "My First SmartApp", | |
namespace: "mygithubusername", | |
author: "Peter Gregory", | |
description: "This is my first SmartApp. Woot!", | |
category: "My Apps", | |
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 { | |
section("Turn on when motion detected:") { | |
input "themotion", "capability.motionSensor", required: true, title: "Where?" | |
} | |
section("Turn off when there's been no movement for") { | |
input "minutes", "number", required: true, title: "Minutes?" | |
} | |
section("Turn on this light") { | |
input "theswitch", "capability.switch", required: true | |
} | |
} | |
def installed() { | |
initialize() | |
} | |
def updated() { | |
unsubscribe() | |
initialize() | |
} | |
def initialize() { | |
subscribe(themotion, "motion.active", motionDetectedHandler) | |
subscribe(themotion, "motion.inactive", motionStoppedHandler) | |
} | |
def motionDetectedHandler(evt) { | |
log.debug "motionDetectedHandler called: $evt" | |
theswitch.on() | |
} | |
def motionStoppedHandler(evt) { | |
log.debug "motionStoppedHandler called: $evt" | |
runIn(60 * minutes, checkMotion) | |
} | |
def checkMotion() { | |
log.debug "In checkMotion scheduled method" | |
def motionState = themotion.currentState("motion") | |
if (motionState.value == "inactive") { | |
// get the time elapsed between now and when the motion reported inactive | |
def elapsed = now() - motionState.date.time | |
// elapsed time is in milliseconds, so the threshold must be converted to milliseconds too | |
def threshold = 1000 * 60 * minutes | |
if (elapsed >= threshold) { | |
log.debug "Motion has stayed inactive long enough since last check ($elapsed ms): turning switch off" | |
theswitch.off() | |
} else { | |
log.debug "Motion has not stayed inactive long enough since last check ($elapsed ms): doing nothing" | |
} | |
} else { | |
// Motion active; just log it and do nothing | |
log.debug "Motion is active, do nothing and wait for inactive" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment