HTTP Binding item, how to send switch command and also query its status

This post was flagged by the community and is temporarily hidden.

As stated in the HTTP Readme, to

Receive repeated updates from a URL (“in” binding):

http="<[<url>:<refreshintervalinmilliseconds>:<transformationrule>]"

This post was flagged by the community and is temporarily hidden.

EDIT: Have made some progress with this ITEM file:

Switch Sprinkersfrontleft “Front Left and Rear Sprinklers” {http=">[ON:GET:(h)ttp://192.168.1.8:81/sn2=1&t=300] >[OFF:GET:(h)ttp://192.168.1.8:81/sn2=0]",
http="<[(h)ttp://192.168.1.8:81/sn2:60000:MAP(onoff.map)]"}

WIth onoff.map:
0=OFF
1=ON

It doesnt error, but its not actually updating the item with state when it checks. (Ignore the brackets around the ‘h’ I arent allowed to post links on this site.)

Any ideas would be much appreciated.

???

What is the (h) all about? That is not a valid URL.

Becuase I am new here, I cannot post ‘links’ The forum is picking the http as a url link and wont let me post without modifying the text.

Still not getting anywhere. Maybe you cant have > commands and a < query in the same http item config?

Just follow the docs, which show this example of a binding string:

http=">[*:POST:http://www.domain.org/home/lights/23871/?status=%2$s&type=text] <[http://www.domain.org/weather/openhabcity/daily:60000:REGEX(.*?<title>(.*?)</title>.*)]"

Interesting. There are all sorts of restrictions the forum places on new users that makes things difficult

You should be able to have both in and out bindings on one item but I personally never do. I’ll usually use three items, an incoming, an outgoing, and a proxy item. I then use roles to update the proxy when the incoming item changes and send commands to the phone when the proxy recurved a command. This let’s me more easily log out and debug the behaviors.

The only thing I can see, not having used the http binding on this way is often the two configs are separated by a comma.

Thanks for your reply and ideas, I have now created separate items for ‘status.’ which I have working. I cant find a way to ‘force’ a http poll though, its only on the default refresh interval.

It would be nice to be able to:

  1. Send http command to turn the station on
  2. Wait a second or so for it to take affect.
  3. poll the station via HTTP to see if the station has in fact turned on.
  4. Set the state of the item on the sitemap to suit.

There is probably a way via rules to do this so will look into that.

Yes, this can be done in rules.

Do you have an example for this? I have the same setup which would be better to poll status after triggering an item instead of polling every 1000ms.

val results = sendHttpGetRequest(<url>)
// check the results for success, deal with failure

createTimer(now.plusSeconds(1), [ |
    val results2 = sendHttpGetRequest(<url>)
    // check results2 for success and that the station is ON

    if(<stationOn>){
        <MyItem>.postUpdate(<newState>)
    }
])

Replace everything in < > with the appropriate code/Items/values.

I don’t understand where I should paste this code. Sorry OH(2) is totally new for me.

Currently I’ve this for example in an item file.

Switch pi1_sprinkler1 "Sprinkler 1" { http=">[ON:GET:http://xxxx:81/17/on] >[OFF:GET:http://xxxx:81/17/off] <[http://xxxx:81/status.json:1000:JSONPATH($.pins.p17.state)]" }

The last part should be reduced by this “rule”.

Should I paste the code above to a .rules file?
“MyItem” would be “pi1_sprinkler1”, right?
“url” is my status URL or the URL for switching the item?
What should I use for “stationOn” and “newState”

Yes.

I recommend a review of the Beginner’s Tutorial and Concepts and Rules sections of the docs. Installing the Demo package and looking at the demo configuration would be helpful as well.

I would assume so. You have not provided near enough information for me to say so definitively.

I can’t really say. You don’t provide enough information for me to day. If the status URL corresponds with Chris;s step 1 and step 3 then yes.

It depends on what your Item that you are sending the Update to is and what it is supposed to mean.

<stationOn> is an if statement that you need to write based on what the URL returns that tells you whether the station is on or off. I don’t know what this URL returns. I couldn’t begin to guess what this condition needs to look like.

<newState> needs to be what ever is appropriate for the Item you want to update when the station is ON.

Ok. I tough that this is enough information:
Switch pi1_sprinkler1 "Sprinkler 1" { http=">[ON:GET:http://xxxx:81/17/on] >[OFF:GET:http://xxxx:81/17/off] <[http://xxxx:81/status.json:1000:JSONPATH($.pins.p17.state)]" }

OH2 runs at a Pi3 and another Pi(1) is handling some relays for garden sprinkler.
The item above is just one of them.

Using the URL http://xxxx:81/17/on a relay is switched on.
Using the URL http://xxxx:81/17/off a relay is switched off.

http://xxxx:81/status.json returns a JSON with the status/state of all relays.

{
	"name" : "pi Sprinkler Status",
	"pins" : {
	
		"p17" : {
			"pin" : 17,
			"name" : "GPIO 17 - Sprinkler 1",
			"state": "OFF"
		},
		"p00" : {}
	}
}

Currently I check the state every 1000ms (":1000:") and transform the JSON result JSONPATH($.pins.p17.state)

The URL for switching on/off a relay is different from status.json
The JSON was created by me to can independent from anything. I have some other tools that also using this pins for switching the relays, eg an old php interface, cronjob, console.

My plan is to use OH2 to trigger the status.json only if OH2 has changed the switch status, instead of checking each 1000ms the status.

A much better case would be that Pi1 (Sprinkler) send a notification to Pi3 (OH2) if a relay state has changed.

Hope this helps more to understand my problem.

If I understand you right I can shorten this code.

if(<stationOn>){
    <MyItem>.postUpdate(<newState>)
}

to

<MyItem>.postUpdate(<newState>)

because I need every time the current state.

But I don’t understand this part…

val results = sendHttpGetRequest(<url>)
// check the results for success, deal with failure

createTimer(now.plusSeconds(1), [ |
    val results2 = sendHttpGetRequest(<url>)
    // check results2 for success and that the station is ON
])

Where is the depending to my item if this was triggered?

I would leave the every second polling so it can catch the state where something else turned on the relay or OH goes down and comes back up and will always have a reasonable recent positive knowledge of the state of the relay.

And your case is NOT the same as OP’s case.

You need the following:

  1. Rule that triggers when the relay Item receives a command
  2. In the Rule get the current status of the Item using sendHttpGetRequest
  3. Parse the results
  4. Update the state of the status Item

Please read the docs to understand how to write Rules (i.e. how the code gets triggered) and the relationship between all the parts of OH.