Timer::isActive() and Header support for sendHttpXXXRequest() for 2.5.0

I’ve submitted a PR to add Timer::isActive() and it was merged into 3.0.

I’ve created a jar so that it can be used in 2.5.0
https://github.com/jimtng/binaries/blob/master/org.openhab.core.model.script-2.5.0.jar?raw=true

What’s in it? The same as what’s in the official 2.5.0 plus:

  • Timer::isActive()
  • Header support for HTTP.sendHttpXXXRequest actions

I am using this with my docker-compose.yaml as follows:

  volumes:
      - /home/openhab/tmp/org.openhab.core.model.script-2.5.0.jar:/openhab/runtime/system/org/openhab/core/bundles/org.openhab.core.model.script/2.5.0/org.openhab.core.model.script-2.5.0.jar

Timer::isActive() allows you to know whether a timer is still going to run or not. The implementation of Timer::isActive() is super simple:

return !terminated && !cancelled;

A timer will not run if it has been cancelled, or when it has already executed.

Code without isActive() (in Jython):

def turn_it_off():
  events.sendCommand("itemname", "OFF")
  timer = None

if i_want_to_cancel and timer != None:
  timer.cancel()
  timer = None

if timer == None:
  # the timer is not active, do something
else:
  # the timer is active, do something else

if timer is None:
  timer = ScriptExecution.createTimer(DateTime.now().plusMinutes(1), turn_it_off)
else
  timer.reschedule(DateTime.now().plusMinutes(1))

Now that we have Timer::isActive():

if i_want_to_cancel and timer != None :
  timer.cancel()

if timer == None or not timer.isActive():
  # the timer is not active, do something
else:
  # the timer is active, do something else

if timer is None:
  timer = ScriptExecution.createTimer(DateTime.now().plusMinutes(1), lambda: events.sendCommand("itemname", "OFF"))
else
  timer.reschedule(DateTime.now().plusMinutes(1))

This way we don’t keep creating new timer objects each time as well.

A rookie mistake I made early on was thinking that timer.hasTerminated() would return true if it had been cancelled. That is not the case.

Additionally, this jar also includes the header support for sendHttpXXXRequest() which is also going to be included in 3.0 as per https://www.openhab.org/docs/configuration/actions.html#http-actions

2 Likes