How calculate deltaSince firstDayOfYear

  • Platform information:
    • Hardware: Docker
    • openHAB version: 3.4.2

Hello, I am looking how I can calculate the delta since the beginning of the year. I have already tried it with different variants (LocalDateTime, Eppoch etc.), but always despair at the OH3 Date Time Conversion.

val ZonedDateTime firstDayThisYear = now.withMonth(1).withDayOfMonth(1).withHour(0).withMinute(0).withSecond(0).withNano(0) 
logInfo("TEST","Since " + firstDayThisYear + " Consumption: " +(VI_PowerConsumption_TV_Total.deltaSince(firstDayThisYear) as Number))
2023-02-14 14:12:02.521 [INFO ] [org.openhab.core.model.script.TEST  ] - Since 2023-01-01T00:00+01:00[Europe/Berlin] Consumption: null

I’m assuming Rules DSL.

Given this error, you’ve got the ZonedDateTime stuff working just fine. The problem is the call to persistence. You could make the calculation of the first day of the year a little shorter but based on the log what you have there works.

Do you have data that goes back that far? For most of the databases, if there isn’t data around the time time you request it will return null. You can look at a chart or call historicState and see if it returns anything.

yes DSL Rule.

Yes you are right, the oldest data is from 20th January. :face_with_peeking_eye: :confused:
If I set firstDayThisYear to day 20, I get a value back. Is there a way that it just uses the oldest value if there is no data to 01.01?

No, you’ll just have to wait until next year and change the 20th back to the 1st.

Too bad, ok then I have to check all that measure Things and set the correct date.

Thanks for your help.