From 5c13794cf83f0a6c1f69babee3e4b49d8f21723c Mon Sep 17 00:00:00 2001 From: Kuldip Date: Sun, 13 Mar 2016 21:45:40 -0500 Subject: [PATCH] MSA-958: This module controls light bulbs along with door open/close sensors. If a light bulb is already on when a door opens, it stays on. Else, it will switch off after specified interval. --- .../smart-timer.src/smart-timer.groovy | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 smartapps/smartthings/smart-timer.src/smart-timer.groovy diff --git a/smartapps/smartthings/smart-timer.src/smart-timer.groovy b/smartapps/smartthings/smart-timer.src/smart-timer.groovy new file mode 100644 index 0000000..b9c8473 --- /dev/null +++ b/smartapps/smartthings/smart-timer.src/smart-timer.groovy @@ -0,0 +1,62 @@ +/** + * 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. + * + * CoolDeep + * + * Author: CoolDeep + */ + +definition( + name: "Smart Timer", + namespace: "smartthings", + author: "CoolDeep", + description: "If a light/device is already on, leave it on. Else, if a light/device switches on on a trigger, switch off in a given time", + category: "My Apps", + iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png", + iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience%402x.png" +) + +preferences { + section("When a sesonsor triggers(turns on)...") { + input "mySwitch", "capability.switch", title: "Switches", multiple: true, required: true + input "mySensor", "capability.contactSensor", title: "Sensors", multiple: true, required: true + } + section("Turn it off after how many minutes?") { + input "minutesLater", "decimal", title: "When?" + } +} + +def installed() { + log.debug "Installed with settings: ${settings}" + subscribe(mySensor, "contact.opened", myHandler) +} + +def updated() { + log.debug "Updated with settings: ${settings}" + unsubscribe() + subscribe(mySensor, "contact.open", myHandler) +} + +def myHandler(evt) { + def lightVal = mySwitch.currentSwitch + log.debug ">> TheSwitch current value turned ${lightVal}" + + if (lightVal.contains("off")) { + def delay = minutesLater * 60 + log.debug ">>> Turning off in ${minutesLater} minutes (${delay}seconds)" + runIn(delay, turnOffSwitch) + } +} + +def turnOffSwitch() { + mySwitch.off() +} \ No newline at end of file