Timers in Python (jython) with OH3

Ok, I’ve scoured the community and documentation for help on this but I admit defeat :slight_smile:

Can someone point me to the “right” way to use timers in Python under OH3. I am currently using datetime and threading.Timer (which works fine) but I am also seeing that I should be using the native OH timers like ScriptExecution.createTimer. I’m getting the error:

TypeError: createTimer(): 1st arg can't be coerced to java.time.ZonedDateTime

Is there a simple example for say, setting a timer for 10 seconds from now and calling a function? Also want to be able to start/stop the timer. (obviously)

Thanks! I hope I’m not the only one confused by this :slight_smile:

Have you seen this example before?


(Despite the recommendations I stick to using native python stuff as much as possible - far more documentation out there!)

There’s also a bunch of example scripts in the Github repository for those docs:

1 Like

Thanks - Yes I have timers working fine with threading.Timer but thought that there were OH native timers that were preferred … I’ll assume no, and just continue with what I have.


Preferred is up to the implementer. Certainly, the advice I received some time back was to use the OH native timers (presumably you saw the examples in the link I posted?).

I still use threading.timer though, as explained above.

1 Like