@Shiznorak
Mike,
Please bear in mind all the usual disclaimers (if your house/smartthings blows up I will not be held responsible etc… etc…)
Use at your own risk
There may be a better way to code this (and I would be surprised if there wasn’t) but here it is:
/**
* Cycle Switch Timer
*
* Copyright 2017 Andrew Parker
*
* Code for days of the week is not mine but I can't find out where it came from
* If it's yours then please contact me so I can properly credit you
*
* 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: "Cycle Switch On Timer",
namespace: "Cobra",
author: "Andrew Parker",
description: "Sets a switch on for a specified time then off for a specified time then on again... ",
category: "My Apps",
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("Which switch to enable/disable App") {
input(name:"switch1", type: "capability.switch", required: false, multiple: true)
}
section("Which days to run") {
input "days", "enum", title: "Select Days of the Week", required: true, multiple: true, options: ["Monday": "Monday", "Tuesday": "Tuesday", "Wednesday": "Wednesday", "Thursday": "Thursday", "Friday": "Friday", "Saturday": "Saturday", "Sunday": "Sunday"]
}
section("Which switch to cycle...") {
input(name:"switch2", type: "capability.switch", required: true, multiple: true)
}
section("How long to stay on") {
input(name:"ondelay", type:"enum", title: "Minutes", required: true, options: [1,2,3,4,5,10,15,20,30,40,50,60])
}
section("How long to stay off") {
input(name:"offdelay", type:"enum", title: "Minutes", required: true, options: [1,2,3,4,5,6,7,8,9,10,15,20,30,40,50,60])
}
}
def installed() {
log.debug "Installed with settings: ${settings}"
initialize()
}
def updated() {
log.debug "Updated with settings: ${settings}"
unsubscribe()
initialize()
}
def initialize() {
subscribe(switch1, "switch", switch1Handler)
}
// Config & actions
def switch1Handler(evt){
state.currS1 = evt.value
log.trace " $switch1 is $state.currS1"
if (state.currS1 != "off" ) {
runswitchOnNow()
}
if (state.currS1 != "on" ) {
switchOff()
}
}
def runswitchOnNow () {
if (state.currS1 != "off" ) {
def df = new java.text.SimpleDateFormat("EEEE")
df.setTimeZone(location.timeZone)
def day = df.format(new Date())
def dayCheck = days.contains(day)
if (dayCheck) {
log.debug "Cycling power on switch(es): $switch2 "
def delay = ondelay
switch2.on()
log.debug "switch: $switch2 is on"
log.debug " Waiting for ${delay} minute(s) before switching off"
if (delay == "1"){
runIn(60, switchOff)}
else if (delay == "2"){
runIn(120, switchOff)}
else if (delay == "3"){
runIn(180, switchOff)}
else if (delay == "4"){
runIn(240, switchOff)}
else if (delay == "5"){
runIn(300, switchOff)}
else if (delay == "10"){
runIn(600, switchOff)}
else if (delay == "15"){
runIn(900, switchOff)}
else if (delay == "20"){
runIn(1200, switchOff)}
else if (delay == "30"){
runIn(1800, switchOff)}
else if (delay == "40"){
runIn(2400, switchOff)}
else if (delay == "50"){
runIn(3000, switchOff)}
else if (delay == "60"){
runIn(3600, switchOff)}
}
else {
log.debug " Not today!"
}
}
}
def switchOff (){
log.debug "Switching off"
switch2.off()
log.trace "$switch2 is now off"
switchbackOn()
}
def switchbackOn () {
if (state.currS1 != "off" ) {
def df = new java.text.SimpleDateFormat("EEEE")
df.setTimeZone(location.timeZone)
def day = df.format(new Date())
def dayCheck1 = days.contains(day)
if (dayCheck1) {
log.debug "Turning on switch(es): $switch2 "
def delay1 = offdelay
log.debug " Waiting for ${delay1} minute(s) before switching on"
if (delay1 == "1"){
runIn(60, switchOn)}
else if (delay1 == "2"){
runIn(120, switchOn)}
else if (delay1 == "3"){
runIn(180, switchOn)}
else if (delay1 == "4"){
runIn(240, switchOn)}
else if (delay1 == "5"){
runIn(300, switchOn)}
else if (delay1 == "6"){
runIn(360, switchOn)}
else if (delay1 == "7"){
runIn(420, switchOn)}
else if (delay1 == "8"){
runIn(480, switchOn)}
else if (delay1 == "9"){
runIn(540, switchOn)}
else if (delay1 == "10"){
runIn(600, switchOn)}
else if (delay1 == "15"){
runIn(900, switchOn)}
else if (delay1 == "20"){
runIn(1200, switchOn)}
else if (delay1 == "30"){
runIn(1800, switchOn)}
else if (delay1 == "40"){
runIn(2400, switchOn)}
else if (delay1 == "50"){
runIn(3000, switchOn)}
else if (delay1 == "60"){
runIn(3600, switchOn)}
else {
log.debug " Not today!"
}
}
}
}
def switchOn (){
log.trace "Switching back on"
runswitchOnNow ()
}
Oh… also added which days of the week to run…
I have left quite a lot of debug logging in there so you can see how it works for you.
Once you have confirmed it works ok for you then we can remove all that.
I can also easily alter the selected time slots if you need a different one.