Timer latch design pattern for alarm example from Rich - something wrong here

I found an example like this from Rich Koshak and i want to use it for an alarm scenario.
The siren gets swichted on for 10 seconds and should only start when last start is1 minute ago:

This what openhab.log shows me and of course is not working:

2018-03-04 13:11:29.800 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘G11.rules’ is either empty or cannot be parsed correctly!
2018-03-04 13:11:30.331 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'G11.rules’
2018-03-04 13:12:58.156 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘PIR Neo_PIR_1 for Switch with Siren’: The name ‘timer’ cannot be resolved to an item or type; line 12, column 7, length 5

And this is my G11.rules:

var Timer shutoffTimer= null
var DateTime lastRun = now.minusMinutes(1) // on a reboot switch on sirene on the first event

rule “PIR Neo_PIR_1 for Switch with Siren”

when

	Item NEO_PIR_1_Bewegung changed from OFF to ON
	
then
	if(lastRun.isBefore(now.minusMinutes(1))) {
		if(timer==null) {
		sendCommand(zwave_device_296beed3_node4_switch_binary, ON)
			lastRun = now
		logInfo("RuleInfo", "Siren START")
		timer = createTimer(now.plusSeconds(10)) [|
			sendCommand(zwave_device_296beed3_node4_switch_binary, OFF)
			logInfo("RuleInfo", "Siren STOP")
		timer = null
			]
		}
	}

end

“timer” isn’t defined. I think you want use “shutoffTimer” instead.

Oh Thanks,
but what do you mean which command in which line should I replace with shutoffTimer?

Thanks

Now it works, Great thanks

var Timer shutoffTimer= null
var DateTime lastRun = now.minusMinutes(1) // on a reboot switch on sirene on the first event

rule “PIR Neo_PIR_1 for Switch with Siren”

when

	Item NEO_PIR_1_Bewegung changed from OFF to ON
	
then
	if(lastRun.isBefore(now.minusMinutes(1))) {
		if(shutoffTimer==null) {
		sendCommand(zwave_device_296beed3_node4_switch_binary, ON)
			lastRun = now
		logInfo("RuleInfo", "Siren START")
		timer = createTimer(now.plusSeconds(10)) [|
			sendCommand(zwave_device_296beed3_node4_switch_binary, OFF)
			logInfo("RuleInfo", "Siren STOP")
		timer = null
			]
		}
	}

end

There are two more lines you use “timer”.

OK, but do I need to change this?
Because it is working now, switch on for 10 seconds and after 1 Minute it switchs again for 10 seconds!

The only thing is my openhab.log looks ugly, this doesn´t tell me any thing, you?

2018-03-04 15:37:39.427 [INFO ] [ipse.smarthome.model.script.RuleInfo] - Siren START
2018-03-04 15:37:39.454 [ERROR] [ntime.internal.engine.RuleEngineImpl] - Rule ‘PIR Neo_PIR_1 for Switch with Siren’: An error occurred during the script execution: Couldn’t invoke ‘assignValueTo’ for feature JvmVoid: (eProxyURI: G11.rules#|::0.2.0.2.0.0.1.0.0.1.0.3::0::/1)
2018-03-04 15:37:49.442 [INFO ] [ipse.smarthome.model.script.RuleInfo] - Siren STOP
2018-03-04 15:37:49.448 [ERROR] [org.quartz.core.JobRunShell ] - Job DEFAULT.2018-03-04T15:37:49.433+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
sendCommand(,)
logInfo(,)
.timer =
} ] threw an unhandled Exception:
java.lang.IllegalArgumentException: Couldn’t invoke ‘assignValueTo’ for feature JvmVoid: (eProxyURI: G11.rules#|::0.2.0.2.0.0.1.0.0.1.0.3.7.6.1.1.0.2::0::/1)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.assignValueTo(XbaseInterpreter.java:1224) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1212) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:215) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) [164:org.eclipse.xtext.xbase:2.12.0.v20170519-0752]
at com.sun.proxy.$Proxy153.apply(Unknown Source) [?:?]
at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:49) [145:org.eclipse.smarthome.model.script:0.10.0.b1]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
2018-03-04 15:37:49.463 [ERROR] [org.quartz.core.ErrorLogger ] - Job (DEFAULT.2018-03-04T15:37:49.433+01:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
sendCommand(,)
logInfo(,)
.timer =
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [115:org.eclipse.smarthome.core.scheduler:0.10.0.b1]
Caused by: java.lang.IllegalArgumentException: Couldn’t invoke ‘assignValueTo’ for feature JvmVoid: (eProxyURI: G11.rules#|::0.2.0.2.0.0.1.0.0.1.0.3.7.6.1.1.0.2::0::/1)
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.assignValueTo(XbaseInterpreter.java:1224) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:1212) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:215) ~[?:?]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:446) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:227) ~[?:?]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:215) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluate(XbaseInterpreter.java:189) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.ClosureInvocationHandler.doInvoke(ClosureInvocationHandler.java:46) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.AbstractClosureInvocationHandler.invoke(AbstractClosureInvocationHandler.java:29) ~[?:?]
at com.sun.proxy.$Proxy153.apply(Unknown Source) ~[?:?]
at org.eclipse.smarthome.model.script.internal.actions.TimerExecutionJob.execute(TimerExecutionJob.java:49) ~[?:?]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[?:?]
… 1 more

Yes. It is only partially working.

The errors are cause by using the non-existant timer instead of shutoffTimer.

Do you use any persistence or is lastrun only stored in the variable and will be erased every time you restart openhab? So you do this on startup with the global declaration var DateTime lastRun = …