Help calling multiple hubactions from single command


I don’t think my subject explains what I am trying to accomplish well.

Essentially, I need to send two packets to my device, separately. For instance, in the “fade” command of the device, I want to send packet #1, then packet #2. Unfortunately, only the second hubaction command gets actually run (because that is what I am returning).

So, how do I send two hubactions from a single command? I tried this in the fade handler, but nothing happens.

def cmds = []
cmds << sendCommand(getCode("select_group_1"))
cmds << "delay 1200"
cmds << sendCommand(getCode("fade", percentage))

I also tried this:

return sendCommand(getCode("fade", percentage))

which, as you can expect, only the second sendComand hubaction is actually executed (first is never sent)

Hi Jared. A couple of questions to understand what you’ve got going on:

Is this in parse?

How is sendCommand defined?

You could take a look at this device type to see how it uses createEvent to send multiple events from parse:

Arg. I knew I needed to explain this better. Thanks for your interest in my issue. sendCommand is used to send raw TCP packets to a device on my network. It is executed when a user interacts with the device in the SmartThings UI (hits a button, or moves a slider).

def sendCommand(cmd) {

    def hexIP = convertIPtoHex(ipAddress)
    def hexPort = convertDecToHex(port)
    device.deviceNetworkId = "$hexIP:$hexPort"   
    try {
        def hubAction = new physicalgraph.device.HubAction(
            "${cmd}", physicalgraph.device.Protocol.LAN, device.deviceNetworkId

        log.debug hubAction
    catch (Exception e) {
        log.debug "Hit Exception $e on $hubAction"

Ok, I get it. I’m not sure about this… I’ve only ever sent one. If returning a list of them doesn’t work like it does for events then my guess is that they don’t have a way to do it. The documentation doesn’t mention anything about this either. Maybe @Jim could help?

If no proper response is given, the hub can wait for up to 20 seconds to timeout before listening for another response.

It is not a proper web socket, it is a web browser, essentially.

Best way to accomplish what you are trying to do is create a command queue and a scheduler to run until the queue is empty. Wait for a parse / response before sending the next item in the queue, etc.

Is there documentation around creating a command queue for SmartThings? Is that different from the code I posted in the first post?

def cmds = []
cmds << sendCommand(getCode("select_group_1"))
cmds << "delay 1200"
cmds << sendCommand(getCode("fade", percentage))

No documentation, I don’t have a good example to share… You have only built the queue with a map of the commands.

What I am talking about is getting around the delay / timeout and parse a response…

What is the use case here?

I’m not so sure about this timeout topic. I have some buttons on my custom lighting device (on/off/white/red/blue, for example). I can hit these buttons and send the raw TCP commands without any issues one after the other, without waiting for a response.

The challenge here is sending two commands from within the same “button click”. SmartThings seems to only want to send one.

I’m trying to do exactly the same thing, to control X10 through an Insteon hub. This takes two messages to the hub, one to set the unit address and one to set the command.

Your sendCommand code doesn’t actually send anything. It just creates a hubAction object and returns it. When you return from the command method that is called in your deviceType (i.e. the on or off method for a switch) whatever is returned is sent to the hub. I think that if you return an array of hubCommands it will send each in turn. You can also try calling sendHubAction(hubAction) but I’m not sure this is working the way I expect :frowning:

My problem is that the insteon hub needs about 500ms between calls otherwise it just ignores them. I’m trying to work out a way of getting this to happen, think it involves scheduling (as one of the other respondents suggested).

1 Like

I have the same issue. I need to perform a HTTP GET to determine the state of something, then a HTTP POST in the same function. This seems impossible. I have attempted putting a wait in, but it simply will not execute the hubAction if you already called it within the same session :frowning:

You can call httpGet() directly from the deviceType code, so theoretically you could make several calls before returning the hubaction (or null if you don’t need any further calls). Having said this I’m calling httpGet() within a method called by runIn() and it throws an error every time. I get: “error something went wrong: org.apache.http.conn.ConnectTimeoutException: Connect to [/] failed: connect timed out”.
That’s it, no further debug info, which isn’t terribly helpful!

It’s a shame we can’t run this code in a debugger. Even more of a shame that we can’t download the smartthings libraries into our own IDE to get name completion, compile-time checking etc.

You cannot use httpGet() for local hub calls, can you? httpGet is run in the cloud?

I agree, the SDK is somewhat limiting, and you have to sometimes do silly things to achieve simple tasks.

I found an answer to my question whcih lets me call multiple hubactions from a single command, just goto the question I posted above ^^

Yes, gave up on httpGet(). It’d documented in the device driver code but doesn’t say anything about how it works - as usual :frowning: Finally got it all working by returning a list of hubActions from the command call, see

1 Like