Where's the time difference? (OH3)

Hi all

I migrated most of my rules. Particularly in one of the energy calculations, there’s a difference now.
I use the power meter value of the beginning of the year together with the current value and calculate the estimated consumption by the end of the current year.

The database was migrated, so all values are the same. But comparing OH2 with OH3, the calculated values are slightly off.

Here are the conversions I’ve used:

oh2: val day = now.getDayOfWeek
oh3: val day = now.getDayOfWeek.getValue

oh2: val hour = now.getHourOfDay
oh3: val hour = now.getHour

oh2: val currentMonth = now.getMonthOfYear
oh3: val currentMonth = now.getMonthValue

oh2: .historicState(new DateTime(now.getYear,1,1,0,0,0,0)).state as Number
oh3: .historicState(DateTimeType.valueOf(now.getYear+"-01-01").getZonedDateTime).state as Number

oh2: .historicState(now.withTimeAtStartOfDay).state as Number
oh3: .historicState(now.with(LocalTime.of(0,0,0,0))).state as Number

oh2: val beginningOfYearEpoch = now.withDayOfYear(1).withMillisOfDay(0).millis
oh3: val beginningOfYearEpoch = now.withDayOfYear(1).with(LocalTime.of(0,0,0,0)).toInstant.toEpochMilli

oh2: val endOfYearEpoch = now.withMonthOfYear(12).withDayOfMonth(31).withHourOfDay(23).withMinuteOfHour(59).millis
oh3: val endOfYearEpoch = now.withMonth(12).withDayOfMonth(31).withHour(23).withMinute(59).toInstant.toEpochMilli

oh2: now.millis
oh3: now.toInstant.toEpochMilli

My guess is, that beginningOfYearEpoch is wrong, maybe something with timezones? So I end up getting the value of 31.12.2020 23:00 instead of 01.01.2020 00:00.

Can someone spot the difference ?

Thanks a lot

2 Likes

I guess what I would do is log out each of those lines in OH 2.5 and OH 3 and compare what gets returned. That should show you which line appears to be giving a different result which will narrow the search.

Hey Rich

You’re absolutely right. I went through the code step by step with log messages and found the problem.
Had nothing to do with the calculations I posted above :blush:

I use two internal counters to calculate the total energy consumption (one for high, one for low tariff).
Turns out, that these two counters were slightly off in OH3 probably due to some bad timing in migrating the items and the database.

Used the API to push the same values from OH2 to OH3 and tadaa, everything is in sync now.

Thanks for the hint :+1: