Opendata JSON read out rule for the highest/latest entry

hi openhab community

this is my first topic and I hope I’m doing it right.

I try to ready out some data from a Opendata JSON file. The read out works fine, but I don’t now how to read out the latest entry on the file. they just add the newest entry (result.) at the end (highest number). so I want to read out the highest result…

this is the JSON link:

https://tecdottir.herokuapp.com/measurements/tiefenbrunnen

and this is my rule…

rule “Wasser & Luft - Tiefenbrunnen” // all 15 min
when
Time cron "0 0/15 * 1/1 * ? * "
then
var String data = sendHttpGetRequest(“https://tecdottir.herokuapp.com/measurements/tiefenbrunnen”)
var String varWaTi1 = transform (“JSONPATH”, “$.result.[0].values.timestamp_cet.value”, data)
var String varWaTi2 = transform (“JSONPATH”, “$.result.[0].values.air_temperature.value”, data)
var String varWaTi3 = transform (“JSONPATH”, “$.result.[0].values.humidity.value”, data)
var String varWaTi4 = transform (“JSONPATH”, “$.result.[0].values.water_temperature.value”, data)
WaLu_Tiefenbrunnen_01.sendCommand(varWaTi1)
WaLu_Tiefenbrunnen_02.sendCommand(varWaTi2)
WaLu_Tiefenbrunnen_03.sendCommand(varWaTi3)
WaLu_Tiefenbrunnen_04.sendCommand(varWaTi4)
logInfo(“Wasser & Luft - Tiefenbrunnen”, varWaTi4)
end

so by the JASONPATH after the result. it should count up and take the highest number. but how can I manage it. it’s probably easy going, but I’m totally new here…

thank you already for every little help…

How to count elements in a JSON array is shown here

If you only want the last element, you need not iterate but can go to it directly.
Remember elements start at zero, so the last element of an array with length 17 is [16].

thank you for the hint…

this is my woking result:

rule “Wasser & Luft - Tiefenbrunnen” // alle 15 min

when
    Time cron "0 0/15 * 1/1 * ? *" 
then
    var String data = sendHttpGetRequest("https://tecdottir.herokuapp.com/measurements/tiefenbrunnen")
    val int numElements = Integer.parseInt(transform("JSONPATH", "$.result.length()", data))
    val MinusEins = numElements - 1
    var String varWaTi1 = transform ("JSONPATH", "$.result.[" + MinusEins + "].values.timestamp_cet.value", data)
    var String varWaTi2 = transform ("JSONPATH", "$.result.[" + MinusEins + "].values.air_temperature.value", data)
    var String varWaTi3 = transform ("JSONPATH", "$.result.[" + MinusEins + "].values.humidity.value", data)
    var String varWaTi4 = transform ("JSONPATH", "$.result.[" + MinusEins + "].values.water_temperature.value", data)
    WaLu_Tiefenbrunnen_01.sendCommand(varWaTi1)
    WaLu_Tiefenbrunnen_02.sendCommand(varWaTi2)
    WaLu_Tiefenbrunnen_03.sendCommand(varWaTi3)
    WaLu_Tiefenbrunnen_04.sendCommand(varWaTi4)
    logInfo("Wasser & Luft - Tiefenbrunnen", varWaTi1)

end

1 Like

You’d normally postUpdate() your Items with new information states, it’s more efficient than using sendCommand() for “do something” instructions.

You don’t need to calculate MinusEins:

var String varWaTi1 = transform ("JSONPATH", "$.result.[(@.length-1)].values.timestamp_cet.value", data)

should be sufficient. Even

var String varWaTi1 = transform ("JSONPATH", "$.result.[-1:].values.timestamp_cet.value", data)

should do the job.

1 Like