Power Cycle Connected Device

Hello everyone, I am a new user to SmartThings and to board. I recently connected Harmony Hubs to my smart home. I am finding that the Hubs something loose the ability to send commands to Roku devices. Based on a response from Logitech, my best option is to unplug the Harmony hub, in essence perform a power cycle on the device.

My goal is to connect the hubs to a Samsung SmartThings plug, so that I have the ability to stop, wait, then restart the Harmony device. But I would like to automate these steps; I was thinking of an app that would turn off the outlet, sleep for a few seconds, then start the outlet again.

Any help the community can provide would be greatly appreciated.

I’m not sure if you’ll have this issue, but when I power off my Harmony Hub it ‘forgets’ the current input position of my TV and audio. I have to manually fix it each time. If that isn’t an issue for you you can use CoRE (to pretty much do anything).

@abrock
I’ve been thinking about knocking up an app to reboot a couple of devices that I have problems with
I suppose now would be a good time to make it as it would be helpful for you too

I’ll get working on it :slight_smile:

Ok… version one… this will just turn a switch on a specified seconds after it has been turned off.
You would use this with ‘smart lighting’ or similar to schedule the turn off

It should only be used with switches that are never manually switched off - Obviously it will just turn back on again! :slight_smile:

/**
 *  Copyright 2017 SecurEndpoint
 *
 *  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.
 *
 *  Power Cycle
 *  V1.0
 *
 *  Turns on a switch after being turned off (after a set number of seconds)
 *
 *  Author: COBRA
 */





definition(
    name: "Power Cycle",
    namespace: "Cobra",
    author: "SecureEndpoint",
    description: "When a switch is turned off, automatically turn it back on after a set number of seconds you specify.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
)

preferences {
	section("When a switch turns off...") {
    
	    input(name:"theSwitch", type: "capability.switch", required: true, multiple: true)
	}
	section("Turn it on how many seconds later?") {
		input(name:"secondsLater", type:"number", title: "Seconds", required: true)
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	subscribe(theSwitch, "switch.on", switchOnHandler, [filterEvents: false])
}

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

	unsubscribe()
	subscribe(theSwitch, "switch.on", switchOnHandler, [filterEvents: false])
}

def switchOnHandler(evt) {
	log.debug "Switch ${theSwitch} turned: ${evt.value}"
	def delay = secondsLater
	log.debug "Turning on in ${secondsLater} seconds (${delay}seconds)"
	runIn(delay, turnOnSwitch)
}

def turnOnSwitch() {
	theSwitch.on()
}

I will look at adding the schedule so that we can just use this app for everything…

1 Like

Thanks for the info @jhamstead! I have not run into the issue you describe with the Hub loosing the input but I will keep that in mind with my experimentation.

I will give CoRE a try eventually!

Wow thanks @Cobra! I am going to give this solution a try this evening. Thank you for your help, I will let you know how it goes.

OK @abrock

Version 1.1 :slight_smile:
This solution is a little bit better and does it all for you.
It’s probably not the tidiest code and I’m sure there is probably a better way of doing this - but it does work!
I have programmed set delays for the ‘Off’ time which you can select but if you need anything different please let me know
I have tested it with mine and I’m using 30 seconds.
I have also tested with multiple switches and that seems ok too.

Manually switching the device should not affect schedule etc however it WILL turn the device on, even if it was off previously

/**
 *  Copyright 2017 SecurEndpoint
 *
 *  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.
 *
 *  Power Cycle
 *  V1.1 - Added scheduling
 *  V1.0 - Initial release
 *
 *  Last updated 05/05/2017
 *  Sets a schedule to turn off a switch,  after a preset number of seconds it will turn back on again - Used to power cycle some equipment daily
 *
 *  Author: COBRA
 */





definition(
    name: "Scheduled Power Cycle",
    namespace: "Cobra",
    author: "SecureEndpoint",
    description: "Schedule a switch to turn off then automatically turn it back on after a set number of seconds you specify.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
)

