Exec Binding curl

Hey Guys i have a little problem with the exec Binding i try to send the following command:

curl -XPUT -H "Content-type: application/json" -d '{"scene": "e-nLzneCKyX0Zk4"}' 'http://192.100.20.11/api/myuser/groups/3/action'
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: application
curl: (3) [globbing] unmatched close brace/bracket in column 18
curl: (1) Protocol "'http" not supported or disabled in libcurl

what exactly does that mean? if i fire the command directly from the Pi CLI it runs just fine

I don’t know the answer but why not use HTTP Binding?

As far as I know the HTTP Binding only supports get and post or am I incorrect? I need put. I’m also not sure about the syntax in combination with both the header and the body.

You may be right, I don’t know much about this binding.
But maybe you can use a rule with executeCommandLine(command) as a workaround? You can define command earlier or use a bash script saved as file or just type it explicitly (but this sometimes causes errors)

When running commands using the exec binding remember they are running as the user openhab. This user has a very limited path, limited permissions, and no shell. Any one of these could be the source. From reading the error I would say that libcurl is either not in openhab’s path or openhab does not have permission to use it.

Thanks for the Help,

I fixed it by sending the HTTP Put from the Rule:

rule "HallwLightsON"
when
    Item HallwMotion changed from 0 to 1
	
then
	if(HallwScene.state=="Off") {
      }

	else if(HallwScene.state=="Nightlight"){
    	sendHttpPutRequest("http://192.100.20.11/api/GBL1Hvu8d4kbK7G1iCb3BtsRQiPXFabP9RpKnjng/groups/3/action", "application/json", '{"scene":"l7hjbIc8lJNMvS8"}')
      }

   	else if(HallwScene.state=="Bright"){
    	sendHttpPutRequest("http://192.100.20.11/api/GBL1Hvu8d4kbK7G1iCb3BtsRQiPXFabP9RpKnjng/groups/3/action", "application/json", '{"scene":"e-nLzneCKyX0Zk4"}')
      }

    else if(HallwScene.state=="Activate") {
    	sendHttpPutRequest("http://192.100.20.11/api/GBL1Hvu8d4kbK7G1iCb3BtsRQiPXFabP9RpKnjng/groups/3/action", "application/json", '{"scene":"7TOrA-ovqnbfnzs"}')
      }

end

That works pretty good now. Now my question is im looking at a sting to call the right scene. I saw some examples with switch case that looked pretty nice in the rule as well. However as far as i understand a Switch with more than 2 Positions is defined in the SItemap correct? I plan on using only Habmin as GUI so thats not an option. Is it possible to directly define the switch item with more then 2 states? Im using Openhab2.
Does my approach have any major drawbacks compared to the switch case approach?

1 Like
val url = "http://192.100.20.11/api/GBL1Hvu8d4kbK7G1iCb3BtsRQiPXFabP9RpKnjng/groups/3/action"
switch HallwScene.state {
    case "Nightlight": sendHttpPutRequest(url, "application/json", '{"scene":"l7hjbIc8lJNMvS8"}')
    case "Bright":     sendHttpPutRequest(url, "application/json", '{"scene":"e-nLzneCKyX0Zk4"}')
    case "Activate":   sendHttpPutRequest(url, "application/json", '{"scene":"7TOrA-ovqnbfnzs"}')
    case "Off":
}

Use a Number or String Item and the Switch element on the sitemap with mappings.

No. You either need to define it as a Number or a String and use some UI element to make it behave as a Switch. If Habmin doesn’t support something like Mappings then you are out of luck.

None functionally. I tend to find switch cases a little easier to read and follow so tend to choose them over if/else clauses unless the conditions are complicated or the code that is done for each case is more than a few lines of code. For example, the switch statement I have above is much more compact and easy to read in my opinion, particularly when using a val to store and reuse for the url.

Thanks rlkoshak,

Thought so. Im going to use the val approach as well. That make it defiantly easier to read an maintain.
Are val and var actually local to one rule or are they share between rules?

Tomibeck

It depends on what context they are declared.

If you have a val/var above the first rule in the file than it is a global available to all rules in that file.

If you declare it in a rule it is valid only for that one run of the rule. Each subsequent run of the rule will create its own val/var.

If you declare it inside { } the val/var is only valid inbetween those { }. So, for example, the following is invalid:

if(condition){
    var myVar = "test"
    logInfo("Test", myVar) // valid
}
logInfo("Test", mvYar) // error, myVar does not exist here