JSONPath Transformation with Kaiterra Laser Egg

  • Hardware: Raspberry Pi 3 model B

  • OS: Raspbian 10 Buster

  • Java Runtime Environment: openjdk version “1.8.0_252”
    OpenJDK Runtime Environment (Zulu 8.46.0.225-CA-linux_aarch32hf) (build 1.8.0_252-b225)
    OpenJDK Client VM (Zulu 8.46.0.225-CA-linux_aarch32hf) (build 25.252-b225, mixed mode, Evaluation)

  • openHAB version: 2.5.2

Add-ons installed:

  • JSONPath Transformation
  • HTTP binding
  • RegEx Transformation

Hi everyone,

I have a Kaiterra Laser Egg 2 and I would like to extract certain data such as the pm2.5 and temperature.
I have registered my device UUID and have received my API key from the Kaiterra developer dashboard.

When I run this command from the OpenHAB console:

$ curl -v "https://api.kaiterra.cn/v1/lasereggs/1234abcd567-12ab34-12ab34-1234adcd567?key=API-KEY"

I receive the following response:

{"id":"1234abcd567-12ab34-12ab34-1234adcd567","info.aqi":{"ts":"2020-06-23T04:56:06Z","data":{"humidity":79.53,"pm10":19,"pm25":19,"temp":24.5}}}

So far, so good.

Reading over the forums as a guide, this is the configuration so far…

http.cfg:

laseregg.url=https://api.kaiterra.cn/v1/lasereggs/1234abcd567-12ab34-12ab34-1234adcd567?key=API-KEY
laseregg.updateInterval=60000

.items

String lasereggID     "Laser Egg ID [%s]"      { http="<[laseregg:60000:JSONPATH($.id)]" }
Number lasereggPm25   "Laser Egg pm2.5 [%.1f]"  { http="<[laseregg:60000:JSONPATH($.[{info.aqi}].data.pm25)]" }

.sitemap

Text item=lasereggID    // used this to test if the UUID of the deivce is returned - yes it works
Text item=lasereggPm25 

The lasereggID text item on the .sitemap returns the correct UUID of the device, so this tells me that http.cfg is pulling the data. However, I receive this error for the pm25 data:

2020-06-23 21:54:15.603 [WARN ] [ab.binding.http.internal.HttpBinding] - Transformation 'JSONPATH($.[{info.aqi}].data.pm25)' threw an exception. [response={"id":"1234abcd567-12ab34-12ab34-1234adcd567","info.aqi":{"ts":"2020-06-23T13:53:40Z","data":{"humidity":78.78,"pm10":3,"pm25":3,"temp":24.75}}}]
org.openhab.core.transform.TransformationException: An error occurred while transforming JSON expression.
	at org.openhab.core.transform.TransformationHelper$TransformationServiceDelegate.transform(TransformationHelper.java:71) ~[bundleFile:?]
	at org.openhab.binding.http.internal.HttpBinding.execute(HttpBinding.java:218) [bundleFile:?]
	at org.openhab.core.binding.AbstractActiveBinding$BindingActiveService.execute(AbstractActiveBinding.java:146) [bundleFile:?]
	at org.openhab.core.service.AbstractActiveService$RefreshThread.run(AbstractActiveService.java:169) [bundleFile:?]

And this is where I am stuck. I have tried to create a .rules that would change the extracted data for pm25 and display it in my .sitemap but all my attempts have failed. I have been starting at my monitor for two days and reading and re-reading the forums to try to figure out what the problem could be. I don’t have the .rules code I tried as I have tried so many iterations I’ve lost count. The closest I have come is a post on the German version of the OpenHAB forums, but no further ( I had to use Google Translate as I don’t read/speak German).

I am sure there is a simple explanation here and I’m hoping that a fresh pair of eyes and someone with more experience will be able to point out the problem. In a nutshell:

  1. Is this the most efficient configuration?
  2. If so, do you have any advice on what the correct transformation would be?
  3. If not, suggestions are welcome!

Thanks for your time,
-Ben

Not sure what all the brackets are about in your JSONPATH. Here is a recent similar thread

Your expression looks bad due to the period in info.api… and all the brackets. Try using the bracket notation…

"$['info.aqi'].data.pm25"

Thank you @rossko57 and @5iver for your replies.

The problem was the brackets facepalm. From a few other threads I had looked at others had used

["info.aqi"]   OR  [(info.aqi)]   OR   [{info.aqi}]

I hadn’t tried the single quotes ’ ’ yet.
So I don’t know how they got theirs working. Thanks very much - I thought it would be something trivial - I appreciate your time and suggestions.

1 Like