Timer in Jython JSR233 script

Hi,

I’m trying to develop a rule with an jsr233 script. (It’s a test)

When a contact is opened, I would like to start a timer.
After the timer is finished, I would like to log a message.

And here’s my script:

from org.slf4j import LoggerFactory
from threading import Timer

scriptExtension.importPreset("RuleSimple")
scriptExtension.importPreset("RuleSupport")

class ContactRule(SimpleRule):
    def __init__(self, contact):
        self.timer = None
        self.itemName = contact
        self.logger = LoggerFactory.getLogger("org.eclipse.smarthome.automation.ContactRule")
        self.triggers = [Trigger("MyTrigger", "core.ItemStateUpdateTrigger", Configuration({ "itemName": contact}))]


    def timerEval(self):
        self.logger.info(self.itemName + ' ist immer noch offen.')

    def execute(self, module, inputs):
        triggerItemState = inputs['event'].getItemState()

        if triggerItemState == OpenClosedType.OPEN:
            self.logger.info(self.itemName + ': Timer gestartet.')
            self.timer = Timer(DateTime.now().plusMinutes(10), self.timerEval).start()
        else:
            if self.timer:
                self.logger.info(self.itemName + ': Timer gestoppt.')
                self.timer.cancel()
                self.timer = None

automationManager.addRule(ContactRule("th_co_wz_couch_State"))

The command “self.logger.info(self.itemName + ‘: Timer gestartet.’)” is executed, but after that an error occured. In openhab.log I found the following messages:

2018-05-02 23:25:06.982 [INFO ] [pse.smarthome.automation.ContactRule] - th_co_wz_couch_State: Timer gestartet.
2018-05-02 23:25:06.983 [ERROR] [.automation.core.internal.RuleEngine] - Failed to execute rule ‘9c944bcb-d17c-4104-83e6-d62e04a31cb1’: Failed to execute action ‘1’: null

Where’s my error?

Kind regards
Thomas

I define my timers in jython like this

myTimer = Timer(600,myFunction)
myTimer.start()

This runs fine for me on OH2.2. I do however see similar errors when I start OH2.3 snapshots. Most of them are related to the use of DateTIme (see here) I see the error mentioned there and also the same as yours:

Failed to execute rule 'ProcessTempHumRule-b9528181-c479-4780-8b0e-3ba03c115967': Failed to execute action '1': null

@nasi_be Which version of OH are you running?

EDIT: my above mentioned error is solved in the linked topic.

Hi,

I’m running OH 2.3 snapshot and your hint was correct.
It’s not the timer, but the DateTime which makes the problem.

When I changed the DateTime to a fixed number of seconds, the rule runs as it should.

Thanks for the hint.

Do you use persistence extensions in your jython rules? If so, are you seeing the errors reported by me in the other thread (link in my previous post)?

I don’t use persistence extensions in my rules, but the hint in your linked post helped to solve my problem too.