openHAB 5.1: averageSince issue

Hi,

I’m running openhab 5.1 since a few days and it looks like averageSince is not working as excpected:

Script:

const past10Minute = time.ZonedDateTime.now().minusMinutes(10)
const itmSolar = items.getItem("mqtt_solar_energy_current")
const currentValue = itmSolar.rawState
const solarLastHourState = itmSolar.persistence.averageSince(past10Minute)
const solarLastHour = parseFloat(itmSolar.persistence.averageSince(past10Minute).rawState)
const solarCount = itmSolar.persistence.countSince(past10Minute)
const solarSum = parseFloat(itmSolar.persistence.sumSince(past10Minute).rawState)
const solarAvg = solarSum / solarCount

console.log("averageSince(", past15Minute, ") =", solarLastHourState)
console.log("averageSince(", past15Minute, ").rawState =", solarLastHour)
console.log("countSince(", past15Minute, ") =", solarCount)
console.log("sumSince(", past15Minute, ") =", solarSum)
console.log(" ", solarSum, "/", solarCount, "=" , solarAvg)

Console output:

averageSince( 2026-05-16T18:58:09.645+02:00[Europe/Berlin] ) = PersistedState (State=-730.9234180105466 W)
averageSince( 2026-05-16T18:58:09.645+02:00[Europe/Berlin] ).rawState = -730.9137161275914
countSince( 2026-05-16T18:58:09.645+02:00[Europe/Berlin] ) = 2
sumSince( 2026-05-16T18:58:09.645+02:00[Europe/Berlin] ) = 1393.9
1393.9 / 2 = 696.95

The corresponding data in the database (MariaDB)

image

The sum is correct: 671.7 + 722.2 = 1393.9. How could a average of only positive values be negative?

Has anyone any ideas?

What is the current state of your item? `averageSince` may also consider the current state if different from the last persisted state. `averageSince` also calculates a weighted average. So it will consider the 5 minutes between the 2 considered values for the first value and the time to now for the last value. `Sum` does not consider any time dimension (and therefore is of limited use in my view, the better methods are `RiemannSum`).

I still don’t understand why it would be negative though.

Thanks for your input. In my case it makes no difference between Sum and RiemannSum because the values are stored continuously every 5 minutes. Therefore the time a value is present is constant, ergo equals weights. I also intentionally pick a time where the current item state equals the last state stored. The only difference could be, that the current state is considered twice (last and current). But the average differs a lot even if the backend consider the last state twice.

I will play the next days a little bit more with the persistence functions und see what I could find out. It looks like, that I’m the first who run into this issue.

It will make a difference as the current time is never exactly 5 minutes after the last persisted value. And the last value will only be considered proportionally for the time between last persisted value and now. The same is true for averageSince.

From a mathematical POV, you are right, but let’s say it this way: In my case it will make no significantly difference for me. It doesn’t matter here for me if the value is 1234.12 or 1238.21. Because I want only get a rough estimate about the produced power from my little solar power plant for my own interest. But what matters is, that I got sometimes an average of 4000 where I would expect 1234.12 and sometimes -700, which makes no sense at all…

Can you switch on debug logging or org.openhab.persistence.jdbc and org.openhab.core.persistence? The retrieved values and calculations are not logged I believe, but at least the SQL query is and the number of values retrieved. Maybe there is something to be learned from that.

I’ve found the issue: For some reason the timezone of the OS (Ubuntu Server 24.04 LTS) was set to UTC. I set up openHAB with timezone UTC+1 (Berlin, Rome etc.). Therefore there exists a miss match in the set up timezones. After changing the OS timezone to UTC+1 and reboot the machine, everything worked fine.