Update Hue-Party-Mode.groovy
[smartapps.git] / official / elder-care-slip-fall.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: Slip & Fall
14  *
15  *  Author: SmartThings
16  *  Date: 2013-04-07
17  * 
18  */
19
20 definition(
21     name: "Elder Care: Slip & Fall",
22     namespace: "smartthings",
23     author: "SmartThings",
24     description: "Monitors motion sensors in bedroom and bathroom during the night and detects if occupant does not return from the bathroom after a specified period of time.",
25     category: "Family",
26     iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/calendar_contact-accelerometer.png",
27     iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/calendar_contact-accelerometer@2x.png"
28 )
29
30 preferences {
31         section("Bedroom motion detector(s)") {
32                 input "bedroomMotion", "capability.motionSensor", multiple: true
33         }
34         section("Bathroom motion detector") {
35                 input "bathroomMotion", "capability.motionSensor"
36         }
37     section("Active between these times") {
38         input "startTime", "time", title: "Start Time"
39         input "stopTime", "time", title: "Stop Time"
40     }
41     section("Send message when no return within specified time period") {
42         input "warnMessage", "text", title: "Warning Message"
43         input "threshold", "number", title: "Minutes"
44     }
45     section("To these contacts") {
46                 input("recipients", "contact", title: "Recipients", description: "Send notifications to") {
47                         input "phone1", "phone", required: false
48                         input "phone2", "phone", required: false
49                         input "phone3", "phone", required: false
50                 }
51     }
52 }
53
54 def installed() {
55         log.debug "Installed with settings: ${settings}"
56         initialize()
57 }
58
59 def updated() {
60         log.debug "Updated with settings: ${settings}"
61
62         unsubscribe()
63         initialize()
64 }
65
66 def initialize() {
67         state.active = 0
68         subscribe(bedroomMotion, "motion.active", bedroomActive)
69     subscribe(bathroomMotion, "motion.active", bathroomActive)
70 }
71
72 def bedroomActive(evt) {
73         def start = timeToday(startTime, location?.timeZone)
74     def stop = timeToday(stopTime, location?.timeZone)
75     def now = new Date()
76     log.debug "bedroomActive, status: $state.ststus, start: $start, stop: $stop, now: $now"
77     if (state.status == "waiting") {
78         log.debug "motion detected in bedroom, disarming"
79         unschedule("sendMessage")
80         state.status = null
81     }
82     else {
83         if (start.before(now) && stop.after(now)) {
84             log.debug "motion in bedroom, look for bathroom motion"
85             state.status = "pending"
86         }
87         else {
88             log.debug "Not in time window"
89         }
90     }
91 }
92
93 def bathroomActive(evt) {
94         log.debug "bathroomActive, status: $state.status"
95         if (state.status == "pending") {
96         def delay = threshold.toInteger() * 60
97         state.status = "waiting"
98         log.debug "runIn($delay)"
99         runIn(delay, sendMessage)
100     }
101 }
102
103 def sendMessage() {
104         log.debug "sendMessage"
105         def msg = warnMessage
106     log.info msg
107
108         if (location.contactBookEnabled) {
109                 sendNotificationToContacts(msg, recipients)
110         }
111         else {
112                 sendPush msg
113                 if (phone1) {
114                         sendSms phone1, msg
115                 }
116                 if (phone2) {
117                         sendSms phone2, msg
118                 }
119                 if (phone3) {
120                         sendSms phone3, msg
121                 }
122         }
123     state.status = null
124 }