Possible to send slider level value as POST variable?

I have a networked lighting controller that accepts post commands. Simple on/off commands work but I can’t seem to get the level value to pass in the POST request. It just keeps reporting as ‘null’ and sets intensity to 0%.

Commands are formatted like this :
/api/element/on?id=957044a9-be3c-4d27-92de-8b5010c1f9e3&intensity=

I’m trying to get the slider level to be a variable in the passed POST but it always returns ‘null’

httpPost(“http://:8888/api/element/on”, “id=957044a9-be3c-4d27-92de-8b5010c1f9e3&intensity=$level”)

1 Like

Just wondering if you’ve tried the obvious debugging messages to Live Logging, such as…

log.debug("http://:8888/api/element/on", "id=957044a9-be3c-4d27-92de-8b5010c1f9e3&intensity=${level}")

Like @tgauchat said have you tried doing a simple log.debug $level to see if the variable even has what you’re looking for? Also, I’m doing something somewhat similar and had to use the code device.currentValue(“level”) to get the current slider level. You might try that as well.

1 Like

I have a log.debug stating the level inside of def setLevel. It shows the slider value. I was trying to get the value into the def on() but it would always be null. I added the post command inside of setLevel and it works. I don’t quite understand the way the “def”'s work yet.

The scope of a “defined variable” is only the current method.

SmartTiles “sandbox” does not allow creation global variables, but provides “state” map as an option that already has scope of the entire SmartApp (or DTH?). Please see the developer documentation.

However, if you are using an Attribute – they are NOT variables!!! But they can be read using currentValue(attribute-name), and written using “sendEvent”.

So this code :
def setLevel(percent) {
log.debug "setting level to $percent"
sendEvent(name: “level”, value: $percent)

I would be able in def on() to use …&intensity=$currentValue(level) ?

Roughly, yes… Feel free to post the full source code on GitHub / Gist or something if you want syntax double-checked.

In the on() {} command method, it wouldn’t hurt to def a local variable to that method to make it a tad convenient to debug.

def on() {
    def myLevel = device.currentValue( level );
    log.debug ( "In on() with level ${myLevel} ");
   ...

I think you may need to reference the “device” object to use the “currentValue()” method.

Please reference Docs… I think this is a pretty relevant link / example:

http://docs.smartthings.com/en/latest/ref-docs/device-handler-ref.html#device

Great! Thanks for all your help @tgauchat !! Now if I could just figure out this hubAction thing for local network post I’d be all set. Currently works with dyndns setup and forwarded ports, but I’d rather my server not be publicly accessible for lack of authentication ability.

BTW still getting null when in def on().
Copied in your code snippit and it returned in debug “In on() with level null”

Try putting level in quotes “level” like I posted earlier.

Still logs In on() with level null

At this point I’d recommend posting your full code as @tgauchat suggested so we can see what you’re actually doing. It’s probably something simple but we’re just guessing since we can’t see it.

1 Like

It’s kinda messy as I’ve just commented out stuff to delete later when it all works.

Wow, I feel really dumb now. After seeing it in github I noticed the sendEvent value didn’t have quotes. All works now. lol no more null numbers. Any direction on the hubAction would be appreciated though.

1 Like

So a few things with your hubaction. Your host:port needs to be in hex. Check out this link: http://docs.smartthings.com/en/latest/cloud-and-lan-connected-device-types-developers-guide/building-lan-connected-device-types/building-the-device-type.html

Also, your path should just be the part after the port i.e. api/element/on?id=957044a9-be3c-4d27-92de-8b5010c1f9e3&intensity=${percent}

This is just an example that I grabbed from one of my DH’s:

 def result = new physicalgraph.device.HubAction(  method: "GET",
    path: path,
    body: content,
    headers: [
        HOST: hostHex
    ]
)

sendHubCommand(result)