HTTP binding - openHAB 3 version

just to mention where I found the information:
-> It is explained under “URL Formatting” here https://github.com/J-N-K/openhab-addons/tree/http/bundles/org.openhab.binding.http

http://www.domain.org/home/lights/23871/?status=%2$s&date=%1$tY-%1$tm-%1$td

There’s 2 occurences. The one in the URL you refer to is correct.
But there’s another one 4 lines up which is bad which is what I took.

Did you see about the state extension not being honored ? (6 posts up)

It’s here:
After the parameter reference the format needs to be appended

I guess @mstormi means this sentence in documentation:

After concatenation of the baseURL and the commandExtension (if provided) the URL is formatted using the java.util.Formatter.

.
It better should be:
.

After concatenation of the baseURL and the commandExtension or the stateExtension (if provided) the URL is formatted using the java.util.Formatter.

I have fixed the exception and made the documentation a little bit more explicit.The link above is updated. Be careful if you drop the .jar in your addons folder: It’s now 2.5.8 instead of 2.5.7, so the old version will not be overwritten.

1 Like

Thanks, it’s working as expected now. Here’s my promised complex example for your docs:

My ‘Dreambox’ TV receiver has an HTTP API. The hostname is dm900.
On requesting /web/powerstate, it returns an XML

<e2powerstate>
<e2instandby>false</e2instandby>
</e2powerstate>

meaning it is currently not in standby thus ON. A POST request /web/powerstate?newstate=5 will instruct it to switch OFF and newstate=4 means to switch ON.


The state transformation REGEX fully reads REGEX:.*?<e2instandby>(.*?)</e2instandby>.* and returns what is between <e2instandby> and </e2instandby>, i.e. false or true.
dreambox.map for the command transformation is

true=5
false=4
NULL=no data
-=no data

Very unlike the HTTPv1 binding, the item definition is only a very simple channel reference
Switch Dreambox "Dreambox [%s]" { channel="http:url:<thing ID>:<channel ID>" }

1 Like

:smiley: That’s exactly my suggestion I made here:

1 Like

In the readme it says:

`mode` `READWRITE` Mode this channel is allowed to operate. `READ` means receive state, `WRITE` means send commands.

In the code it is actually:

<option value="READWRITE">Read/Write</option>
<option value="READONLY">Read Only</option>
<option value="WRITEONLY">Write Only</option>
2 Likes

Exellent work!!
This will be the binding to integrate all those proprietary smart/cloud devices.
But I’m stuck with authentication on my Air-Heatpump.
Does the method for URL need to be Post?
Cause REGEX can’t find matches, because it’s only loading the loginpage.

If your device requires authentication, you need to define this on the thing (user/pwd or token in the http header). This depends on the device you want to connect.


If have done that.
http://servicewelt/?user=Dirk&pass=******
But the login page uses POST to deliver the credentials, so I wonder if I have to configure the binding to use POST too.

I guess “text/html” is wrong. I always try via curl to fetch the data first. There you can add credentials, header, content-type … Afterwards you can transform the parameters to the thing in openHAB.
I don’t know if you need POST or GET for your call.

Thank you for the advice.
Will try it first with Curl, cause I already have a php-script running with exec-binding, which does a similar task.

Got it, but wasn’t easy.

    $AUTH_USERNAME="Dirk";
	$AUTH_PASSWORD="*****";
	$url = 'http://'.$linksys_ip.'/?s=1.1'; 	
$data = array("user" => $AUTH_USERNAME,  "pass" => $AUTH_PASSWORD);
foreach($data as $key=>$value) {
$postvars .= $key . "=" . $value . "&";
  }
   
   $ch=curl_init($url);
   curl_setopt($ch, CURLOPT_POST, true);
   curl_setopt($ch, CURLOPT_POSTFIELDS, $postvars);
   curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
      
   $response = curl_exec( $ch );
   if(!$response){
       die('Error: "' . curl_error($ch) . '" - Code: ' . curl_errno($ch));
   }

Any idea how to transform this into http-binding?

This is an important binding, thanks @J-N-K for the effort! I made a OH3 build of the current version, for those interested.

I tried it because I need to query some Yamaha devices with an JSON/RPC API, currently I use rules to do the polling and send commands, which works fine, but doing it with a proper binding instead of triggering a rule every 5 seconds is far better.
For example I can retrieve the volume in the response to /main/getStatus which looks like this:

$ http http://192.168.1.xxx/YamahaExtendedControl/v1/main/getStatus
HTTP/1.1 200 OK
Content-Length: 473
Content-Type: application/json
Server: Network_Module/1.0 (RX-V479)

{
    "actual_volume": {
        "mode": "db",
        "unit": "dB",
        "value": -47.0
    },
    "contents_display": true,
    "direct": false,
    "disable_flags": 0,
    "distribution_enable": true,
    "enhancer": true,
    "input": "spotify",
    "link_audio_delay": "audio_sync",
    "link_audio_quality": "uncompressed",
    "link_control": "standard",
    "max_volume": 161,
    "mute": false,
    "power": "on",
    "response_code": 0,
    "sleep": 0,
    "sound_program": "5ch_stereo",
    "surr_decoder_type": "dolby_pl2x_music",
    "tone_control": {
        "bass": 0,
        "mode": "manual",
        "treble": 0
    },
    "volume": 67
}

but to update the volume I need to do a GET (for some reason?) like /main/setVolume?volume=67

So I found the Command method advanced parameter of the Thing which I changed to GET (and reduced the polling to every 5 seconds):


And created a Number channel (read only) with the JSONPATH:$.volume transformation:


I then created & linked an item to verify the volume was properly retrieved and parsed into the item’s state:



Next I wanted the same item to also control the volume when receiving commands, so I created a second write-only channel. Oddly enough I made a mistake and set the “State URL Extension” instead of “Command URL Extension” to /main/setVolume?volume=%2$d but it still worked:


Then I linked the same item to that channel (you can do that in a number of ways, from the Thing page, the Model page, or the item details page):


I saw @mstormi’s solution which is better only after doing that - a single channel would have sufficed!

Finally since this Number item is controllable I set the “Default Standalone Widget” metadata to override the default which is a simple label card for this type of items, selected “Slider Card”, and changed some options.


Now the item represents the current volume in a slider with its default control, and operating that slider changes the volume as expected. Great!

Next I saw there’s support for Image items, which is great for cameras and album art, so I’ll try to retrieve it and see how it works (there is no support for images in the OH3 UI yet btw, it’s probably time to add it).

Hope that helps.

2 Likes

As a user who has not used the http binding before, what is something the average user might find useful / neat to use it for?

Could one use it to check if a website returns a status code of 2XX? vs 401 Not found? (Useful for monitoring a remote server?)

OH is not a monitoring system. If you need special HTTP response conditions you should use rules.

The main use of the binding is to talk to devices to have a web UI or API but no OH binding.

Thanks - Just looking to see how i might have tried to use this binding, but based on this, nothing I have to use it for.

This is a good example! Nice stuff.

@J-N-K,
Great work and thanks for sharing for the community!

I have something strange behavior with the addons.
The data from text and number channels aren’t shown in a native OB application (Android & IOS), from the web-browser everything is work.

Any clue?