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.
20 namespace: "smartthings",
21 author: "SmartThings",
22 description: "Notifies you when you have left a door or window open longer that a specified amount of time.",
23 category: "Convenience",
24 iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/bon-voyage.png",
25 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/bon-voyage%402x.png"
30 section("Monitor this door or window") {
31 input "contact", "capability.contactSensor"
33 section("And notify me if it's open for more than this many minutes (default 10)") {
34 input "openThreshold", "number", description: "Number of minutes", required: false
36 section("Delay between notifications (default 10 minutes") {
37 input "frequency", "number", title: "Number of minutes", description: "", required: false
39 section("Via text message at this number (or via push notification if not specified") {
40 input("recipients", "contact", title: "Send notifications to") {
41 input "phone", "phone", title: "Phone number (optional)", required: false
47 log.trace "installed()"
58 subscribe(contact, "contact.open", doorOpen)
59 subscribe(contact, "contact.closed", doorClosed)
64 log.trace "doorOpen($evt.name: $evt.value)"
66 def delay = (openThreshold != null && openThreshold != "") ? openThreshold * 60 : 600
67 runIn(delay, doorOpenTooLong, [overwrite: false])
68 log.debug "scheduled doorOpenTooLong in ${now() - t0} msec"
73 log.trace "doorClosed($evt.name: $evt.value)"
76 def doorOpenTooLong() {
77 def contactState = contact.currentState("contact")
78 def freq = (frequency != null && frequency != "") ? frequency * 60 : 600
80 if (contactState.value == "open") {
81 def elapsed = now() - contactState.rawDateCreated.time
82 def threshold = ((openThreshold != null && openThreshold != "") ? openThreshold * 60000 : 60000) - 1000
83 if (elapsed >= threshold) {
84 log.debug "Contact has stayed open long enough since last check ($elapsed ms): calling sendMessage()"
86 runIn(freq, doorOpenTooLong, [overwrite: false])
88 log.debug "Contact has not stayed open long enough since last check ($elapsed ms): doing nothing"
91 log.warn "doorOpenTooLong() called but contact is closed: doing nothing"
97 def minutes = (openThreshold != null && openThreshold != "") ? openThreshold : 10
98 def msg = "${contact.displayName} has been left open for ${minutes} minutes."
100 if (location.contactBookEnabled) {
101 sendNotificationToContacts(msg, recipients)