Jsonpath noob question

Tags: #<Tag:0x00007fc20ff45c08>

Hi
I have a device (an ESP32) which returns the following when pinged:

[{“deviceId”:“163350134616381”,“water”:“709”,“humidity”:" 71.00",“temperature”:" 27.00"}]

I have the JSONPath Transformation installed.
The HTTP Binding is installed.
I recognize that this is an Openhab1 binding so I understand I am not to use PaperUI but rather items and rules files alone…

My items file reads as follows:

String Temperature_json "Temperature [JSONPATH($.temperature):%s °C]" { http="<[http://192.168.0.150:81/:6000:REGEX(.*?<html>(.*?)</html>.*)]" }
Number Temperature "Temperature [%1.f °C]"

My rules file reads as follows:

rule "Temperature"
when
        Item Temperature_json changed
then
        val newValue = transform("JSONPATH", "$.temperature", Temperature_json.state.toString)
        Temperature.postUpdate(newValue)
end

My sitemaps file reads as follows:

Text item=Temperature label="Current Temperature [%s]" icon="temperature"

However my sitemap shows a “-” where a figure for the temperature should be visible.

Frontail log provides the following error message:

2020-02-13 00:01:55.327 [WARN ] [rest.core.item.EnrichedItemDTOMapper] - Failed transforming the state ‘NULL’ on item ‘Temperature_json’ with pattern ‘JSONPATH($.temperature):%s °C’: Invalid path ‘$.temperature’ in ‘NULL °C’

with subsequent errors:

2020-02-13 00:01:58.927 [ERROR] [org.openhab.io.net.http.HttpUtil ] - Fatal transport error: java.net.SocketTimeoutException: Read timed out

2020-02-13 00:01:58.931 [ERROR] [ab.binding.http.internal.HttpBinding] - No response received from 'http://192.168.0.150:81/

Would be grateful for your advice.

thanks

You should start debugging at this point. Do you get any response if you enter that URL in a browser ???

Firstly, thank you for the reply !!

Yes, the response on entering the URL into a browser is the text below:

{"deviceId":"163350134616380","water":"806","humidity":" 60.00","temperature":" 26.00"}

Any advice welcome !

Give it a try without the Regex expression

Tried this:

String Temperature_json "Temperature [JSONPATH($temperature.water):%s °C]" { http="<[http://192.168.0.150:81/:6000]" }

but no luck…

Error log ?

2020-02-13 00:46:21.331 [ERROR] [el.item.internal.GenericItemProvider] - Binding configuration of type ‘http’ of item ‘Temperature_json’ could not be parsed correctly.

org.eclipse.smarthome.model.item.BindingConfigParseException: bindingConfig ‘http://192.168.0.150:81/:6000’ doesn’t represent a valid in-binding-configuration. A valid configuration is matched by the RegExp ‘(.?)({.})?:(?!//)(\d*):(.*)’

have tried this Syntax:

BTW:
Is there a value in the item, if you’re using it in this way

String Temperature_json "Temperature [%s °C]" { http="<[http://192.168.0.150:81/:6000:REGEX(.*?<html>(.*?)</html>.*)]" }

… without JSON ?

Thanks - though doesn’t seem to help - have changed in both item and rules files.

Is it returning this

[{“deviceId”:“163350134616381”,“water”:“709”,“humidity”:" 71.00",“temperature”:" 27.00"}]

Or this

{"deviceId":"163350134616380","water":"806","humidity":" 60.00","temperature":" 26.00"}

If the former, then doesn’t it need to be handled as an array (with one element)?

So, if it is an array, try this.

val newValue = transform("JSONPATH", "$.[0].temperature", Temperature_json.state.toString)

Judging from your error message I would say your usage of the http-binding is wrong. You have to enter the URL in your http.cfg like

tempJSON.url=http://192.168.0.150:81/
tempJSON.updateInterval=6000

and the define your item like

Number Temperature "Temperature [%.1f °C]" { http="<[tempJSON:6000:JSONPATH($.temperature)]" }

When using the JSONPATH-transformation in the item you don’t need the rule, it is alredy transformed there. If you want just the JSON returned from the URL then leave out the tranformation in your Item (and make it a String-item)

Edit: Just saw, that you can use the URL directly in the item definition, but I’m not sure that you can use an URL with a port in it, because the “:” is interpreted as a separator.

2 Likes

Yes you can. The original post shows it was pulling the url with the specified port.

This is the original failure that has to be addressed.

@namraccr the error you mention would mean, that the server under that URL is not answering. Later he also mentiond this error:

For me this looks like that the binding doesn’t accept the port-part of the URL. So the first thing I would try is to do this in the http.conf and see if it works.

No, the problem with this later string is that it is missing the third part. It has no transform:

http="<[<url>:<refreshintervalinmilliseconds>:<transformationrule>]"

Ahh, OK. Didn’t see that he left out the “REGEX” there. Then he should try

Number Temperature "Temperature [%.1f °C]" { http="<[http://192.168.0.150:81/:6000:JSONPATH($.temperature)]" }

Thanks all for your efforts.
I am coming to the conclusion that this is a problem with the device I am trying to ping, rather than with the java code.

The device I am trying to reach (an ESP32) can be reached by a webpage / IP address and displays:

[{"deviceId":"163350134616380","water":"693","humidity":" 87.00","temperature":" 29.00"}]

as expected; however this is not the case when I ping the same device using Openhab using the various code snippets provided above.

I am using an ESP32 in this case. I am especially suspicious as I appear to be able to get reasonable results from an ESP8266 using similar code.

If anyone is aware of any gotchas when trying to make requests of an ESP 32 when using Openhab I’d be delighted to know about it.

many thanks

Mark

Maybe the device you run OpenHAB on has no access to your URL. Do you get a response when you do

curl http://192.168.0.150:81/

on this device?