using executeCommandLine to send a curl command

772 views
Skip to first unread message

eric

unread,
Dec 30, 2014, 3:01:19 AM12/30/14
to ope...@googlegroups.com

Hello,
I'm trying to use the OpenHAB interface to initiate a curl command.  OpenHAB is running on a Raspberry Pi.  I'm able to run this command from the Pi's terminal to dim lights.


curl "http://192.168.1.7/set_dev_value.php" -d "nodeId=a&attrId=aprontest -u -m2 -t2 -v 5;"

I'd like to run this command everytime DemoSwitch is pressed, for example.  Trying to use this rule, I don't see any results.

rule "dim it"
when
Item DemoSwitch received update
then
executeCommandLine("curl "http://192.168.1.7/set_dev_value.php" -d "nodeId=a&attrId=aprontest -u -m2 -t2 -v 5;"");
end

This gives me an error:

ERROR o.o.c.s.ScriptExecutionThread[:50] - Error during the execution of rule 'dim it': The name 'http' cannot be resolved to an item or type.


If I put the curl command in a script file and run the script file, that works.  However, eventually, I'd like to be able to pass the dim level directly from an OpenHAB dimmer widget to the curl command, so I thought it's pretty important to be able to run the curl command directly from OpenHAB.  Perhaps I'm wrong?  Can I run a script file and pass the script file a parameter ( the dim value)?

Thanks.

druciak

unread,
Dec 30, 2014, 3:36:04 AM12/30/14
to ope...@googlegroups.com
Escape double quotes inside your command:

  executeCommandLine("curl \"http://192.168.1.7/set_dev_value.php\" -d \"nodeId=a&attrId=aprontest -u -m2 -t2 -v 5;\"");

Krzysztof

eric

unread,
Dec 30, 2014, 2:58:59 PM12/30/14
to ope...@googlegroups.com
Ah, thanks.  If I want to replace the literal value "5" with the value of a item of type Number, is that possible?  After playing with it, I think scripting it makes sense, as long as I can pass a parameter to the script, like this:

Number My_Light_Level "Light Level [%.1f]"


executeCommandLine("/opt/dim1.sh My_Light_Level.state");


Except I want it to replace the parameter with the value of <My_Light_Level.state>.

Also, while the script is running, is OpenHAB blocked from doing other things until the script completes?

druciak

unread,
Dec 31, 2014, 2:02:16 AM12/31/14
to ope...@googlegroups.com
You can build your command line by concatenating string of using String::format().
Like this:
executeCommandLine("/opt/dim1.sh" + My_Light_Level.state)

or this in more complex cases:
var DecimalType level = My_Light_Level.state as DecimalType
executeCommandLine(String::format("/opt/dim1.sh %d", level.intValue))

Krzysztof
Reply all
Reply to author
Forward
0 new messages