Well, I am a bit lazy and using the createTimer action I do not have to store the timer in a variable and start it explicitly
Today I did some testing with the Python timer, using the example linked in the documentation as a template.
I think, that there a two bugs in the example:
First:
if chargerTimer1 is None or str(chargerTimerAttached.getState()) == "TERMINATED":
Beside the typo in the timer name “chargerTimerAttached”, I get an error, that the timer object has no method “getState()”
Second:
chargerTimer1.stop()
A timer can be created by calling:
createPythonTimer(15, 'TestTimerName', lambda: theInnerFunction())
There I also get an error, that this method does not exists, instead “cancel()” can be used.
What seems to work for me:
testTimer = {}
def createPythonTimer(elapseTime, item, callback):
myTimer = testTimer.get(item) # returns none, when key does not exists
if myTimer is None or not myTimer.isAlive():
myTimer = Timer(elapseTime, callback)
testTimer[item] = myTimer
myTimer.start()
log.info("test_rules.py - Created python timer, item: " + item + ", time: " + str(elapseTime))
def stopPythonTimer(item):
myTimer = testTimer.get(item) # returns none, when key does not exists
if myTimer is not None and myTimer.isAlive():
myTimer.cancel()
testTimer[item] = None
log.info("test_rules.py - Stopped python timer, item: " + item)
In this small examples I store the timers in a dictionary, so that I do not have to create a variable for each timer.
According to what I found in the internet, “myTimer.finished” should be better than “myTimer.isAlive()”, as there is a small time between finishing the timer and exiting the background thread, internally used for the timer and therefor setting “isAlive()” to false. But checking “finished” does not work for me, I do not know why.