Delay between commands

Command1
Wait 2secs
Command2
Wait 5secs
Command 3
Wait 1 sec
Command 4

I know this is probably really simple but I’ve tried everything my novice mind could think of and I can’t get this to work. How do I accomplish this?

delayBetween(cmdList,ms)

I would need a different delay between each command.

not necessarily. You could put a bunch of null commands in that would eat up a 1 sec delay each for now, until a better option came along.

1 Like

You could use three runIns…
Or pauseExecution(Long milliseconds)

1 Like

I tried runIns but it would only seem to execute when each command was different. What I’m really trying to do is run the same command with a different parameter each time.

Thanks for the suggestions. I will try the null commands and pauseExecutions to see if I get better luck. If you have any other tricks I’m all ears.

def cmds = [
	Command1,
	"delay 2000",
	Command2,
	"delay 5000",
	Command3,
	"delay 4000",
	Command4
]
2 Likes

So…like everything I’ve tried so far…it doesn’t quite work. It runs but all the commands execute with no delay. Arrrgg…What am I doing wrong? See the dth code below…the string of commands is in the runSequence method.

/**
 *  HTTP Momentary Switch
 *
 *  Copyright 2018 Daniel Ogorchock
 *
 *  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.
 *
 *  Change History:
 *
 *    Date        Who            What
 *    ----        ---            ----
 *    2018-02-18  Dan Ogorchock  Original Creation
 *
 * 
 */
metadata {
	definition (name: "HTTP Momentary Switch Sequencer", namespace: "ogiewon", author: "Dan Ogorchock") {
        capability "Switch"
        capability "Momentary"
        command "RunSequence", ["number"]
	}

	preferences {
		input(name: "deviceIP", type: "string", title:"Device IP Address", description: "Enter IP Address of your HTTP server", required: true, displayDuringSetup: true, submitOnChange: true)
		input(name: "devicePort", type: "string", title:"Device Port", description: "Enter Port of your HTTP server (defaults to 80)", defaultValue: "80", required: false, displayDuringSetup: true, submitOnChange: true)
		input(name: "deviceMethod", type: "enum", title: "POST, GET, or PUT", options: ["POST","GET","PUT"], defaultValue: "POST", required: true, displayDuringSetup: true, submitOnChange: true)
        input "hub", "enum", title: "Select Hub", required: false, displayDuringSetup: true, options: getFromApi("hub"), submitOnChange: true
        input "activities", "enum", title: "Select Activity", required: false, displayDuringSetup: true, options: getFromApi("activities"), submitOnChange: true
        //input(name: "devicePath", type: "string", title:"URL Path", description: "Rest of the URL, include forward slash.", displayDuringSetup: true)
        getCommands()
	}
}

private getOptionsInput(item) {
    input "${item}", "enum",
        title: "Select ${item}",
	//defaultValue: "none",
	required: false,
	displayDuringSetup: true,
	options: getFromApi(item)
}

def getCommands(){
    for(i in 1..10) {
    	input "deviceCommand${i}", "enum", description: "", title: "Command ${i}", displayDuringSetup: true, options: getFromApi("commands")
    }
    
}

def getFromApi(item){
    def myOptions
	def myPath = ""
    def myServer = ""
    if(deviceIP) myServer = "http://${deviceIP}:${devicePort}"
   //log.debug myServer
    if(item=="hub") myPath = "/hubs"
    if(item=="activities") myPath = "/hubs/${hub}/activities"
    if(item=="commands") myPath = "/hubs/${hub}/commands"
    //log.error myPath
    def params = [
		uri: myServer,
        path: myPath
	]

	try {
    	httpGet(params) { resp ->
        	resp.headers.each {
        	//log.debug "${it.name} : ${it.value}"
    	}
    	//log.debug "response contentType: ${resp.contentType}"
    	//log.debug "response data: ${resp.data}"
       	if(item=="hub") myOptions = resp.data.hubs
        if(item=="activities") myOptions = resp.data.activities.slug
        if(item=="commands") myOptions = resp.data.commands.slug
        //log.error list
    	}
	} catch (e) {
		log.error "something went wrong: $e"
	}
   		//log.debug list.hubs.size
     return myOptions
}

def parse(String description) {
	log.debug(description)
}

def push() {
    //toggle the switch to generate events for anything that is subscribed
    sendEvent(name: "switch", value: "on", isStateChange: true)
    runIn(1, toggleOff)
    //sendEvent(name: "switch", value: "off", isStateChange: true)
    def newPath = "/hubs/"+hub+"/activities/"+activities
    runCmd(newPath, deviceMethod)
}

def on() {
	push()
}

def off() {
	//push()
    //sendEvent(name: "switch", value: "off", isStateChange: true)
    def newPath = "/hubs/"+hub+"/off"
    runCmd(newPath, "PUT")
}

def toggleOff() {
    sendEvent(name: "switch", value: "off", isStateChange: true)
}

