With current hour reading json string. Midnight value

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

What’s the JSON object looks like?

In question of the offset:

var row = now.minusHours(1).getHour

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},

So you’re looking for

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)

I tryied this line but, i get this error

d != java.time.ZonedDateTime in test , i am running OH3.2

sorry, forgot the .getHour

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.

rules.when().cron('0 0 21 * * ?').then(() => {
  const request = actions.HTTP.sendHttpGetRequest('https://www.nordpoolgroup.com/api/marketdata/page/59?currency=EUR&endDate=');
  console.debug('Request = ' + request);
  const parsed = JSON.parse(request);
  const row = time.toZDT('PT-1H').hour(); // the JS version of Udo's approach
  items.cenaNowStr.postUpdate(parsed.data.Rows[row].Columns[1].Value;
}).build('Cena NOW');

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.

rules.when().cron('0 0 21 * * ?').then(() => {
  const parsed = JSON.parse(actions.HTTP.sendHttpGetRequest('https://www.nordpoolgroup.com/api/marketdata/page/59?currency=EUR&endDate='));
  items.cenaNowStr.postUpdate(parsed.data.Rows[time.toZDT('PT-1H').hour()].Columns[1].Value;
}).build('Cena NOW');

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.

it fixed the error now it shows

2024-01-08 20:59:16.792 [WARN ] [b.core.model.script.actions.BusEvent] - Cannot convert '{“data”:{“Rows”:[{“Columns”:

Never had deal with .js rules. Where you need to save them? Under openhab-conf/Script folder?

See JavaScript Scripting - Automation | openHAB for the full docs.

I have …conf/automation/jsr223 folder is empty, should i creat clear …conf/automation/js?

You have to install the JS Scripting binding. Once installed, as documented at the link I posted you will either create rules through the UI or

The JS Scripting binding will load scripts from automation/js in the user configuration directory.

Installing the add-on and creating UI rules is covered at:

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.

Argh… another typo, obviously…

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)

It still reads all lines, all values for all week.
Basically everything that you can see in this link.

https://www.nordpoolgroup.com/api/marketdata/page/59?currency=EUR&endDate=

{“data”:{“Rows”:[{“Columns”:[{“Index”:0,“Scale”:0,“SecondaryValue”:null,“IsDominatingDirection”:false,“IsValid”:false,“IsAdditionalData”:false,“Behavior”:0,“Name”:“10-01-2024”,“Value”:“87,15”,“GroupHeader”:null,“DisplayNegativeValueInBlue”:false,“CombinedName”:“10-01-2024”,“DateTimeForData”:“0001-01-01T00:00:00”,“DisplayName”:“87,15_True”,“DisplayNameOrDominatingDirection”:“87,15”,“IsOfficial”:true,“UseDashDisplayStyle”:false},

Hmm… jsonpath.com gives one value…

But openHAB does, in fact, give the whole JSON Object.
I have to do more testing…

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.