Python rule: how specify historic DateTime in PersistenceExtensions.sumSince?

I have a number item that is updated with the number 1 whenever another related item is updated. I want to write a python rule that checks how many times the item has been updated within the previous ten seconds by summing those 1s. If the sum of the 1s is over a threshold value then the rule will set the state of a datetime item to now. It sounds odd but due to a quirk of the way the hardware involved (a radiator thermostat) works this will allow me to record the date and time the batteries were last changed so I can be more organised with battery changes. I can then change the batteries before the hardware starts beeping and annoying the other people in the house who can’t seem to change batteries but CAN find the time to complain about them. :slight_smile:

I have the following line in a rule:

numUpdates = float(str(PersistenceExtensions.sumSince(ir.getItem(event.itemName), DateTime.now().minusSeconds(10)).state))

But it gives the following error when it’s run:

2019-10-10 16:17:49.426 [ERROR] [perture change count frequently rule] - 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 41, in thermostat_aperture_count_updated
TypeError: sumSince(): 2nd arg can't be coerced to org.joda.time.base.AbstractInstant

I’ve tried DateTime.now() - - DateTime.timedelta(seconds = 15) but that didn’t work either. What am I doing wrong?

Did you import DateTime?

from org.joda.time import DateTime

I also don’t think you will want the .state on there… will probably throw an error. sumSince returns a number, not an Item.

Yes, DateTime is imported. I’ve removed the .state so the full rule is as follows:

from core.triggers import when
from core.rules import rule
# from core.actions import ScriptExecution
from org.joda.time import DateTime
from core.actions import PersistenceExtensions
from core.date import seconds_between
from core.actions import Pushover
from core.log import logging, LOG_PREFIX

@rule("Update datetime item when thermostat valve aperture change count frequently rule", description="This rule updates a datetime item when a valve aperture changes frequently after a battery change", tags=["Example"])
@when("Member of gRoomThermostatUpdateCount received update")
def thermostat_aperture_count_updated(event):
    triggeringRoomGroup                 = None
    heatingThermostatBatteryChangedItem = None
    numUpdates                          = 0
    prevTime                            = None

    log = logging.getLogger("org.eclipse.smarthome.model.script.heating")

    triggeringRoomGroup                 = [group for group in ir.getItem("gRoom").members if group.type == "Group" and ir.getItem(event.itemName) in group.members][0]
    heatingThermostatBatteryChangedItem = [group for group in ir.getItem(str(triggeringRoomGroup.name)).members if group in ir.getItem("gRoomThermostatBatteryChanged").members][0]

    if heatingThermostatBatteryChangedItem != None:
        # prevTime    = DateTime.now().minusSeconds(10)
        # prevTime    = DateTime.now() - - DateTime.timedelta(seconds = 15)
        numUpdates  = float(str(PersistenceExtensions.sumSince(ir.getItem(event.itemName), DateTime.now().minusSeconds(10))))
        log.info("Heating: valve aperture updated ({}) times in the last 10 seconds in room ({}).".format( numUpdates, triggeringRoomGroup.name ))
        # events.sendCommand(ir.getItem(thermostatUpdateCountItem.name), 1)

Even if I remove the .minusSeconds(10) the rule still gives the same error:

2019-10-10 20:40:39.316 [ERROR] [perture change count frequently rule] - 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 42, in thermostat_aperture_count_updated
TypeError: sumSince(): 2nd arg can't be coerced to org.joda.time.base.AbstractInstant

This works for me using a NumberItem. How is gRoomThermostatUpdateCount and its members defined?

The group is defined like this:

Group gRoomThermostatUpdateCount

The item is defined like this:

Number HeatingBlueroomThermostatUpdateCount "Blueroom Heating Thermostat Update Count" (gBlueRoom, gPersist_History, gRoomThermostatUpdateCount)

Make a test script and put just this in it, save, and then check your log…

from core.log import logging, LOG_PREFIX, log_traceback
log = logging.getLogger(LOG_PREFIX + ".TEST")

from org.joda.time import DateTime
from core.actions import PersistenceExtensions

log.warn("[{}]".format(float(str(PersistenceExtensions.sumSince(ir.getItem("HeatingBlueroomThermostatUpdateCount"), DateTime.now().minusSeconds(10))))))

I created a file called sumsincetest.py and put the code in it:

from core.log import logging, LOG_PREFIX, log_traceback
log = logging.getLogger(LOG_PREFIX + ".TEST")
from org.joda.time import DateTime
from core.actions import PersistenceExtensions

log.warn("[{}]".format(float(str(PersistenceExtensions.sumSince(ir.getItem("HeatingBlueroomThermostatUpdateCount"), DateTime.now().minusSeconds(10))))))

The same error was output to the log:

2019-10-11 09:58:45.440 [INFO ] [rt.internal.loader.ScriptFileWatcher] - Loading script 'personal/sumsincetest.py'
2019-10-11 09:58:45.458 [ERROR] [ipt.internal.ScriptEngineManagerImpl] - Error during evaluation of script 'file:/etc/openhab2/automation/jsr223/personal/sumsincetest.py': TypeError: sumSince(): 2nd arg can't be coerced to org.joda.time.
base.AbstractInstant in <script> at line number 7

This is very strange if it works for you but not for me. I’m running 2.4, in case that makes any difference.

That must be the difference, since I’m using S1705. I’ll take a look later… there might be a way to get this working for you… but an upgrade to 2.5M3 might do it.

What if you convert it to an Instant?

from core.log import logging, LOG_PREFIX, log_traceback
log = logging.getLogger(LOG_PREFIX + ".TEST")
from org.joda.time import DateTime
from core.actions import PersistenceExtensions

log.warn("[{}]".format(float(str(PersistenceExtensions.sumSince(ir.getItem("HeatingBlueroomThermostatUpdateCount"), DateTime.now().minusSeconds(10).toInstant())))))