HTTP binding - openHAB 3 version

I‘m already working on a solution.

I have updated the link above. There is a new option ignoreSSLErrors on thing-level. I checked with badssl.com that in fact invalid certificates are ignored.Please test and report if it works for you.

2 Likes

Works flawlessly :+1: , thanks for the fast implementation.

1 Like

I have a thing with user/password set but cannot delete (set empty) those ?? PaperUI bug ?

I’m failing to get another complex case to work. Maybe it could serve as an example once I get it to work.

Wanted: a “Switch” item to retrieve and send status/ON+OFF commands via HTTP
The base URL of the Thing is http://<IP>/web, channel config see below.
On retrieving URL/powerstate, the URL returns an XML that I can parse with a transformation (that worked in httpv1):

<?xml version="1.0" encoding="UTF-8"?>
<e2powerstate>
        <e2instandby>true</e2instandby>
</e2powerstate>

First problem is that state retrieval for this item does not work - item remains UNDEF. Thing is ONLINE. Although I have trace logging, I see no "GET"s logged, just “execute() called”.
TCPdump however reveals there’s requests going and being answered.

Second, on changing item I want it to POST URL/powerstate?newstate=4 if “ON” and 5 for “OFF”.
But I see the number is missing in the URL. Do I need a command transformation ?

@mstormi

One question.

Your state <e2instandby>true</e2instandby> represents true=OFF, false=ON ?
(in standby=true means power=OFF, in standby=false means power=ON ?? )

If so, then you should try MAP transformation e.g onoff.map for Command Transformation:

Content of onoff.map:

4=false
5=true

OR

false=4
true=5

.

And your channel definition then could be:
.

image

Hi

Great binding. I just have one issue.
I can read the values for 3 channels via GET method, but I need also a GET Method to set a switch item to ON & OFF.

here is the output when I set it via curl:

# curl -i -H "Accept: application/json" -H "Content-Type: application/json" -H "Token:SECRET" 'http://10.0.1.27/relay?state=1'
HTTP/1.1 200 OK
Pragma: no-cache
Cache-Control: no-store, no-cache
Content-Type: text/plain
Content-Length: 0
Connection: close

In the old binding, I defined the item that way:
ON:GET:http://10.0.1.27/relay?state=1{Token=SECRET}
OFF:GET:http://10.0.1.27/relay?state=0{Token=SECRET}

When I configure the channel that way, I get an exception in openhab.log.

2020-07-22 16:26:43.165 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method 'ThingHandler.thingUpdated()' on 'org.openhab.binding.http.internal.HttpThingHandler@1ad26566': String index out of range: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
	at java.lang.String.charAt(String.java:658) ~[?:1.8.0_252]
	at org.openhab.binding.http.internal.HttpThingHandler.concatenateUrlParts(HttpThingHandler.java:308) ~[?:?]
	at org.openhab.binding.http.internal.HttpThingHandler.createChannel(HttpThingHandler.java:191) ~[?:?]
	at java.util.ArrayList.forEach(ArrayList.java:1257) ~[?:1.8.0_252]
	at java.util.Collections$UnmodifiableCollection.forEach(Collections.java:1082) ~[?:1.8.0_252]
	at org.openhab.binding.http.internal.HttpThingHandler.initialize(HttpThingHandler.java:161) ~[?:?]
	at org.eclipse.smarthome.core.thing.binding.BaseThingHandler.thingUpdated(BaseThingHandler.java:166) ~[?:?]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_252]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_252]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_252]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_252]
	at org.eclipse.smarthome.core.internal.common.AbstractInvocationHandler.invokeDirect(AbstractInvocationHandler.java:152) [bundleFile:?]
	at org.eclipse.smarthome.core.internal.common.Invocation.call(Invocation.java:52) [bundleFile:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_252]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_252]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_252]
	at java.lang.Thread.run(Thread.java:748) [?:1.8.0_252]

regards
Remo

@HaKuNa

I would also try MAP transformation with e.g. onoff.map

Content of onoff.map:

1=on
0=off

.
And your URL Definition should be:
.

This was the right direction, thanks.

my map file:

true=1
false=0

and the channel definition:

1 Like

Thanks for your snapshot. The missing part actually was that I need to use %2$s for the state argument. @J-N-K if I didn’t overlook it that’s not documented ?

It in documentation at the bottom. See here.

That doesn’t seem to work properly yet.
On my last example (to read and write the channel), State URL Extension was ignored.
And when I’ve set it to empty (but readwrite mode), I got an exception on saving saying

2020-07-22 22:31:15.968 [ERROR] [nal.common.AbstractInvocationHandler] - An error occurred while calling method 'ThingHandler.thingUpdated()' on 'org.openhab.binding.http.internal.HttpThingHandler@163344': String index out of range: 0
java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.lang.StringLatin1.charAt(StringLatin1.java:47) ~[?:?]
        at java.lang.String.charAt(String.java:693) ~[?:?]
        at org.openhab.binding.http.internal.HttpThingHandler.concatenateUrlParts(HttpThingHandler.java:308) ~[?:?]
        at org.openhab.binding.http.internal.HttpThingHandler.createChannel(HttpThingHandler.java:191) ~[?:?]

With another thing which has no Command Extension, State Extension is working, but apparently not together with Command Extensions.

Actually those docs say %2$ but I had to use %2$s like in @Celaeno1’s example.

[quote=“mstormi, post:67, topic:101851, full:true”]

Well, that‘s not exactly true.

l. 101 states that the „format needs to be appended „ and l. 106 shows exactly %2$s. But probably this needs to be reworded to be more clear.

I took the string from your README.md (as most people will simply do rather than read java docs) so please fix it there

That‘s in the readme. Anyway, it should be made more clear.

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.