Jsonpath transform in OH2 http binding

Hi, I’m seeing the following error in OH2 (today’s cloudbees build), which isn’t present in OH1. I’ve only just installed/started with OH2 and have copied everything (sitemap/items/rules) verbatim from OH1

[.o.b.http.internal.HttpBinding] - transformation throws exception [transformation=JSONPATH($…currentPower[0].power), response={“overview”:{“lastUpdateTime”:“2015-12-02 15:32:01”,“lifeTimeData”:{“energy”:2605638.0,“revenue”:423.67673},“lastYearData”:{“energy”:2626275.0},“lastMonthData”:{“energy”:2439.4055},“lastDayData”:{“energy”:1007.95056},“currentPower”:{“power”:0.0},“measuredBy”:“INVERTER”}}]
org.openhab.core.transform.TransformationException: An error occured while transforming JSON expression.
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:63) ~[org.openhab.core.compat1x_2.0.0.201512010302.jar:na]
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:182) ~[na:na]
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156) [org.openhab.core.compat1x_2.0.0.201512010302.jar:na]
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173) [org.openhab.core.compat1x_2.0.0.201512010302.jar:na]

item definition:

Number	Solar_CurrentPower	"Current Solar Power [%.1f W]"	<sun>	(Outdoor_Front,MyOpenHAB)		{http="<[solaredgeenergycache:300000:JSONPATH($..currentPower[0].power)]"}

Is there another way? Is this syntax no longer supported or is it a bug?

1 Like

Do you have the complete stacktrace available? I would assume there is some exception from the JSONPath transformation code, which should hint at the problem.

That was the entirety of the error text in the log file

It is truncated by default. Please change the logging setting in https://github.com/openhab/openhab2/blob/master/distribution/openhabhome/runtime/etc/logback_debug.xml#L12
and replace the ex{10}%n by ex%n and you should see the full stack trace, I hope.

Thanks I’ll try this later and report back
EDIT: I get a slightly different, but not longer error:

[.o.b.http.internal.HttpBinding:189  ] - transformation throws exception [transformation=JSONPATH($..currentPower[0].power), response={"overview":{"lastUpdateTime":"2015-12-03 07:56:22","lifeTimeData":{"energy":2605638.0,"revenue":423.67673},"lastYearData":{"energy":2626275.0},"lastMonthData":{"energy":2439.4055},"lastDayData":{"energy":0.0},"currentPower":{"power":0.0},"measuredBy":"INVERTER"}}]org.openhab.core.transform.TransformationException: An error occured while transforming JSON expression.
at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:63)
at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:182)
at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:156)
at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:173)

EDIT2: The code uses Throwable.getMessage() which from what i’ve read may not return the full stack trace

With $..currentPower[0] you’re addressing a JSON array, but in your server response currentPower is an object. Try to leave out the [0] and it should work. At least then it’s the correct syntax for the JSON response.

Thanks @misc - I’ve tried that and realised the reason I originally added [0] to the jsonpath in OH1.
In OH2 with your change (remove [0] indexer) I see [890.3479] displayed rather than just 890.3479
It seems in OH1 adding the [0] gets you the just the number, but in OH2 it throws an error instead.

solved by swapping JSONPATH for a JS transform, with a .js file in the transforms folder. This way It returns the number as expected (rather than [number])

OH1 uses json-path-0.9.0, which is very different from json-path-2.0.0 that Eclipse SmartHome currently uses. (There is a json-path-2.1.0 available since 22 November.) I strongly suspect that this is just the result of major changes between those two releases, which means that some rules and item binding strings from OH1 that use the JSONPATH transform may not work the same in ESH/OH2 The alternative would then be to roll back the version used in ESH, but that doesn’t seem right.

From what I read in JSonPath doc, the $… syntax is aimed to target properties within arrays but in your case we’re not dealing with array. I guess this is more about a lack of control in the JSonPath library used in OH1 than a breaking change in OH2.

By the way, you would directly by able to address desired value with :

$.overview.currentPower.power

If JsonPath strings produce different results in OH2 than how they do in OH1, then I would call it a breaking change. But if in every case the JsonPath string or receiving code can be changed to work in OH2, it would not be that big of an issue.

I believe it is a breaking change, OH2 is working to JSONPATH spec - in that from what i’ve read every result should be returned as an array. I failed to find a way to get OH2 to return a scalar value, it behaves exactly like https://jsonpath.curiousconcept.com/ and either returns nothing/an error or an array.

@richbeales : $.overview.currentPower.power does not return an array in OH2 but a scalar value, while $…power does

@glhopital I could have sworn I tried that, It’s possible I only tried it in the online json testers, but anyway, it works, so thank you.

@richbeales did you ever get this working as a rule using jsonpath? I can see you’re trying to parse neurio data and I’ve been working on the same thing.

I had it working as separate calls via items, but i wanted more complex rules setup and switched. However, in the rules I’m not able to get json transform working. Did you manage to setup rules for your neurio data?

Hi yes glhopital’s suggested fix worked fine for me

are you able to provide me your final working rule? Whenever I try to use the transform “JSONPATH” function i get an error that the type doesn’t exist. (yet it’s installed and works find when parsing from an item file)

Apologies I’m only using it in items file not as a rule however it sounds like you might be missing an import statement at the top of the file

Hmm, interesting. I’ll have to poke around to see what that include is. If you know off hand please post.

Was wondering if you could explain how you are getting the data from your Neurio? Are you using the API or scraping it from the local page?

I am fairly new to openHAB and have not used the JSONPATH before. I would like to be able to grab the current usage from Neurio and display it on habpanel.

Thanks
B