JSONPATH, Items and Rules

Good morning at all, I’m new to openhab.
I just installed on Raspberry Pi.
I would like to read every hour an HTTP service that returns a JSON string.
The string contains an array and I want the last value (the most recent one).
I created insertion in the “items” file and then a transformation into the “rules” file.
Below the entries:
ITEMS:
String Sensor “My_Sensor” {http = "<[url? Param1=%1$tY-%1$tm-%1$tdT%1$tH:00&param2=%1$tY-%1$tm-%1$tdT%1$tH:59:60000:JSONPATH($.Array[(@.Length-1)])]}

RULES:
Import java.lang.String

Rule “Sensor1"
When
Time cron “0 0/1 * 1/1 *? *“
then
Var String FP_data = My_Sensor.toString
Var String FP_d = FP_data.substring (FP_data.indexOf (”[”] + 1, FP_data.indexOf (”]"))
LogInfo ("— RULE — Sensor1: JSON", FP_d)
Var String v_tm = transform (“JSONPATH”, “$ .datetime_utc”, FP_d)
Var String v_l = transform (“JSONPATH”, “$ .light”, FP_d)
Var String v_batt = transform (“JSONPATH”, “$ .battery”, FP_d)
LogInfo ("— RULE — Time__", v_tm)
LogInfo ("— RULE — Light_", v_l)
LogInfo ("— RULE — Batt__", v_batt)
PostUpdate (FP_Time, v_tm)
PostUpdate (FP_Light, v_l)
PostUpdate (FP_Batt, v_batt)
end

The first problem is that the service parameters are in UTC and I’m using Local Time. How can I send UTC Time into ITEM?
Secondly,in the ITEM [ JSONPATH($.Array[(@.Length-1)])] if the response string is empty, I get an error.
How do I handle it and not send anything?
What I did is the correct parameterization?

Thank you
Matteo

If your code is copied straight from the files… well, there are a lot of typos.

First is at the end of the item definition, you forgot a "

String Sensor "My_Sensor" {http = "<[url? Param1=%1$tY-%1$tm-%1$tdT%1$tH:00&param2=%1$tY-%1$tm-%1$tdT%1$tH:59:60000:JSONPATH($.Array[(@.Length-1)])]"}

I’m pretty sure the rest of the definition is not correct either, for example there is no “http://”, see http://docs.openhab.org/addons/bindings/http1/readme.html#item-configuration

In your rule, you wrote the most key words with a capital. This wrong

import java.lang.String

rule "Sensor1"
when
    Time cron "0 0/1 * 1/1 *? *"
then
    var String FP_data = Sensor.toString
    var String FP_d = FP_data.substring (FP_data.indexOf ("[") + 1, FP_data.indexOf ("]"))
    logInfo ("--- RULE --- Sensor1: JSON", FP_d)
    var String v_tm = transform ("JSONPATH", "$ .datetime_utc", FP_d)
    var String v_l = transform ("JSONPATH", "$ .light", FP_d)
    var String v_batt = transform ("JSONPATH", "$ .battery", FP_d)
    logInfo ("--- RULE --- Time__", v_tm)
    logInfo ("--- RULE --- Light_", v_l)
    logInfo ("--- RULE --- Batt__", v_batt)
    postUpdate (FP_Time, v_tm)
    postUpdate (FP_Light, v_l)
    postUpdate (FP_Batt, v_batt)
end

Item definition was for Sensor, FP_data is built from My_Sensor, in line 6 there was a ] instead of ).

Did you try Smarthome Designer yet?

1 Like

Thank you!
I have modified the Item string with:
String Sensor "My_Sensor" {http = "<[https://my_url.com?param1=%1$tY-%1$tm-%1$tdT%1$tH:00&param2=%1$tY-%1$tm-%1$tdT%1$tH:59:60000:JSONPATH($.samples[(@.length-1)])]"}

But I have this error:

11:49:18.821 [ERROR] [ab.binding.http.internal.HttpBinding] - Transformation ‘JSONPATH($…samples[(@.length-1)])’ threw an exception.

If in transformation I write JSONPATH($..samples[0]) no error was found.
But I search the last item of the array… What mistake I’m doing?