[SOLVED] HTTP GET Command to Control Device via REST API

Because your payload contains quote marks, you have to try a bit harder to get that passed to the http action. The rules interpreter is looking at those quotemarks, and treating them like it does any other quotemark. “Ooh, a string that starts here and stops there.”

Here’s an example

my new rule file. It does not work.

rule "testcase"

    when
    	Item test received update ON
    then
        var url = "http://172.21.10.182/devices/1010013"
        var contenttype = "application/json"
        var POSTrequest = '{“name”: “GOTO_POS_CMD”, “value”: 20}'

        var output = sendHttpPostRequest(url, contenttype, POSTrequest)
        
     
    end

Detail? You’ve looked in openhab.log?

The double quotemarks you’ve used here

var POSTrequest = '{“name”: “GOTO_POS_CMD”, “value”: 20}'

are not the same as these

var POSTrequest = '{"name": "GOTO_POS_CMD", "value": 20}'

Do you know which sort you really need ?

See also my answer above, PUT not POST.

2 Likes

@markymark

From the answers above, it should be:

rule "testcase"

when
	Item test received update ON
then
    var url = "http://172.21.10.182/devices/1010013"
    var contenttype = "application/json"
    var PUTrequest = '{"name": "GOTO_POS_CMD", "value": 20}'

    var output = sendHttpPutRequest(url, contenttype, PUTrequest)
    
 
end
2020-01-24 11:02:26.741 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home2.sitemap' has errors, therefore ignoring it: [2,2]: required (...)+ loop did not match anything at input 'switch'

2020-01-24 11:02:44.822 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'home2.items'
2020-01-24 11:02:53.767 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home2.sitemap' has errors, therefore ignoring it: [2,2]: required (...)+ loop did not match anything at input 'switch'

2020-01-24 11:02:58.508 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home2.sitemap' has errors, therefore ignoring it: [2,2]: required (...)+ loop did not match anything at input 'switch'

2020-01-24 11:04:19.930 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'home.sitemap' has errors, therefore ignoring it: [44,1]: no viable alternative at input 'switch'

2020-01-24 11:04:50.326 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'home.sitemap'
2020-01-24 11:05:26.350 [INFO ] [el.core.internal.ModelRepositoryImpl] - Refreshing model 'home.sitemap'
2020-01-24 11:05:33.630 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'home2.sitemap'

Okay, you’ve got two broken sitemaps. This is does not relate to your rule. Unless … what file are you editing your rule into?

testcase.rule

That’s one error then. Rules files need to named like xxx.rules, even if there is only one rule in it.

When you edit a rules file, you can check in openhab.log to see if the system loaded it or complains about it.

1 Like

Now there are the errors of the rules :slight_smile:

2020-01-24 13:45:15.957 [WARN ] [.core.internal.folder.FolderObserver] - Error while opening file during update: C:\openHAB\conf\rules\testcase.rules
2020-01-24 13:45:16.342 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'testcase.rules'
2020-01-24 13:45:16.342 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'testcase.rules' is either empty or cannot be parsed correctly!
2020-01-24 13:45:17.584 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model 'testcase.rules' has errors, therefore ignoring it: [1,6]: no viable alternative at input 'â'

Don´t know what openHAB means at the last line

It does not help when you only post the errors here.

Please post your home.sitemap, home2.sitemap and your testcase.rules here.

Thanks.

It means it doesn’t like the content of your rules file.
Given the funny character, you probably need to look into the character encoding your editor uses when it saves a file.

Encoding is ANSI

home2.sitemap

sitemap home2 label="test ui" {
Switch item=test label="test"
}

testcase.rules

rule “testcase”

when
	Item test received update ON
then
    var url = "http://172.21.10.182/devices/1010013"
    var contenttype = "application/json"
    var PUTrequest = '{"name": "GOTO_POS_CMD", "value": 20}'

    var output = sendHttpPutRequest(url, contenttype, PUTrequest)
    
 
end

Your next job then, is to find out what encoding it should be for openHAB to use it.

1 Like

It should be:

rule "testcase"
1 Like

In comination of those two fixes it works now :slight_smile:
openHAB use UTF 8

Thank you for all who helped me. This is the best community! :heart_eyes:

1 Like

What I have to put in instead of the value 20 to make it variable?

Well, your variable PUTrequest is just a string.
In rules, you can make strings by joining them together, amongst other things.

var newstring = oldstring + “abc” + myItem.state.toString

Have you fixed your sitemaps, as well?

Please read also this topic.

This will be your complete solution.

Btw, there are other commands for HomePilot, too:

http://HomePilotIP/devices/deviceID

PUT with content type :application/json

SWITCH:

{"name":"TURN_ON_CMD"} 
{"name":"TURN_OFF_CMD"} 

THERMOSTAT:

{"name":"TARGET_TEMPERATURE_CFG","value":16}

LIST OF DEVICES:

http://HomePilotIP/v4/devices

DEVICE STATE:

GET
http://HomePilotIP/devices/deviceID
.
.

EDIT: @markymark

For your rollershutter the following commands also should work:

PUT

{"name":"POS_UP_CMD"}
{"name":"POS_DOWN_CMD"}
{"name":"STOP_CMD"}
1 Like