preferences {
	section("Which switch to cycle...") {
        input(name:"theSwitch", type: "capability.switch", required: true, multiple: true)
	}
	section("Power Cycle at...") {
		input (name: "rebootTime", title: "At what time?", type: "time",  required: true)
	}
    section("How long to stay off") {
		input(name:"secondsdelay", type:"enum", title: "Seconds", required: true, options: [10,20,30,40,50,60,90,120])
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	schedule(rebootTime, rebootNow)
    
    
         
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	schedule(rebootTime, rebootNow)
   
    
   
   
}


def rebootNow(evt) {
	log.debug "Cycling power on switch(es): $theSwitch "
    def delay = secondsdelay
    theSwitch.off()
    log.debug "switch is off"
    log.debug " waiting for ${delay} seconds"
    
     if (delay == "10"){
    runIn(10, switchOn)}
     else if (delay == "20"){
    runIn(20, switchOn)}
     else if (delay == "30"){
    runIn(30, switchOn)}
     else if (delay == "40"){
    runIn(40, switchOn)}
     else if (delay == "50"){
    runIn(50, switchOn)}
     else if (delay == "60"){
    runIn(60, switchOn)}
     else if (delay == "90"){
    runIn(90, switchOn)}
     else if (delay == "120"){
    runIn(120, switchOn)}
  
   
}

def switchOn (){
log.debug "switching on"
theSwitch.on()
}
1 Like

Hey @Cobra, this works really well!

@abrock Did you try V1.0 or V1.1? :slight_smile:

I’m using V1.1 set at 30 seconds downtime in between power off and power on

I am using 1.1, it works great. I was going to look a adding an option to select a day of the week for devices that I want to cycle less frequently. If I get it working I will share back!

@abrock
Great!
I hadn’t thought of that as I needed a reboot every day
It would be useful to schedule different days though

You can use smartlighting for this except your limited to 1 minute increments.

@abrock Ok…
V1.2
This one allows you to select multiple days of the week.

/**
 *  Copyright 2017 SecurEndpoint
 *
 *  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.
 *
 *  Power Cycle
 *  V1.2 - Added days of the week so can be scheduled to operate only on certain days
 *  V1.1 - Added daily scheduling
 *  V1.0 - Initial release
 *
 *  Last updated 07/05/2017
 *  Sets a schedule to turn off a switch,  after a preset number of seconds it will turn back on again - Used to power cycle some equipment daily
 *
 *  Author: COBRA
 */





definition(
    name: "Scheduled Power Cycle",
    namespace: "Cobra",
    author: "SecureEndpoint",
    description: "Schedule a switch to turn off then automatically turn it back on after a set number of seconds you specify.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
)

preferences {
	section("Which switch to cycle...") {
        input(name:"theSwitch", type: "capability.switch", required: true, multiple: true)
	}
	section("Power Cycle at...") {
		input (name: "rebootTime", title: "At what time?", type: "time",  required: true)
	}
    section("On Which Days") {
        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("How long to stay off") {
		input(name:"secondsdelay", type:"enum", title: "Seconds", required: true, options: [10,20,30,40,50,60,90,120])
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	schedule(rebootTime, rebootNow)
    
    
         
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	schedule(rebootTime, rebootNow)
   
    
   
   
}


def rebootNow(evt) {

 def df = new java.text.SimpleDateFormat("EEEE")
    // Ensure the new date object is set to local time zone
    df.setTimeZone(location.timeZone)
    def day = df.format(new Date())
    //Does the preference input Days, i.e., days-of-week, contain today?
    def dayCheck = days.contains(day)
    if (dayCheck) {


	log.debug "Cycling power on switch(es): $theSwitch "
    def delay = secondsdelay
    theSwitch.off()
    log.debug "switch is off"
    log.debug " waiting for ${delay} seconds"
    
     if (delay == "10"){
    runIn(10, switchOn)}
     else if (delay == "20"){
    runIn(20, switchOn)}
     else if (delay == "30"){
    runIn(30, switchOn)}
     else if (delay == "40"){
    runIn(40, switchOn)}
     else if (delay == "50"){
    runIn(50, switchOn)}
     else if (delay == "60"){
    runIn(60, switchOn)}
     else if (delay == "90"){
    runIn(90, switchOn)}
     else if (delay == "120"){
    runIn(120, switchOn)}
 } 
 else {
 log.debug "Not today!"
 }
}

def switchOn (){
log.debug "Switching back on"
theSwitch.on()
log.debug "$theSwitch is now on"
}

I think this is pretty much done now…
I know I could have used smart lighting but I used this as an exercise to help my learning curve with this language.

I have tested this but only for today - If you look in the IDE I have added a little debug logging to see what is going on
Have a play for a couple of days and let me know if this works for you too :slight_smile:

Andy

Can you check the code? The reason I ask is because when I try to install it I get an error message.

I copied the exact code added as a smart app in the IDE and when I try to go and add the app in SmartThings and gives me an unexpected error

I’v tried a couple of times today to install code for a different app and I had to try it a few times before it would work.
I think smartthings cloud is playing up again

Are you on ios or android?

Android. Tried from my phone and a MAC.

It might be that Android has a problem with ‘enum’ (the bit for selecting how long to be off before on)
It works perfectly on IOS
I’ll look at re writing it so you can put a number in instead

@BBoy486
ok… I’ve rewritten part of the code to see if ‘enum’ is the problem
You can try this but if it doesn’t work then I’m not sure where to go from here.
I only have ios devices to test with and it works perfectly with this.

Code:

/**
 *  Copyright 2017 SecurEndpoint
 *
 *  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.
 *
 *  Power Cycle
 *  
 *  V1.3 - changed downtime set method so a number is entered rather than a selection - Trying to fix Android issue 
 *  V1.2 - Added days of the week so can be scheduled to operate only on certain days
 *  V1.1 - Added daily scheduling
 *  V1.0 - Initial release
 *
 *  Last updated 07/05/2017
 *  Sets a schedule to turn off a switch,  after a preset number of seconds it will turn back on again - Used to power cycle some equipment daily
 *
 *  Author: COBRA
 */





definition(
    name: "Scheduled Power Cycle V1.3.0",
    namespace: "Cobra",
    author: "Andrew Parker",
    description: "Schedule a switch to turn off then automatically turn it back on after a set number of seconds you specify.",
    category: "Convenience",
    iconUrl: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet.png",
    iconX2Url: "https://s3.amazonaws.com/smartapp-icons/Meta/light_outlet@2x.png"
)

preferences {
	section("Which switch to cycle...") {
        input(name:"theSwitch", type: "capability.switch", required: true, multiple: true)
	}
	section("Power Cycle at...") {
		input (name: "rebootTime", title: "At what time?", type: "time",  required: true)
	}
    section("On Which Days") {
        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("How long to stay off") {
		input(name:"secondsdelay", type:"number", title: "Seconds", required: true)
	}
}

def installed() {
	log.debug "Installed with settings: ${settings}"
	schedule(rebootTime, rebootNow)
    
    
         
}

def updated() {
	log.debug "Updated with settings: ${settings}"
	unsubscribe()
	schedule(rebootTime, rebootNow)
   
    
   
   
}


def rebootNow(evt) {

 def df = new java.text.SimpleDateFormat("EEEE")
    // Ensure the new date object is set to local time zone
    df.setTimeZone(location.timeZone)
    def day = df.format(new Date())
    //Does the preference input Days, i.e., days-of-week, contain today?
    def dayCheck = days.contains(day)
    if (dayCheck) {


	log.debug "Cycling power on switch(es): $theSwitch "
    def delay1 = secondsdelay as int
    
    theSwitch.off()
    log.debug "switch is off"
    log.debug " waiting for ${delay1} seconds"
     
    runIn(delay1, switchOn)
    }
 
 else {
 log.debug "Not today!"
 }
}

def switchOn (){
log.debug "Switching back on"
theSwitch.on()
log.debug "$theSwitch is now on"
}

Please let me know how you get on

That worked!

@BBoy486
Great!
So now I know the problem with Androids :slight_smile:

I hope you find it useful

1 Like