Hello dont have any ideas any more for this problem. I am reading value from json string. Eaxample here
rule "Cena NOW"
when
Time cron " 15,30 0,1 * ? * * "
then
var row = (now.getHour() - 1)
var Nowresult1 = sendHttpGetRequest('https://www.nordpoolgroup.com/api/marketdata/page/59?currency=EUR&endDate=')
var String cenaNow = transform ("JSONPATH", "$.data.Rows[" + row +"].Columns[1].Value", Nowresult1)
cenaNowStr.postUpdate(cenaNow)
end
I am have problem to read value under “$.data.Rows[” + row +"] at midnight, because the values starts with offset.
row = 0 has value fom 0:00-1:00
row = 1 has value fom 1:00-2:00
row = 2 has value from 2:00-3:00
and so on till
row = 23 has value from 23:00-0:00
so basicly i need 1hour offset everything is fine till (now.getHour() - 1) is at 0:00. Because 0-1 = -1 not 23.
Mby someone have some good idea how to solve this?
Thankyou for help
Thankyou for fast respons i’ll check if this do the trick.
Here is line from json
{“Index”:1,“Scale”:0,“SecondaryValue”:null,“IsDominatingDirection”:false,“IsValid”:false,“IsAdditionalData”:false,“Behavior”:0,“Name”:“07-01-2024”,“Value”:“169,50”,“GroupHeader”:null,“DisplayNegativeValueInBlue”:false,“CombinedName”:“07-01-2024”,“DateTimeForData”:“0001-01-01T00:00:00”,“DisplayName”:“169,50_True”,“DisplayNameOrDominatingDirection”:“169,50”,“IsOfficial”:true,“UseDashDisplayStyle”:false},
val name = String.format("%02d",now.minusHours(1)) + " - " + String.format("%02d",now)
val path = "$.data.rows[?(@.Name=='" + name + "')].Columns[?(@.Index=='1')].Value"
var String cenaNow = transform("JSONPATH", path, Nowresult1)
val name = String.format("%02d",now.minusHours(1).getHour) + " - " + String.format("%02d",now.getHour)
val path = "$.data.rows[?(@.Name=='" + name + "')].Columns[?(@.Index=='1')].Value"
var String cenaNow = transform("JSONPATH", path, Nowresult1)
I really recommend that if you have to deal with JSON to use JS as the rule to process the data rather than the transform Action and JSONPATH. It almost always is going to be simpler and more straight forward. the “JS” in JSON stands for “JavaScript” after all.
And you can use the rule builder in a .js file to make this rule really simple and streamlined, or you can use the UI.
I’ve purposely make this a little more verbose than it needs to be for clarity. Once tested I’d probably collapse it to the following unless I periodically encounter errors.
In the UI you’d just set the cron trigger and create a Script Action with the two lines inside the then function above.
The advantage to parsing the JSON natively is you can access, loop through it, and manipulate it in a much more straight forward way than indirectly extracting stuff using JSONPATH.
Everything you need to know about JS Scripting rules including file locations, syntax, UI vs. file based is documented and demonstrated in these and the link above.
val name = String.format("%02d",now.minusHours(1).getHour) + " - " + String.format("%02d",now.getHour)
val path = "$.data.Rows[?(@.Name=='" + name + "')].Columns[?(@.Index=='1')].Value"
var String cenaNow = transform("JSONPATH", path, Nowresult1)