Update vacation-lighting-director.groovy
[smartapps.git] / official / greetings-earthling.groovy
1 /**
2  *  Copyright 2015 SmartThings
3  *
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:
6  *
7  *      http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
12  *
13  *  Greetings Earthling
14  *
15  *  Author: SmartThings
16  *  Date: 2013-03-07
17  */
18 definition(
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"
26 )
27
28 preferences {
29
30         section("When one of these people arrive at home") {
31                 input "people", "capability.presenceSensor", multiple: true
32         }
33         section("Change to this mode") {
34                 input "newMode", "mode", title: "Mode?"
35         }
36         section("False alarm threshold (defaults to 10 min)") {
37                 input "falseAlarmThreshold", "decimal", title: "Number of minutes", required: false
38         }
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
43         }
44         }
45
46 }
47
48 def installed() {
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)
52 }
53
54 def updated() {
55         log.debug "Updated with settings: ${settings}"
56         // log.debug "Current mode = ${location.mode}, people = ${people.collect{it.label + ': ' + it.currentPresence}}"
57         unsubscribe()
58         subscribe(people, "presence", presence)
59 }
60
61 def presence(evt)
62 {
63         log.debug "evt.name: $evt.value"
64         def threshold = (falseAlarmThreshold != null && falseAlarmThreshold != "") ? (falseAlarmThreshold * 60 * 1000) as Long : 10 * 60 * 1000L
65
66         if (location.mode != newMode) {
67
68                 def t0 = new Date(now() - threshold)
69                 if (evt.value == "present") {
70
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"
75                         }
76                         else {
77                                 def message = "${person.displayName} arrived at home, changing mode to '${newMode}'"
78                                 send(message)
79                                 setLocationMode(newMode)
80                         }
81                 }
82         }
83         else {
84                 log.debug "mode is the same, not evaluating"
85         }
86 }
87
88 private getPerson(evt)
89 {
90         people.find{evt.deviceId == it.id}
91 }
92
93 private send(msg) {
94     if (location.contactBookEnabled) {
95         sendNotificationToContacts(msg, recipients)
96     }
97     else {
98         if (sendPushMessage != "No") {
99             log.debug("sending push message")
100             sendPush(msg)
101         }
102
103         if (phone) {
104             log.debug("sending text message")
105             sendSms(phone, msg)
106         }
107     }
108 }