Created
September 22, 2015 14:25
-
-
Save jimmyjames/7a36e48a0c6dcc280ee3 to your computer and use it in GitHub Desktop.
Simple SmartApp that turns a light on with motion, and off when no motion has been detected for a certain time period.
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
/** | |
* Text Me When There's Motion | |
* | |
* Author: SmartThings | |
*/ | |
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