HTTP binding - openHAB 3 version


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?

Look in your events.log. If your Items do not get updated, there is nothing to show.

In the label section maybe add the state presentation https://www.openhab.org/docs/configuration/items.html#label

like add [%s],…

1 Like

Until now all of my things and items have been mqtt related.
I now want to display some data from my energy monitor webui.
As this is my first use of HTTP binding I thought I should use the openhab2 version.
It’s not clear what I need to do to install the binding. I’ve downloaded the jar file and copied it to the openhab2-addons folder and restarted openhab but there are no entries in the log indicating that it started. Is there something else I need to configure?

You need to configure a thing.

Go to karaf console and do a

bundle:list |grep HTTP

Then something like this should appear:

292 │ Active   │  80 │ 2.5.7.202007190812      │ openHAB Add-ons :: Bundles :: HTTP Binding

If so, then configure a thing in PaperUI. Got to --> Inbox --> + --> HTTP Binding

I just checked and it appears under Configuration > Bindings in PaperUI so I guess it did install.
I’ve been searching the community and docs and I think my things and items should look like this but they don’t appear in PaperUI so I guess I’ve got it wrong.

Thing "ecomon" @ "myhome/Ecomon"
	{
		Channels:
    		Type Number Importing "Value: [%.1f kWh]" { http="<[http://192.168.1.34/ecostatus.json:JSONPATH($.(0).NWATT)]" }
    		Type Number Exporting "Value: [%.1f kWh]" { http="<[http://192.168.1.34/ecostatus.json:JSONPATH($.(0).WATT)]" }
    		Type Number Generating "Value: [%.1f kWh]" { http="<[http://192.168.1.34/ecostatus.json:JSONPATH($.(12).WATT)]" }
	}
Number Importing "Value: [%.1f kWh]" { http="<[http://192.168.1.34/ecostatus.json:JSONPATH($.(0).NWATT)]" }
Number Exporting "Value: [%.1f kWh]" { http="<[http://192.168.1.34/ecostatus.json:JSONPATH($.(0).WATT)]" }
Number Generating "Value: [%.1f kWh]" { http="<[http://192.168.1.34/ecostatus.json:JSONPATH($.(12).WATT)]" }

Can you provide example things and items files?

Your item configuration is related to http1 (OH1) binding. You have to create things and channels in PaperUI first. Then configure your items with the http2 binding channels.

e.g.

Number Importing "Value: [%.1f kWh]" { channel="http:url:ec17b366:4711" }

Up to now I’ve only added bindings through PaperUI, all things, items and sitemaps have been files.
I’ll try creating the thing in PaperUi.

You also could create a .things file. Configuration is explained here. But first you should try it with PaperUI to get confident with the new http2 binding.

An example of a thing file you can find here.

I’ve created a thing and channels in PaperUI but get the following error.

Executing transformation ChannelStateTransformation{pattern='($.(0).NWATT)', serviceName='JSONPATH'} failed: An error occurred while transforming JSON expression.

The pattern extracts the data I want on jsonpath.com