def RunSequence(num) {
    log.info num
    def path = "/hubs/"+hub+"/commands/"+settings["deviceCommand${num}"]
    log.info path
    //runSeq(path, deviceMethod)
    def cmds = [
		runSeq("/hubs/"+hub+"/commands/"+settings["deviceCommand1"],deviceMethod),
		"delay 2000",
		runSeq("/hubs/"+hub+"/commands/"+settings["deviceCommand2"],deviceMethod),
		"delay 5000",
		runSeq("/hubs/"+hub+"/commands/"+settings["deviceCommand3"],deviceMethod),
		"delay 4000",
		runSeq("/hubs/"+hub+"/commands/"+settings["deviceCommand4"],deviceMethod),
	]
    return cmds
}

def runSeq(varCommand,method){
	def localDevicePort = (devicePort==null) ? "80" : devicePort
	//def path = varCommand
    //def meth = method
	def body = "" 
	def headers = [:] 
    headers.put("HOST", "${deviceIP}:${localDevicePort}")
	headers.put("Content-Type", "application/x-www-form-urlencoded")

	try {
		def hubAction = new hubitat.device.HubAction(
			method: method,
			path: varCommand,
			body: body,
			headers: headers
			)
		log.debug hubAction
		return hubAction
	}
	catch (Exception e) {
        log.debug "runCmd hit exception ${e} on ${hubAction}"
	}  
}

def runCmd(String varCommand, String method) {
	def localDevicePort = (devicePort==null) ? "80" : devicePort
	def path = varCommand 
	def body = "" 
	def headers = [:] 
    headers.put("HOST", "${deviceIP}:${localDevicePort}")
	headers.put("Content-Type", "application/x-www-form-urlencoded")

	try {
		def hubAction = new hubitat.device.HubAction(
			method: method,
			path: path,
			body: body,
			headers: headers
			)
		log.debug hubAction
		return hubAction
	}
	catch (Exception e) {
        log.debug "runCmd hit exception ${e} on ${hubAction}"
	}  
}

I think I see the issue, the delay command only works on zigbee and z-wave commands.

You want to delay the hubaction for a lan request. You probably should look at using runIn() as a way to schedule each request.

1 Like

I initially tried the runIn() but it wouldn’t execute all the commands. If I created a different method for each command it would run, but since I’m using the same method with different parameters, it would not process all the commands…just the first. I can revert to that method and post the code alongside the logs if that will help diagnose.

It may be a limit of how we process functions containing a hubaction. I’ll set up a test over the weekend.

If you got it working with sub functions I’d go with that and just pass the params to that sub function.

So hopefully I can explain this without confusing everyone. Looking at the code below…

def runSequence(){
    	runIn(3,"runTest1",[data: [num: 1]])
    	runIn(6,"runTest1",[data: [num: 2]])
}

def runTest1(data) {
    log.info data.num
    def path = "/hubs/"+hub+"/commands/"+settings["deviceCommand${data.num}"]
    log.info path
    runSeq(path, deviceMethod)   
}


def runTest2(data) {
    log.info data.num
    def path = "/hubs/"+hub+"/commands/"+settings["deviceCommand${data.num}"]
    log.info path
    runSeq(path, deviceMethod)
}


def runSeq(varCommand,method){
	def localDevicePort = (devicePort==null) ? "80" : devicePort
	//def path = varCommand
    //def meth = method
	def body = "" 
	def headers = [:] 
    headers.put("HOST", "${deviceIP}:${localDevicePort}")
	headers.put("Content-Type", "application/x-www-form-urlencoded")

	try {
		def hubAction = new hubitat.device.HubAction(
			method: method,
			path: varCommand,
			body: body,
			headers: headers
			)
		log.debug hubAction
		return hubAction
	}
	catch (Exception e) {
        log.debug "runCmd hit exception ${e} on ${hubAction}"
	}  
}

runTest1() and runTest2() are identical

If I code runSequence() to use the same method with a different parameter, it olny executes the second command.

If I code runSequence() to 2 separate methods (with the exact same code in both) it will then execute both commands.
eg.

def runSequence(){
    	runIn(3,"runTest1",[data: [num: 1]])
    	runIn(6,"runTest2",[data: [num: 2]])
}

I could create a separate method for each line, but this would be FAR from ideal since in at least one case, I will be executes about 10 commands.

I believe something like the following should work, but if it has the same rate limits as SmartThings you can only have 4 scheduled at the same time.

def runSequence(){
    	runIn(3,"runTest",[overwrite: false, data: [num: 1]])
    	runIn(6,"runTest",[overwirte: false, data: [num: 2]])
}
2 Likes

Worked perfectly. Thank you @krlaframboise!

1 Like

@mike.maxwell, @chuck.schwer

Mike and/or Chuck,

I am trying to understand whether or not Hubitat supports the old SmartThings “pause(milliseconds)” command or not? If not, what is a suitable replacement? I have tried “sleep(milliseconds)” and “thread.sleep(milliseconds)”, but both are rejected by Hubitat as Not Allowed.

Mike mentions “pauseExecution(seconds)” above. Is that a valid call? If so, is it really seconds? I need something that can handle milliseconds. As I am only looking to delay for about 250ms.

Thanks,
Dan

1 Like

sorry, pauseExecution(Long milliseconds), I’ll correct that post…

3 Likes

Thanks Mike! I’ll give that a try.

1 Like