Taking a Nap with SmartThings

04 Nov Taking a Nap with SmartThings

So @DigitalM0nkey had a question in the forums today about code examples for taking a simple nap in SmartThings and having it turn lights off at the beginning of the nap and turning the lights back on at the end of the nap. 

Here is what I came up with as an example for him 

 

/**
 * Nap Time
 *
 * Copyright 2014 Brian Critchlow
 *
 * 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.
 *
 */
definition(
 name: "Nap Time",
 namespace: "docwisdom",
 author: "Brian Critchlow",
 description: "Turns off selected switches for a duration of time and turns them back on after the nap. ",
 category: "Health & Wellness",
 iconUrl: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience.png",
 iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png",
 iconX3Url: "https://s3.amazonaws.com/smartapp-icons/Convenience/Cat-Convenience@2x.png")

preferences {
 section("Nap for:"){
 input "time1", "number", title: "Minutes?", required: true
 }
 section("And control:"){
 input "switches", "capability.switch", title: "These lights", multiple: true
 }
}

def installed() {
 log.debug "Installed with settings: ${settings}"
 initialize()
}

def updated() {
 log.debug "Updated with settings: ${settings}"
 unschedule()
 initialize()

}

def scheduleCheck()
{
 log.debug "lights on"
 switches.on()
 unschedule("scheduleCheck")
}

def initialize() {
 log.debug "lights off"
 switches.off()
 def seconds = time1 * 60
 log.debug "scheduled to run in ${seconds} seconds"
 runIn(seconds, "scheduleCheck")
}

No Comments

Post A Comment