Possibilities with Date/time calculations (JODA Time)

Hello Guys…
can someone please explain a bit more on that the possibilities for using the Jodatime when getting data from the persistence… Like "historicState(now.minusdays(1))… is it possible to create a delta area - meaning last day from 24.00?

When using Designer a handy trick is to type:

now.<ctrl><space>

This will bring up a small dialog with all the methods you can call on now. Looking through the list of methods you will see a now.withTimeAtStartOfDay which gives you midnight on the current day.

A handy trick I’ve used is, for example to get 06:00 today:

now.withTimeAtStartOfDay.plusHours(6)

Hello Rick.
Thanks - that was nice to know with the …

Is there any limitation on how to use etc. now.minusMinutes(1440) i get some strange results if i update 10 seconds after 24.00 - seems like it takes always from the current day and can’t go any further return?

There shouldn’t be a problem but I usually try to avoid operating over midnight like that. Also, keep in mind that joda has no 24:00, only 00:00.
If you want now - 1 day try:

now.minusDays(1)

If you want two hours before midnight today, if this doesn’t work:

now.withTimeAtStartOfDay.minusHours(2)

Try

now.minusDays(1).withTimeAtStartOfDay.plusHours(22)

I’ve used constructs like both of these successfully.

Great - that gives me more about how to construct the JODA…
Thanks :slight_smile:

Hi @rlkoshak

If I want to get a readout of every hour in a day, starting at midnight - how would you do so?

 LastHourPower.postUpdate(TodayPower.deltaSince(now.minusHours(1)))

I’m I in a total direction?

I’d create a for loop that loops from 0 to 23 and inside the loop put the call to persistence.

I’m not at a computer and can’t remember the proper subtract for a for loop like this. It’s something like

for(var i =0; i < 24; i = i + 1) {
    var reading = TodayPower.historicStste(now.withTimeAtStartOfDay.plusHours(i)
} 
1 Like

I’m a novice writing rules, what I’m doing wrong here?


rule "Power Hour Grid" 
	when
	Time cron "0 0 * ? * * *"
	or
        Item EM24_Energi_24_kWh changed
then

for(var i =0; i < 24; i = i + 1) {
 var reading = TodayPower.historicStste(now.withTimeAtStartOfDay.plusHours(i))
 postUpdate(LastHourPower, reading)
 }    
 end

historicState

I strongly recommend VSC for syntax checking:

I’m still struggling with this rule - and corrected the syntax .

I think I found a solution :slight_smile:

Is there a similar approach for with start of Month like:
historicState(now.withTimeAtStartOfMonth,“jdbc”).state.toString
This one above doesn’t work.

I would like to get a number value (total power consumption) from the beginning of the month for a calculation.

I think, I found what I need:
…triggeringItem.historicState(now.withTimeAtStartOfDay.withDayOfMonth(1)).state.toString

1 Like