Skip to content

Instantly share code, notes, and snippets.

@aurman
Created September 18, 2013 22:00
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 aurman/6616375 to your computer and use it in GitHub Desktop.
Save aurman/6616375 to your computer and use it in GitHub Desktop.
/****
* Mode Change AWAY
* Bon Voyage with some minor additions
*
* Author: SmartThings
* Date: 2013-03-07
* Mods by Carl Aydelotte
*
* Monitors a set of presence detectors and triggers a mode change when everyone has left plus an indicator light to display when
* the mode is set to AWAY
*
****/
preferences {
section( "When all of these people leave home" ) {
input "people", "capability.presenceSensor", multiple: true
}
section( "Change to this mode" ) {
input "newMode", "mode", title: "Mode?"
}
section( "False alarm threshold in minutes (default = 10)" ) {
input "falseAlarmThreshold", "decimal", title: "Number of minutes", required: false
}
section( "Notifications" ) {
input "sendPushMessage", "enum", title: "Send a push notification?", metadata:[values:["Yes","No"]], required: false
input "phone", "phone", title: "Send a Text Message?", required: false
}
section( "Turn ON these Mode Indicators" ) {
input "switches", "capability.switches", multiple: true
}
section( "And turn OFF these devices just in case" ) {
input "switchoff", "capability.switches", multiple: true
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
subscribe(people, "presence", presence)
}
def updated() {
log.debug "Updated with settings: ${settings}"
log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
unsubscribe()
subscribe(people, "presence", presence)
}
def presence(evt)
{
log.debug "evt.name: $evt.value"
if (evt.value == "not present") {
if (location.mode != newMode) {
log.debug "checking if everyone is away"
if (everyoneIsAway()) {
log.debug "starting sequence"
def delay = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? falseAlarmThreshold * 60 : 10 * 60
runIn(delay, "takeAction")
}
}
else {
log.debug "mode is the same, not evaluating"
}
}
else {
log.debug "canceling"
unschedule("takeAction")
}
}
def takeAction()
{
// TODO -- uncomment when app label is available
//def message = "${app.label} changed your mode to '${newMode}' because everyone left home"
def message = "SmartThings changed your mode to '${newMode}' because everyone left home"
log.info message
send(message)
setLocationMode(newMode)
log.debug "Mode Indicator is ON"
switches?.on()
switchoff?.off()
unschedule("takeAction") // Temporary work-around to scheduling bug
}
private everyoneIsAway()
{
def result = true
for (person in people) {
if (person.currentPresence == "present") {
result = false
break
}
}
log.debug "everyoneIsAway: $result"
return result
}
private send(msg) {
if ( sendPushMessage != "No" ) {
log.debug( "sending push message" )
sendPush( msg )
}
if ( phone ) {
log.debug( "sending text message" )
sendSms( phone, msg )
}
log.debug msg
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment