Problem with http binding in openhab2

Hi,

I switched from openhab1.7.0 to openhab2.1.0 and i’m having trouble with the http binding. i get the following error:

19:43:00.212 [ERROR] [b.core.service.AbstractActiveService] - Error while executing background thread HTTP Refresh Service
java.lang.NullPointerException
	at org.eclipse.smarthome.transform.javascript.internal.JavaScriptTransformationService.transform(JavaScriptTransformationService.java:70)[220:org.eclipse.smarthome.transform.javascript:0.9.0.b5]
	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:60)[176:org.openhab.core.compat1x:2.1.0]
	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:189)[221:org.openhab.binding.http:1.10.0]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:157)[176:org.openhab.core.compat1x:2.1.0]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169)[176:org.openhab.core.compat1x:2.1.0]

I have no idea what is causing this error becouse the following worked in openhab1.6 without any problem:

Number Photo_WattsPhoto "Watt (Photovoltaik) [%.0f Watt]" (gPhoto,gPhotoWatt) {http="<[batterie:3000:JS(getWattPhoto.js)]"}

getWattPhoto.js:

(function(i) {
	var txt = i.split('service="urn:psi-storage-com:serviceId:Photovoltaics1" variable="Watts" value="');
	var txt2 = txt[1].split('"');
	return txt2[0];
})(input)

Has anyone an idea why it is not working?

I only found this similar topic, but it is about JSONPATH and not Javascript:
https://community.openhab.org/t/error-executing-the-transformation-jsonpath-an-error-occured-while-transforming-json-expression/20366

Thank you!

That is quite a jump.

What is your http.cfg ? I’m assuming you are using the cached configuration since the readme has a completely different format.

With the information provided all I could guess is that the HTTP GET is not returning anything.

Hi Rich,

thank you for your answer. Here the http.cfg

# timeout in milliseconds for the http requests (optional, defaults to 5000)
#timeout=

# the interval in milliseconds when to find new refresh candidates
# (optional, defaults to 1000)
#granularity=

# whether to substitute the current time or state value into the URL
# (optional, defaults to true)
#format=

# configuration of the first cache item
batterie.url=http://192.168.0.12:3400/data_request?id=status&output_format=xml
batterie.updateInterval=3000

In the logs of openhab2 I can see the xml answer from the URL above. It was much to long to post it. Here a short snippet of the relevant part:

<device id="13" status="-1">
<states>
<state id="243" service="urn:psi-storage-com:serviceId:Photovoltaics1" variable="Watts" value="673.3"/>
<state id="244" service="urn:psi-storage-com:serviceId:Photovoltaics1" variable="MaxFeedIn" value="40"/>
<state id="245" service="urn:psi-storage-com:serviceId:Photovoltaics1" variable="PvPeakPower" value="6.25"/>
<state id="246" service="urn:micasaverde-com:serviceId:HaDevice1" variable="Configured" value="0"/>
</states>
<Jobs/>
<tooltip display="0"/>
</device>

What du you mean with cached configuration?

Thank you!

First of all, please read the readme @rlkoshak pointed to you, it shows the correct syntax for items in OH2 utilising the http-binding, which you are NOT using.
Secondly on your question concerning the “cache”;
Using the http-binding, you have the binding updating the information in the set interval. The updated information is held in the channel and can be used, displayed, persisted… in timesteps of your desire without further actual web-requests. Does that answer your question concerning the cache?

Hi Jürgen,

thank you for your answer. Can you please take al look at
http://docs.openhab.org/addons/bindings/http1/readme.html#example-of-how-to-configure-an-http-cache-item

There I found

Number temperature { http="<[weatherCache:10000:XSLT(demo_yahoo_weather_temperature.xsl)]" }

and I think this is the same as mine (short version):

Number Photo_WattsPhoto {http="<[batterie:3000:JS(getWattPhoto.js)]"}

Thank you for the explanation concerning “cache”. I now understand what is meant.

Any other ideas what can be the problem?

Sorry, I must have been looking something else! Yes, your item syntax is correct.
From reading the error, I would say the script is trying to get an array member which doesn’t exist. So you might not in your array “txt” or “txt2” what you think you are having.

Hi Jürgen,

thank you for your fast reply again. You are right I also thought this is the problem, but when I change the getWattPhoto.js for testing to:

(function(i) {
	return 12;
})(input)

I get the same error. And the strange thing is that it is working in openhab1.7 with no problem.
So I think there must be another problem.

I do not know java really good but can you perhaps give me a hint where I can find the file JavaScriptTransformationService.java
So perhaps I can look at line 70 and see what is going on…

Or do you have any other ideas?

Thank you!

That line you do find in here

Hi Jürgen,

thank you for the link. It looks like the error is in this lines:

ScriptEngine engine = manager.getEngineByName("javascript");
engine.put("input", source);

Because the source is shown in my log I think the problem perhaps is a not defined engine. So manager.getEngineByName(“javascript”) cannot find this engine. Is that possible?
I activeted the transform in PaperUI. Is there a way to check if it is working?

Any other ideas?

http://docs.openhab.org/administration/console.html

Use the command bundle:list | grep JavaScript and you should see “Active” in the second column if it is installed and active.

Hi Rich,

thank you for your help. I now found the real problem, while I was trying to use

bundle:list | grep JavaScript 

The response was:

220 | Active   |  75 | 0.9.0.b5               | Eclipse SmartHome JavaScript Transformation Service

So it seemed okay. But for some reason I switched to the log again and uninstalled and reinstalled the http binding. And then I saw the real error:

11:58:44.484 [ERROR] [ab.binding.http.internal.HttpBinding] - given config key 'format' does not follow the expected pattern '<id>.<url|updateInterval>'

So I changed the http.cfg config file (format=false):

# timeout in milliseconds for the http requests (optional, defaults to 5000)
#timeout=

# the interval in milliseconds when to find new refresh candidates
# (optional, defaults to 1000)
#granularity=

# whether to substitute the current time or state value into the URL
# (optional, defaults to true)
format=false

# configuration of the first cache item
batterie.url=http://192.168.0.12:3400/data_request?id=status&output_format=xml
batterie.updateInterval=3000

and now everything works again. That real error is only shown once at the installation and I have not seen it.

I thought the problem is fixed but after restarting openhab there was the problem again. I have not found a solution after a day searching so I now switch back to openhab 1.7 and stay there until there is a new version of the http binding which is working

1 Like