Cycle Timer

I’ve never written code and I can’t find a product/app to do what I want.

I’m needing an app to control a GE Zwave outdoor switch on and off at certain intervals (aquaponics system). For example I want the switch to be on for fifteen minutes (run the water pump) and than turn off for five minutes (allowing tank to drain) and have that repeat 24/7. I’m reading into CoRE but there’s a notice on how it sometimes wacks out and I don’t want to put my project at risk.

Has anyone written an app like this before that could help out?

Core is at least as reliable as anything else on SmartThings, and probably a little more so. So Core isn’t likely to be the problem. SmartThings itself, on the other hand, does have some reliability issues, so if this is an absolutely critical use case for you, it’s probably not a good match to SmartThings to begin with.

https://www.smartthings.com/guidelines

That said, if you feel that SmartThings would meet your requirements, you can do this without Core by daisychaining a virtual switch but at this point your best option is going to be core or webcore because they will let you set up additional conditions like time of day much more easily. :sunglasses:

1 Like

I did it with core. My issue I wanted less than 60 seconds on and it was not reliably working. It should work fine for your time.

Ended up buying a stand alone timer :slight_smile:

2 Likes

Good point. Anything less than one minute is not guaranteed to work with SmartThings. In fact most of the official features won’t even let you set up something with less than a minute change. Core Will let you set up a schedule that changes based on seconds, but it doesn’t guarantee that it will work. That has to do with the underlying technology of zwave and zigbee mesh, not with SmartThings itself. But cycles of a minute or more should be OK, although they’re not guaranteed to be precise, it might be a minute and two seconds one time and then a minute and four seconds the next.

1 Like

@Shiznorak
Pm me and I’ll try to help out with an app
I wrote an app for someone to power cycle a device that needs a reboot every day. He has been using it to successfully switch off then back on again with a few optional time settings.
I use it for a 15 second off time to reboot some hardware that causes problems.
The timing has been reliable since I installed it
This could probably be adapted for your use.

Alternatively core will, as everyone says, be able to do the same

1 Like

If I can figure out how to private message I would!

Thanks for all the help! I’ve been using smartthings for a few months and if that is the lease stable variable in this equation than I am set! I don’t want to daisy chain because that would be about 200 events, which is crazy.

Cobra I will private message you as soon as I figure out how…

1 Like

If you’re very new in the forum you may not have privileges like PM yet. Like some posts, do some other stuff to interact with the forum interface and it should automatically upgrade you.

2 Likes

@Shiznorak
Mike
I have been working on an app that should help.
I think this is probably what you are looking for:

  1. Select on/off switch (Can be virtual)
  2. Select switch to be controlled
  3. Select On duration
  4. Select Off duration

This just loops on/off
I have been testing this for about 24hrs with an ‘on’ of 15 mins and an ‘off’ of 5 mins - It’s been reliable during all that time according to the logs

If this is what you are looking for then I can post the code for you to try

2 Likes

Yes please!

@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 :slight_smile:

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.

1 Like

Ha… it’s still running on two virtual switches… :slight_smile:

185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:51:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:51:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:51:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:51:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:46:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:46:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:46:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:46:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:31:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:31:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:31:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:31:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:26:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:26:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:26:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:26:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:11:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:11:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:11:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:11:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:06:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:06:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:06:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 11:06:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:51:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:51:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:51:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:51:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:46:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:46:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:46:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:46:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:31:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:31:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:31:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:31:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:26:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:26:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:26:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:26:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:11:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:11:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:11:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:11:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:06:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:06:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:06:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 10:06:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:51:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:51:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:51:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:51:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:46:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:46:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:46:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:46:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:31:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:31:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:31:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:31:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:26:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:26:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:26:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:26:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:11:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:11:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:11:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:11:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:06:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:06:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:06:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 9:06:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:51:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:51:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:51:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:51:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:46:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:46:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:46:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:46:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:31:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:31:55 PM: debug switch: [test 2] is on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:31:55 PM: debug Cycling power on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:31:55 PM: trace Switching back on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:26:55 PM: debug Waiting for 5 minute(s) before switching on
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:26:55 PM: debug Turning on switch(es): [test 2]
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:26:55 PM: trace [test 2] is now off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:26:55 PM: debug Switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:11:55 PM: debug Waiting for 15 minute(s) before switching off
185e800f-70dd-4426-bc5d-4dac5ff0f1bd 8:11:55 PM: debug switch: [test 2] is on

Can’t leave all that logging turned on… :slight_smile:

I’m a complete newbie apparently.

What did I do wrong? I copy and pasted the code in the developer login area and published it to my apps but I get this screen when I go into the app itself?

Not sure how new you are.
Did you copy ALL the code I posted (it’s easy to miss a bit)?

This might help if you are not used to adding smartapps:

I get the same exact error - page.

I’m not a coder - but have cut and pasted many as well as from Github.

Double checked - have the top line and bottom so the C&P was ok 208 lines of code fwiw.

Guys
Maybe it was me who didn’t copy paste correctly
It’s almost midnight here (UK) so I’ll check in the morning

2 Likes

It works in the simulator, my lightbulbs are pulsing right now but when I try to use it on the app it is when I get the error.

Edit: When I publish the app it and add it on my device it gives me two separate smartapp icons instead of one.

Mike
Are you using android or ois?
I do all my testing/setup in ios and wonder if they treat things differently

I have uploaded the code to a different website in case the forum is not presenting it correctly:

http://securendpoint.com/smartthings/cycletimer.txt

I copied that code from the website into the IDE and published without problems.
I wonder if there are any issues with the platform at the moment?
I’m in the UK and on the European shard.

My laptop is a Mac and my phone is an Android. I can have my parents try to add the app on the hub/iPhones and see if it works for them. I will test out what you just sent when I get home from work today.

I wonder if I can just control it from my Mac for now as a “simulator” until we figure out how to fix it for Android :slight_smile:

Mike
I really don’t know what this is, I have never had a problem before but maybe it’s because I wrote stuff that people use on ios?
I really only write stuff for my own use and obviously I am using ios