OH2.1 rule 'system started' - timer java errors

Can’t quite work out what’s wrong here. I set a rule to turn on a proxy switch so when the system starts up, other rules check this, make sure its off otherwise don’t process the rule. Its quite simply as follows

//
// SYSTEM STARTED
//
var Timer startTime = null

rule "System startup"
when
	System started
then
	if (startTime == null)
	{
		logInfo("openhab", "SYSTEM STARTED!")
		sendCommand(swSystemBootUp, ON)
		startTime = createTimer(now.plusMinutes(1), [|
        	    logInfo("openhab", "Timer expired, setting bootup to OFF")
	            sendCommand(swSystemBootUp, OFF)
        	    startTime = null
	        ])
	}
end

But generates these errors in openhab.log:

2017-07-12 13:40:26.152 [ERROR] [org.quartz.core.JobRunShell ] - Job DEFAULT.2017-07-12T13:40:26.138+12:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
logInfo(,)
sendCommand(,)
.startTime =
} ] threw an unhandled Exception:
java.lang.RuntimeException: The name ‘’ cannot be resolved to an item or type.
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:112)[129:org.eclipse.smarthome.model.script:0.9.0.201706270841]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1115)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1045)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:991)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:114)[129:org.eclipse.smarthome.model.script:0.9.0.201706270841]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at com.sun.proxy.$Proxy146.apply(Unknown Source)[:]
at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:44)[129:org.eclipse.smarthome.model.script:0.9.0.201706270841]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[104:org.eclipse.smarthome.core.scheduler:0.9.0.201706270841]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[104:org.eclipse.smarthome.core.scheduler:0.9.0.201706270841]
2017-07-12 13:40:26.185 [ERROR] [org.quartz.core.ErrorLogger ] - Job (DEFAULT.2017-07-12T13:40:26.138+12:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
logInfo(,)
sendCommand(,)
.startTime =
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: java.lang.RuntimeException: The name ‘’ cannot be resolved to an item or type.]
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)[104:org.eclipse.smarthome.core.scheduler:0.9.0.201706270841]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)[104:org.eclipse.smarthome.core.scheduler:0.9.0.201706270841]
Caused by: java.lang.RuntimeException: The name ‘’ cannot be resolved to an item or type.
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:112)[129:org.eclipse.smarthome.model.script:0.9.0.201706270841]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1115)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1045)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:991)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:114)[129:org.eclipse.smarthome.model.script:0.9.0.201706270841]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29)[145:org.eclipse.xtext.xbase:2.9.2.v20160428-1452]
at com.sun.proxy.$Proxy146.apply(Unknown Source)[:]
at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:44)[129:org.eclipse.smarthome.model.script:0.9.0.201706270841]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)[104:org.eclipse.smarthome.core.scheduler:0.9.0.201706270841]
… 1 more

Any ideas?

I don’t think that you need a comma after startTime = createTimer(now.plusMinutes(1),
also, note the parenthesis

try:

startTime = createTimer(now.plusMinutes(1))
[|
        	    logInfo("openhab", "Timer expired, setting bootup to OFF")
	            swSystemBootUp.sendCommand(OFF)
        	    startTime = null
]

Edit: You can avoid timers if you use the expire binding :wink:

Thanks, that didn’t fix it unfortunately.

With regards to the expire binding, I didn’t think of using it here. So if I declared the switch item and set to expire back to OFF after 1 minute, then set a rule on system startup to simply turn it to ON, that would work.

1 Like

hmm…

maybe try to remove the if statement (it doesn’t seem to do much anyway…maybe I am wrong)

var Timer startTime = null

rule "System startup"
when
	System started
then
	logInfo("openhab", "SYSTEM STARTED!")
	swSystemBootUp.sendCommand(ON)
	startTime = createTimer(now.plusMinutes(1))
	[|
       	    logInfo("openhab", "Timer expired, setting bootup to OFF")
            swSystemBootUp.sendCommand(OFF)
       	    startTime = null
	]
end

Issue is possibly due to multiple timers firing - if I save home.rules for example, I get “SYSTEM STARTED!” logged, then refreshing model ‘home.rules’, then SYSTEM STARTED! logged again. The errors are occurring (based on date/time logs) AFTER the timer has expired, so something wrong with code in the brackets after the 1 minute is up.
Also if I run '‘top’, it possibly isn’t helped by the fact my CPU hits 99% for over a minute when the rules file is saved. Assume its recompiling the rules and everything else struggles while that runs.
I’ve gone the easy path anyways and just installed the expire binding, WAY easier, WAY better!
Thanks!

:vulcan_salute:

yup… See also the discussion here: https://github.com/openhab/openhab-docs/pull/416

There are two ways to call createTimer and @chimera is using my preferred approach.

The action createTimer takes two arguments, a Joda DateTime object that tells it when the execute the lambda in the second argument and a lambda which contains the code to be executed.

Given this, the standard and consistant way to define a Timer would be:

createTimer(now.plusMinutes(1), [|
    // some code
])

However, the developers of Xtext apparently found this to be awkward so they created an exception in the language where if the last argument of a method takes a lambda, you can define the lambda outside of the parens. Thus, the following is equivalent to above:

createTimer(now.plusMinutes(1)) [|
    // some code
]

While I agree that this does look better, I dislike it because it hides the fact that the lambda part is actually an argument to createTimer. Put another way, it makes the code look cleaner but it also makes the code less self-documenting and clear. This is why I never use the second approach, even though it looks cleaner.

4 Likes