JensA
(Jens A)
August 4, 2026, 5:39am
1
I want to use riemanSum to get daily PV generation but the calculated sum seems too high.
OpenHab 5.1.4
InfluxDb persistence
BKW_curpower is the power item value from the device, received via MQTT every 66 seconds.
I use a rule via the UI to log the current value
configuration: {}
triggers:
- id: "1"
configuration:
itemName: BKW_curpower
type: core.ItemStateUpdateTrigger
conditions: []
actions:
- inputs: {}
id: "2"
configuration:
type: application/vnd.openhab.dsl.rule
script: |
val Riemann = BKW_curpower.riemannSumSince(now.with(LocalTime.MIDNIGHT),org.openhab.core.persistence.extensions.PersistenceExtensions.RiemannType.TRAPEZOIDAL)
var asKWh = (Riemann as Number) / 3600000
logInfo("BKW_PV_RULE_Power", BKW_curpower.getState.toString)
logInfo("BKW_PV_RULE_Summe", asKWh.toString)
SolarStromDaily.postUpdate(asKWh as Number)
type: script.ScriptAction
log: last log from Aug 03 and first few from Aug 04
2026-08-03 23:59:31.133 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-03 23:59:31.133 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 2.83268735
2026-08-04 00:00:37.122 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 00:00:37.123 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.00096224
2026-08-04 00:01:43.121 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 00:01:43.122 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.00267644
2026-08-04 00:02:49.126 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 00:02:49.126 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.00439066
2026-08-04 00:03:55.122 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 00:03:55.123 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.00610480
2026-08-04 00:05:01.124 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 00:05:01.125 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.00781899
2026-08-04 00:06:07.142 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 00:06:07.143 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.00953323
2026-08-04 00:07:13.125 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 00:07:13.126 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.01124735
Why is the calculated sum > 0 while the item values are all 0 during the night?
I suspect It might have something to do with the fact that in the database there are no values around that time? Looking at InfluxDB I see
2026-08-03 17:52:00 10
2026-08-03 17:58:00 0
2026-08-04 03:56:00 10
2026-08-04 03:58:00 12
2026-08-04 04:00:00 14
What am I missing/doing wrong?
Thanks
Mherwege
(Mark Herwege)
August 4, 2026, 7:28am
2
Is InfluxDB set as your default persistence service, not RRD4J by any chance?
Can you log now.with(LocalTime.MIDNIGHT) to avoid any timezone issue? The method expects a ZonedDateTime as an input. Try now.truncatedTo(ChronoUnit.DAYS) instead.
There is no need to convert Riemann to Number. Just convert the unit: Riemann.toUnit("kWh").
Did you try other Riemann types? What is the result then?
Try logging getAllStatesSince. This should give you the base data the calculation works with, except for the one value before midnight that is also considered.
JensA
(Jens A)
August 4, 2026, 8:52am
3
hi, thanks for the debugging tips
InfluxDB is the default.
now.with(LocalTime.MIDNIGHT) and now.truncatedTo(ChronoUnit.DAYS) give the same output like
2026-08-04T00:00+02:00[Europe/Berlin]
BKW_curpower.getAllStatesSince(now.with(LocalTime.MIDNIGHT)) output starts with below lines
2026-08-04 10:44:07.516 [INFO ] [org.openhab.core.model.script. ] - 2026-08-04T05:55:55.235+02:00[Europe/Berlin] -> 10 W
2026-08-04 10:44:07.517 [INFO ] [org.openhab.core.model.script. ] - 2026-08-04T05:57:01.236+02:00[Europe/Berlin] -> 12 W
.
.
.
2026-08-04 10:55:40.984 [INFO ] [org.openhab.core.model.script. ] - 2026-08-04T10:55:07.420+02:00[Europe/Berlin] -> 96 W
I tried the other Rieman types. LEFT looks not as wrong as the others but still not the expected output. With the expected output beeing the value I see in the vendors android app.
Right now, the app shows 0.290kWh whereas the Rieman Sum LEFT is 0.382kWh
Mherwege
(Mark Herwege)
August 4, 2026, 9:00am
4
Try BKW_curpower.persistedState(now.with(LocalTime.MIDNIGHT)). This will give you the last value persisted at or before midnight. This value is also considered in the calculation.
The problem is most clear around midnight. So it is probably worth to test with RiemannSumBetween and getAllStatesBetween, so it is easier to interpret and compare the results. They call the same methods internally.
JensA
(Jens A)
August 5, 2026, 4:25am
5
Try BKW_curpower.persistedState(now.with(LocalTime.MIDNIGHT)).
logInfo("BKW_PV_RULE_Midnight", BKW_curpower.persistedState(now.with(LocalTime.MIDNIGHT)).timestamp.toString)
That’s interesting: last log of yesterday and first today: both
2026-08-04 23:59:28.762 [INFO ] [.core.model.script.BKW_PV_RULE_Midnight] - 2023-01-03T09:41:33.534+01:00[Europe/Berlin]
2026-08-04 23:59:28.762 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-04 23:59:28.762 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 2.37931432
2026-08-05 00:00:32.284 [INFO ] [.core.model.script.BKW_PV_RULE_Midnight] - 2023-01-03T09:41:33.534+01:00[Europe/Berlin]
2026-08-05 00:00:32.284 [INFO ] [.core.model.script.BKW_PV_RULE_Power] - 0 W
2026-08-05 00:00:32.284 [INFO ] [.core.model.script.BKW_PV_RULE_Summe] - 0.00166555
Looking at the database, the closest entry I can find is 2023-01-03 09:41:43 GMT+1
Does this make any sense to you?
Mherwege
(Mark Herwege)
August 8, 2026, 8:08pm
6
Don’t use LocalTime. I suspect it does not get interpreted properly. LocalTime does not carry a date, only time.DSL may be taking the wrong class for now and misinterpret the date because of it.