# Use item by current time

Hello i am trying to find out is there possible to read item value by current time.

I have 24 items for every hour and want to send them to a single item when hour change.

Item17(somevalue) to ItemCurrentTime(same value as in Item17 if the time now is 17:00) .

Is there possible to make rule that fallows the time and pick different Item by time, or you need to write rule for every single hour?

Sure it’s possible. Just check inside the rule what time it is and update the Item for that time.

But this reeks of an XY Problem. What are you trying to achieve?

I am reading api values and store them in items, i want to use item1 on 13:00, item2 on 14:00 and so on.

Reading API values how? What binding? How often are the values updated? What are these values? What is the type(s) of the Items? Use them how? What happens tomorrow?

All you’ve done is restate the problem as an XY Problem. An XY Problem is one where you’ve already decided how to solve a problem where another approach might (and usually is) a better solution.

Yes it happens tommorow. Values was strings i converted them to number.
Part of rule

``````when
Time cron "0 1 0 * * *"
then
var result1 = sendHttpGetRequest('https://www.nordpoolgroup.com/api/marketdata/page/59?currency=EUR&endDate=')

var String cena1 = transform ("JSONPATH", "\$.data.Rows[0].Columns[0].Value", result1)

cena01Str.sendCommand(cena1)

var Number formatedNumber = Math::round(((cena01Str.state as Number) / 1000 * 100.00).floatValue()) / 100.00
//logInfo("Cena nākamai dienai pa stundām, String to Number convert", "Formated price is: " + formatedNumber)

cena01Num.sendCommand(formatedNumber)
``````

This isn’t going to work like you think it is. Commands are processed asynchronously. If you send a command to an Item, it is highly unlikely that the Item will have changed state by the next line of code in the rule. It will most of the time still be in the old state.

And on top of that, I’m guessing cena01Str is a String Item. You can’t just cast the state of a String Item to a Number. It’s going top be a String.

And it all seems a bit redundant and pointless. You are pulling a JSON string, extracting a property of it as a String, commanding an Item with the String, then trying (unsuccessfully) to convert that String to a Number, do some math with it and then commanding a different Item.

Why can’t you just command cena01Num in the first place and avoid all this work that frankly isn’t accomplishing anything productive?

``````    val cena1Str= transform("JSONPATH", ...
val cena1Num = Float::parseFloat(cent1Str)
var formatedNumber = Math::round(cena1Num / 1000 * 100.00) / 100.00 // you don't need floatValue, everything is already a float, and this calculation makes absolutely no sense
cena01Num.sendCommand(formatedNumber)
``````

Why `sendCommand` here? Does something immediately happen when the command is received? If all you are doing is changing the state of the Item you should use `postUpdate`.

And none of this shows what you mean by “use item1 at 13:00,…”

Thank you for help i changed a code in section cena01Str postUpdate and now everything work fine.

And none of this shows what you mean by “use item1 at 13:00,…”
I am asking is there possible to read several item values and overwrite them in one item.

Now i am getting item1 is on 13:00(with some value) after that item2 is on 14:00 (with some value) i want to transfer this values to a single item. Like item1 to itemNow than item2 to same item.
Is there possible to use some pattern or i need to make 24 rules?

This is working fine, no error in log and no missed values. Don’t know if its good quality code, but for me as sunday code write it is enough.

``````rule "Cena nākamai dienai pa stundām, String to Number convert  0 1,3 * * * *  1 0 * * * ?"
when
Time cron "0 1,3 * * * * "
then
var result1 = sendHttpGetRequest('https://www.nordpoolgroup.com/api/marketdata/page/59?currency=EUR&endDate=')

var String cena1 = transform ("JSONPATH", "\$.data.Rows[0].Columns[0].Value", result1)

cena01Str.postUpdate(cena1)

var Number formatedNumber = Math::round(((cena01Str.state as Number) / 1000 * 100.00).floatValue()) / 100.00
//logInfo("Cena nākamai dienai pa stundām, String to Number convert", "Formated price is: " + formatedNumber)

cena01Num.sendCommand(formatedNumber)

var String cena2 = transform ("JSONPATH", "\$.data.Rows[1].Columns[0].Value", result1)

cena02Str.postUpdate(cena2)

var Number formatedNumber2 = Math::round(((cena02Str.state as Number) / 1000 * 100.00).floatValue()) / 100.00
//logInfo("Cena nākamai dienai pa stundām, String to Number convert", "Formated price is: " + formatedNumber)

cena02Num.sendCommand(formatedNumber2)
``````

You can do it all in one rule. Trigger the rule once per hour on the hour with a cron expression and test to see what time it is from `now` to determine what to send to the Item.

You could also put this into the HTTP binding with a once per hour polling period and use a Script transformation to update the Item based on the time of day. In Rules DSL the transform would look something like this:

``````val currHour = now.getHour()
transform("JSONPATH", "\$.data.Rows[currHour].Colulmns[0].Value", input)
``````

In JS Scripting the transform would be a little cleaner since JS supports JSON natively:

``````(function(input) {
const hour = time.toZDT().hour();
return JSON.parse(input).data.Rows[hour].Columns[0].Value;
})(input)
``````

What you are doing with these values and the nature of the actual JSON is and this still smells like na XY Problem, so I had to make assumptions.
Then you don’t even need a rule and you just have the one Channel and one Item.

Hmm didn’t know that you can use

data.Rows[currHour] , is this data.Rows time dependent? I was thinking that it can read just a single row and column value. Like row[5] column [6].

I’m assuming that at midnight you’d read row 0, at 1 am you’d read row 1, and so on.

You’ve provided no details so That looks like the pattern of this data. But everything is labeled so generically and there are no details provided (e.g. an example JSON and not just generalities about what you are using it for) I either have to make massive assumptions and hope you figure it out, or provide similarly vague and generic advice and hope you figure it out.