Let a timer reschedule itself?

Is it possible to use a timer to reschedule itself? How?
Now, it gives an exception on “timer.reschedule”.
I would prefer not to use a “cron” here

For example:

var timer = null

rule "SomeRule"
when
    ....
then
    ....
    timer = createTimer(now.plusSeconds(10), [ |
                logInfo("test.rules", "Hello World!")
                if (...) {
                     timer.reschedule(now.plusSeconds(10))
                }
                else {
                    timer = null
                }
            ])
   ....
end

Yes. What is the secret exception?

java.lang.reflect.UndeclaredThrowableException: null
It says it does not know reschedule?
I was following this doc

2021-05-10 15:57:36.162 [INFO ] [home.model.script.plugs_zigbee.rules] - Hello World!
2021-05-10 15:57:36.162 [ERROR] [org.quartz.core.JobRunShell         ] - Job DEFAULT.Timer 4 2021-05-10T15:57:36.161+02:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  logInfo(<XStringLiteralImpl>,<XStringLiteralImpl>)
  <XFeatureCallImplCustom>.reschedule(<XMemberFeatureCallImplCustom>)
} ] threw an unhandled Exception: 
java.lang.reflect.UndeclaredThrowableException: null
	at com.sun.proxy.$Proxy682.apply(Unknown Source) ~[?:?]
	at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:48) ~[?:?]
	at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [bundleFile:?]
	at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [bundleFile:?]
Caused by: org.eclipse.smarthome.model.script.engine.ScriptExecutionException: 'reschedule' is not a member of 'java.lang.Object'; line 20, column 17, length 37
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:133) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:861) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:231) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:458) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:239) ~[?:?]
	at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:226) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:215) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:201) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) ~[?:?]
	at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) ~[?:?]
	... 4 more
2021-05-10 15:57:36.165 [ERROR] [org.quartz.core.ErrorLogger         ] - Job (DEFAULT.Timer 4 2021-05-10T15:57:36.161+02:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  logInfo(<XStringLiteralImpl>,<XStringLiteralImpl>)
  <XFeatureCallImplCustom>.reschedule(<XMemberFeatureCallImplCustom>)
} ] threw an exception.

You declared your variable as a generic object. Declare as Timer handle to begin with.

var Timer mytimer = null

2 Likes

Thanks, that fixes it. My background is Python, so I am not so much in declaring types :wink: