2 * Copyright 2015 SmartThings
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed
10 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License
11 * for the specific language governing permissions and limitations under the License.
19 name: "Greetings Earthling",
20 namespace: "smartthings",
21 author: "SmartThings",
22 description: "Monitors a set of presence detectors and triggers a mode change when someone arrives at home.",
23 category: "Mode Magic",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/App-LightUpMyWorld@2x.png"
30 section("When one of these people arrive at home") {
31 input "people", "capability.presenceSensor", multiple: true
33 section("Change to this mode") {
34 input "newMode", "mode", title: "Mode?"
36 section("False alarm threshold (defaults to 10 min)") {
37 input "falseAlarmThreshold", "decimal", title: "Number of minutes", required: false
39 section( "Notifications" ) {
40 input("recipients", "contact", title: "Send notifications to") {
41 input "sendPushMessage", "enum", title: "Send a push notification?", options: ["Yes", "No"], required: false
42 input "phone", "phone", title: "Send a Text Message?", required: false
49 log.debug "Installed with settings: ${settings}"
50 // log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
51 subscribe(people, "presence", presence)
55 log.debug "Updated with settings: ${settings}"
56 // log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
58 subscribe(people, "presence", presence)
63 log.debug "evt.name: $evt.value"
64 def threshold = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? (falseAlarmThreshold * 60 * 1000) as Long : 10 * 60 * 1000L
66 if (location.mode != newMode) {
68 def t0 = new Date(now() - threshold)
69 if (evt.value == "present") {
71 def person = getPerson(evt)
72 def recentNotPresent = person.statesSince("presence", t0).find{it.value == "not present"}
73 if (recentNotPresent) {
74 log.debug "skipping notification of arrival of Person because last departure was only ${now() - recentNotPresent.date.time} msec ago"
77 def message = "${person.displayName} arrived at home, changing mode to '${newMode}'"
79 setLocationMode(newMode)
84 log.debug "mode is the same, not evaluating"
88 private getPerson(evt)
90 people.find{evt.deviceId == it.id}
94 if (location.contactBookEnabled) {
95 sendNotificationToContacts(msg, recipients)
98 if (sendPushMessage != "No") {
99 log.debug("sending push message")
104 log.debug("sending text message")