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.
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.
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.",
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"
33 section("Who are you checking on?") {
34 input "person1", "text", title: "Name?"
36 section("If there's no movement (optional, leave blank to not require)...") {
37 input "motion1", "capability.motionSensor", title: "Where?", required: false
39 section("or a door or cabinet hasn't been opened (optional, leave blank to not require)...") {
40 input "contact1", "capability.contactSensor", required: false
42 section("between these times...") {
43 input "time0", "time", title: "From what time?"
44 input "time1", "time", title: "Until what time?"
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
54 log.debug "Installed with settings: ${settings}"
55 schedule(time1, "scheduleCheck")
59 log.debug "Updated with settings: ${settings}"
60 unsubscribe() //TODO no longer subscribe like we used to - clean this up after all apps updated
62 schedule(time1, "scheduleCheck")
67 if(noRecentContact() && noRecentMotion()) {
68 def person = person1 ?: "your elder"
69 def msg = "Alert! There has been no activity at ${person}'s place ${timePhrase}"
72 if (location.contactBookEnabled) {
73 sendNotificationToContacts(msg, recipients)
83 log.debug "There has been activity ${timePhrase}, not sending alert"
87 private noRecentMotion()
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"
96 log.debug "There have not been any recent 'active' events"
100 log.debug "Motion sensor not enabled"
105 private noRecentContact()
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"
114 log.debug "There have not been any recent 'open' events"
118 log.debug "Contact sensor not enabled"
123 private getSinceTime() {
125 return timeToday(time0, location?.timeZone)
128 return new Date(now() - 21600000)
132 private getTimePhrase() {
133 def interval = now() - sinceTime.time
134 if (interval < 3600000) {
135 return "in the past ${Math.round(interval/60000)} minutes"
137 else if (interval < 7200000) {
138 return "in the past hour"
141 return "in the past ${Math.round(interval/3600000)} hours"