Custom switch, turn off after ten minutes


#1

I am controlling a device over my local network via http get commands. I have this working, however, my device currently does not have the capability to talk back (maybe one day).

Right now, I have a simple on/off switch to control the device. But I need it to automatically flip back to the off position after 10 minutes. Is there a way to do this in the Device Handler? Or do I need to go the SmartApp path and use it in conjunction with my button?


(Rick Pittser) #2

I don’t know about the Device Handler, but that’s a pretty simple function if you use Rule Machine.

Beware the ST scheduler issues though if you go the RM route.


#3

I take it you are using a virtual switch already and have a smartapp to turn on the external switch??
If so… All you really need to include in the app is

def initialize() {
    subscribe(vtSwitch, "switch.on", onHandler) // vtSwitch is a virtual device used to trigger on/off for external device
    subscribe(vtSwitch, "switch.off", offHandler)
}

def onHandler(evt) {
	def currentTime = now()
	httpGet("$ip/event/..........") // turn on switch url
	runNotifyAt(currentTime + state.timerMs) // state.timerMs = 60 seconds * 10 minutes * 1000
}



def runNotifyAt(timeMs) {
	def date = new Date(timeMs)
	runOnce(date, shutOffSwitch)
	log.trace "'shutOffSwitch' scheduled to run at ${date}"
}


def shutOffSwitch() {
	log.trace "Timer has expired. Switch has been shut off"
	state.vtSwitchState=0
	vtSwitch.off()
	httpGet("$ip/event/..........") // turn offswitch url
}

(David Tucker) #4

Or, if you want something that is running locally on on your hub, without needing to run through the ST cloud you could set up a power allowance within the SmartThings “Smart Lighting” smartapp

This is not a better option than the others listed above, just an alternative, for completeness.


#5

I vaguely recall reading something about SmartThings scheduling being inconsistent with a fix in the works. I haven’t come across anything recent though nor have I experienced issues with it.

Was it fixed?


(Kevin) #6

I’m not sure if this is the method you should use to solve your problem, but if you wanted to do it from within the device handler, all you’d have to do is add the runIn line to the on command.

def on()  {
    runIn(600, off)
}

With the current scheduling problems that’s been going on, there’s really no way to reliably turn if off using the built in scheduling commands.


#7

Awesome. I just added it and will try it out tomorrow.

So I take it the scheduling issues have not been fixed…that sucks. So far so good for me though.


(Kevin) #8

[quote=“networx, post:7, topic:38597”]
So I take it the scheduling issues have not been fixed…
[/quote]No they haven’t, but since an external event is initiating the on command, it should turn off most of the time.

If you wanted to increase the chances of it running, you could add a second runIn command like the one below, but if you do that, you should probably add logic to the off command that prevents it from raising events more than once.

runIn(600, off)
runIn(615, off, [overwrite: false])

Someone else asked ST about the “scheduling fix” a few hours ago and there response was: