From c5d07989355b04e6a2987c1e505dcbdc9f57437a Mon Sep 17 00:00:00 2001 From: Steven Short Date: Sun, 18 Jun 2017 13:43:02 -0700 Subject: [PATCH] MSA-2042: A sensor to see if something has been open too long, and then flip a switch (usually simulated) --- .../left-it-open-set-switch.groovy | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 smartapps/smartthings/left-it-open-set-switch.src/left-it-open-set-switch.groovy diff --git a/smartapps/smartthings/left-it-open-set-switch.src/left-it-open-set-switch.groovy b/smartapps/smartthings/left-it-open-set-switch.src/left-it-open-set-switch.groovy new file mode 100644 index 0000000..0516f21 --- /dev/null +++ b/smartapps/smartthings/left-it-open-set-switch.src/left-it-open-set-switch.groovy @@ -0,0 +1,97 @@ +/** + * Copyright 2015 SmartThings + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License + * for the specific language governing permissions and limitations under the License. + * + * Left It Open + * + * Author: SmartThings + * Date: 2013-05-09 + */ +definition( + name: "Left It Open (Set switch)", + namespace: "smartthings", + author: "SmartThings", + description: "Turns on a switch if you have left a door or window open longer that a specified amount of time. Turns if off when closed", + category: "Convenience", + iconUrl: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/bon-voyage.png", + iconX2Url: "https://s3.amazonaws.com/smartapp-icons/ModeMagic/bon-voyage%402x.png" +) + +preferences { + + section("Monitor this door or window") { + input "contact", "capability.contactSensor" + } + section("And notify me if it's open for more than this many minutes (default 10)") { + input "openThreshold", "number", description: "Number of minutes", required: false + } + section("Turn on/off a switch... (usually a simulated one)") { + input "switch1", "capability.switch" + } +} + +def installed() { + log.trace "installed()" + subscribe() +} + +def updated() { + log.trace "updated()" + unsubscribe() + subscribe() +} + +def subscribe() { + subscribe(contact, "contact.open", doorOpen) + subscribe(contact, "contact.closed", doorClosed) +} + +def doorOpen(evt) +{ + log.trace "doorOpen($evt.name: $evt.value)" + def t0 = now() + def delay = (openThreshold != null && openThreshold != "") ? openThreshold * 60 : 600 + runIn(delay, doorOpenTooLong, [overwrite: false]) + log.debug "scheduled doorOpenTooLong in ${now() - t0} msec" +} + +def doorClosed(evt) +{ + log.trace "doorClosed($evt.name: $evt.value)" + switch1.off() +} + +def doorOpenTooLong() { + def contactState = contact.currentState("contact") + def freq = (frequency != null && frequency != "") ? frequency * 60 : 600 + + if (contactState.value == "open") { + def elapsed = now() - contactState.rawDateCreated.time + def threshold = ((openThreshold != null && openThreshold != "") ? openThreshold * 60000 : 60000) - 1000 + if (elapsed >= threshold) { + log.debug "Contact has stayed open long enough since last check ($elapsed ms): calling sendMessage()" + sendMessage() + runIn(freq, doorOpenTooLong, [overwrite: false]) + } else { + log.debug "Contact has not stayed open long enough since last check ($elapsed ms): doing nothing" + } + } else { + log.warn "doorOpenTooLong() called but contact is closed: doing nothing" + } +} + +void sendMessage() +{ + def minutes = (openThreshold != null && openThreshold != "") ? openThreshold : 10 + def msg = "${contact.displayName} has been left open for ${minutes} minutes." + log.info msg + switch1.on() +} \ No newline at end of file