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.
19 name: "Medicine Reminder",
20 namespace: "smartthings",
21 author: "SmartThings",
22 description: "Set up a reminder so that if you forget to take your medicine (determined by whether a cabinet or drawer has been opened) by specified time you get a notification or text message.",
23 category: "Health & Wellness",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/text_contact.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/text_contact@2x.png"
29 section("Choose your medicine cabinet..."){
30 input "cabinet1", "capability.contactSensor", title: "Where?"
32 section("Take my medicine at..."){
33 input "time1", "time", title: "Time 1"
34 input "time2", "time", title: "Time 2", required: false
35 input "time3", "time", title: "Time 3", required: false
36 input "time4", "time", title: "Time 4", required: false
38 section("I forget send me a notification and/or text message..."){
39 input("recipients", "contact", title: "Send notifications to") {
40 input "sendPush", "enum", title: "Push Notification", required: false, options: ["Yes", "No"]
41 input "phone1", "phone", title: "Phone Number", required: false
44 section("Time window (optional, defaults to plus or minus 15 minutes") {
45 input "timeWindow", "decimal", title: "Minutes", required: false
61 def window = timeWindowMsec
62 [time1, time2, time3, time4].eachWithIndex {time, index ->
64 def endTime = new Date(timeToday(time, location?.timeZone).time + window)
65 log.debug "Scheduling check at $endTime"
66 //runDaily(endTime, "scheduleCheck${index}")
69 schedule(endTime, scheduleCheck0)
72 schedule(endTime, scheduleCheck1)
75 schedule(endTime, scheduleCheck2)
78 schedule(endTime, scheduleCheck3)
85 def scheduleCheck0() { scheduleCheck() }
86 def scheduleCheck1() { scheduleCheck() }
87 def scheduleCheck2() { scheduleCheck() }
88 def scheduleCheck3() { scheduleCheck() }
92 log.debug "scheduleCheck"
93 def t0 = new Date(now() - (2 * timeWindowMsec))
95 def cabinetOpened = cabinet1.eventsBetween(t0, t1).find{it.name == "contact" && it.value == "open"}
96 log.trace "Looking for events between $t0 and $t1: $cabinetOpened"
99 log.trace "Medicine cabinet was opened since $midnight, no notification required"
101 log.trace "Medicine cabinet was not opened since $midnight, sending notification"
106 private sendMessage() {
107 def msg = "Please remember to take your medicine"
109 if (location.contactBookEnabled) {
110 sendNotificationToContacts(msg, recipients)
116 if (sendPush == "Yes") {
122 def getTimeWindowMsec() {
123 (timeWindow ?: 15) * 60000 as Long