Update once-a-day.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                                 // Commenting this out since there could be a null value error
76                                 log.debug "skipping notification of arrival of Person"
77                         }
78                         else {
79                                 def message = "${person.displayName} arrived at home, changing mode to '${newMode}'"
80                                 send(message)
81                                 setLocationMode(newMode)
82                         }
83                 }
84         }
85         else {
86                 log.debug "mode is the same, not evaluating"
87         }
88 }
89
90 private getPerson(evt)
91 {
92         people.find{evt.deviceId == it.id}
93 }
94
95 private send(msg) {
96     if (location.contactBookEnabled) {
97         sendNotificationToContacts(msg, recipients)
98     }
99     else {
100         if (sendPushMessage != "No") {
101             log.debug("sending push message")
102             sendPush(msg)
103         }
104
105         if (phone) {
106             log.debug("sending text message")
107             sendSms(phone, msg)
108         }
109     }
110 }