[SOLVED] Jython, sumSince, absolute timestamp

how can i use an absolute time stamp here?
i try:

MeterUseSum = PersistenceExtensions.sumSince(ir.getItem("ZWaveNode2_PowerNode_ElectricMeterkwH_Day"), "2019-03-01")  # line 67

openhab.log:

2020-03-05 17:29:20.026 [ERROR] [jsr223.jython.Gesamtverbrauch Entfeuchter         ] - Traceback (most recent call last):
  File "/etc/openhab2/automation/lib/python/core/log.py", line 51, in wrapper
    return fn(*args, **kwargs)
  File "<script>", line 67, in Stromverbrauch_Gesamt
TypeError: sumSince(): 2nd arg can't be coerced to org.joda.time.base.AbstractInstant

I’ve tried the following, unfortunately without success:

....
from dateutil.parser import parse
....

MeterUseSum = PersistenceExtensions.sumSince(ir.getItem("ZWaveNode2_PowerNode_ElectricMeterkwH_Day"), parse("2019-03-01"))  # line 19

openhab.log:

2020-03-05 19:11:46.779 [ERROR] [ion.module.script.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/python/personal/powerusage.py': ImportError: No module named dateutil in <script> at line number 19

no success was achieved also with:

MeterUseSum = PersistenceExtensions.sumSince(ir.getItem("ZWaveNode2_PowerNode_ElectricMeterkwH_Day"), datetime.date(2019, 3, 01))

Persistence currently uses Joda, but that has been changed in OH 3.0. For this, you need an AbstractInstant. You’ve shown a day and month, but what time of day? Assuming 0:00…

from org.joda.time import DateTime

MeterUseSum = PersistenceExtensions.sumSince(ir.getItem("ZWaveNode2_PowerNode_ElectricMeterkwH_Day"), DateTime.now().withYear(2019).withMonthOfYear(3).withDayOfMonth(1).withTime(0, 0, 0, 0)) # line 67

I like doing it this way, because it’s more readable, but you could also do…

MeterUseSum = PersistenceExtensions.sumSince(ir.getItem("ZWaveNode2_PowerNode_ElectricMeterkwH_Day"), DateTime(2019, 3, 1, 0, 0, 0, 0)) # line 67
1 Like

Once again, thank you very much, both versions work fine!

1 Like