Update double-tap.groovy
[smartapps.git] / official / elder-care-daily-routine.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  *  Elder Care
14  *
15  *  Author: SmartThings
16  *  Date: 2013-03-06
17  *
18  *  Stay connected to your loved ones. Get notified if they are not up and moving around 
19  *  by a specified time and/or if they have not opened a cabinet or door according to a set schedule. 
20  */
21
22 definition(
23     name: "Elder Care: Daily Routine",
24     namespace: "smartthings",
25     author: "SmartThings",
26     description: "Stay connected to your loved ones. Get notified if they are not up and moving around by a specified time and/or if they have not opened a cabinet or door according to a set schedule.",
27     category: "Family",
28     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/calendar_contact-accelerometer.png",
29     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/calendar_contact-accelerometer@2x.png"
30 )
31
32 preferences {
33         section("Who are you checking on?") {
34                 input "person1", "text", title: "Name?"
35         }
36         section("If there's no movement (optional, leave blank to not require)...") {
37                 input "motion1", "capability.motionSensor", title: "Where?", required: false
38         }
39         section("or a door or cabinet hasn't been opened (optional, leave blank to not require)...") {
40                 input "contact1", "capability.contactSensor", required: false
41         }
42         section("between these times...") {
43                 input "time0", "time", title: "From what time?"
44                 input "time1", "time", title: "Until what time?"
45         }
46         section("then alert the following people...") {
47                 input("recipients", "contact", title: "People to notify", description: "Send notifications to") {
48                         input "phone1", "phone", title: "Phone number?", required: false
49                 }
50         }
51 }
52
53 def installed() {
54         log.debug "Installed with settings: ${settings}"
55         schedule(time1, "scheduleCheck")
56 }
57
58 def updated() {
59         log.debug "Updated with settings: ${settings}"
60         unsubscribe() //TODO no longer subscribe like we used to - clean this up after all apps updated
61         unschedule()
62         schedule(time1, "scheduleCheck")
63 }
64
65 def scheduleCheck()
66 {
67         if(noRecentContact() && noRecentMotion()) {
68                 def person = person1 ?: "your elder"
69                 def msg = "Alert! There has been no activity at ${person}'s place ${timePhrase}"
70                 log.debug msg
71
72                 if (location.contactBookEnabled) {
73                         sendNotificationToContacts(msg, recipients)
74                 }
75                 else {
76                         if (phone1) {
77                                 sendSms(phone1, msg)
78                         } else {
79                                 sendPush(msg)
80                         }
81                 }
82         } else {
83                 log.debug "There has been activity ${timePhrase}, not sending alert"
84         }
85 }
86
87 private noRecentMotion()
88 {
89         if(motion1) {
90                 def motionEvents = motion1.eventsSince(sinceTime)
91                 log.trace "Found ${motionEvents?.size() ?: 0} motion events"
92                 if (motionEvents.find { it.value == "active" }) {
93                         log.debug "There have been recent 'active' events"
94                         return false
95                 } else {
96                         log.debug "There have not been any recent 'active' events"
97                         return true
98                 }
99         } else {
100                 log.debug "Motion sensor not enabled"
101                 return true
102         }
103 }
104
105 private noRecentContact()
106 {
107         if(contact1) {
108                 def contactEvents = contact1.eventsSince(sinceTime)
109                 log.trace "Found ${contactEvents?.size() ?: 0} door events"
110                 if (contactEvents.find { it.value == "open" }) {
111                         log.debug "There have been recent 'open' events"
112                         return false
113                 } else {
114                         log.debug "There have not been any recent 'open' events"
115                         return true
116                 }
117         } else {
118                 log.debug "Contact sensor not enabled"
119                 return true
120         }
121 }
122
123 private getSinceTime() {
124         if (time0) {
125                 return timeToday(time0, location?.timeZone)
126         }
127         else {
128                 return new Date(now() - 21600000)
129         }
130 }
131
132 private getTimePhrase() {
133         def interval = now() - sinceTime.time
134         if (interval < 3600000) {
135                 return "in the past ${Math.round(interval/60000)} minutes"
136         }
137         else if (interval < 7200000) {
138                 return "in the past hour"
139         }
140         else {
141                 return "in the past ${Math.round(interval/3600000)} hours"
142         }
143 }