I am a beginner for OpenHab and got some help in the forum setting up that rule below. (Thanks to Rich). Now I miss a little peace in that rule and maybe someone can help me here?
This rule is counting the activation of a motion detector and when the activation was 3 times in a minute, it activates another item. That works!
When this activation was done the timer count (is it the timer size?) should be reset again and wait until the 3rd activation in a minute.
Instead it activates now each following motion detection, so it doesn’t reset the number of PIR activations to 0.
What can I do?
import java.util.List
var List timers = newArrayList
var Timer shutoffTimer = null
var lastRun = now
rule “Sonbas Motion changed from OFF to ON 3 times in a minute”
when
Item Sonbas10b changed from OFF to ON or
Item Sonoff3 changed from OFF to ON
then
if(shutoffTimer !== null || Alarm.state == OFF || lastRun.isAfter(now.minusMinutes(2))) return;
if(timers.size < 2) {
val t = createTimer(now.plusMinutes(1), [ | timers.remove(t) ])
timers.add(t)
}
if(timers.size == 2) {
sendMail("xxx@gmail.com", “Sonbas10b”, “Sonbas10b motion”)
TH03.sendCommand(ON)
lastRun = now
logInfo(“RuleInfo”, “Siren START”)
shutoffTimer = createTimer(now.plusSeconds(30)) [|
TH03.sendCommand(OFF)
logInfo(“RuleInfo”, “Siren STOP”)
shutoffTimer = null
]
}
end
OK, I have inserted the command but now the rule isn´t working, means when i activate the switch it doesn´t react doesn´t matter how often I activate.
No error in openhab.log,
Does it maybe reset the timer now after each activation and does not reach the three activations?
rule “Sonbas Motion changed from OFF to ON 3 times in a minute”
when
Item Sonbas10b changed from OFF to ON or
Item Sonoff3 changed from OFF to ON
then
if(shutoffTimer !== null || Alarm.state == OFF || lastRun.isAfter(now.minusMinutes(2))) return;
logInfo("TIMERS SIZE", timers.size.toString)
if(timers.size < 2) {
val t = createTimer(now.plusMinutes(1), [ | timers.remove(t) ])
timers.add(t)
logInfo("TIMERS SIZE", timers.size.toString)
}
if(timers.size == 2) {
sendMail("xxx@gmail.com", “Sonbas10b”, “Sonbas10b motion”)
TH03.sendCommand(ON)
lastRun = now
logInfo(“RuleInfo”, “Siren START”)
timers.clear()
shutoffTimer = createTimer(now.plusSeconds(30)) [|
TH03.sendCommand(OFF)
logInfo(“RuleInfo”, “Siren STOP”)
shutoffTimer = null
]
}
end
2018-07-05 15:47:34.150 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command ON
2018-07-05 15:47:34.165 [vent.ItemStateChangedEvent] - Sonbas10b changed from NULL to ON
2018-07-05 15:47:36.908 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command OFF
2018-07-05 15:47:36.932 [vent.ItemStateChangedEvent] - Sonbas10b changed from ON to OFF
2018-07-05 15:47:40.050 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command ON
2018-07-05 15:47:40.070 [vent.ItemStateChangedEvent] - Sonbas10b changed from OFF to ON
2018-07-05 15:47:42.459 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command OFF
2018-07-05 15:47:42.471 [vent.ItemStateChangedEvent] - Sonbas10b changed from ON to OFF
2018-07-05 15:47:46.534 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command ON
2018-07-05 15:47:46.548 [vent.ItemStateChangedEvent] - Sonbas10b changed from OFF to ON
2018-07-05 15:47:49.826 [ome.event.ItemCommandEvent] - Item 'TH03' received command ON
2018-07-05 15:47:49.858 [vent.ItemStateChangedEvent] - TH03 changed from OFF to ON
2018-07-05 15:47:50.408 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command OFF
2018-07-05 15:47:50.424 [vent.ItemStateChangedEvent] - Sonbas10b changed from ON to OFF
2018-07-05 15:48:19.851 [ome.event.ItemCommandEvent] - Item 'TH03' received command OFF
2018-07-05 15:48:19.876 [vent.ItemStateChangedEvent] - TH03 changed from ON to OFF
2018-07-05 15:53:45.383 [vent.ItemStateChangedEvent] - TH03Temp changed from 25.9 to 26.0
2018-07-05 15:53:45.397 [vent.ItemStateChangedEvent] - TH03Hum changed from 59.3 to 59.2
2018-07-05 15:53:45.422 [ome.event.ItemCommandEvent] - Item 'TH03' received command OFF
2018-07-05 15:53:54.998 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command OFF
I am confused because I triggered on 15:53 the sonbas1b 4 times and this isn´t appearing
Thos is openhab.log if intersting
2018-07-05 15:48:46.673 [ERROR] [org.quartz.core.ErrorLogger ] - Job (DEFAULT.2018-07-05T15:48:46.582+02:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
<XFeatureCallImplCustom>.remove(<XFeatureCallImplCustom>)
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [107:org.eclipse.smarthome.core.scheduler:0.10.0.oh230]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [107:org.eclipse.smarthome.core.scheduler:0.10.0.oh230]
Caused by: java.lang.reflect.UndeclaredThrowableException
at com.sun.proxy.$Proxy144.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
Caused by: org.eclipse.smarthome.model.script.engine.ScriptExecutionException: The name 't' cannot be resolved to an item or type; line 15, column 65, length 1
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:133) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:901) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:864) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:223) ~[?:?]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:219) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.internalEvaluate(XbaseInterpreter.java:203) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.evaluateArgumentExpressions(XbaseInterpreter.java:1115) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1045) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:991) ~[?:?]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:143) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:763) ~[?:?]
at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:219) ~[?:?]
at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.doEvaluate(ScriptInterpreter.java:219) ~[?:?]
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:219) ~[?:?]
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.$Proxy144.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
Now I tried it agian with a different behavior after the second activatiob it reacts
2018-07-05 15:58:45.384 [vent.ItemStateChangedEvent] - TH03Hum changed from 59.2 to 59.0
2018-07-05 16:02:06.297 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command ON
2018-07-05 16:02:06.319 [vent.ItemStateChangedEvent] - Sonbas10b changed from OFF to ON
2018-07-05 16:02:09.373 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command OFF
2018-07-05 16:02:09.394 [vent.ItemStateChangedEvent] - Sonbas10b changed from ON to OFF
2018-07-05 16:02:11.729 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command ON
2018-07-05 16:02:11.745 [vent.ItemStateChangedEvent] - Sonbas10b changed from OFF to ON
2018-07-05 16:02:13.875 [ome.event.ItemCommandEvent] - Item 'Sonbas10b' received command OFF
2018-07-05 16:02:13.895 [vent.ItemStateChangedEvent] - Sonbas10b changed from ON to OFF
2018-07-05 16:02:14.354 [ome.event.ItemCommandEvent] - Item 'TH03' received command ON
2018-07-05 16:02:14.385 [vent.ItemStateChangedEvent] - TH03 changed from OFF to ON
2018-07-05 16:02:44.382 [ome.event.ItemCommandEvent] - Item 'TH03' received command OFF
2018-07-05 16:02:44.401 [vent.ItemStateChangedEvent] - TH03 changed from ON to OFF
So do have some time again and changed code as you recommended.
and this what it shows now when triggering the sonbas1b item two times, and it switches
the item TH03 after the second trigger, not after the third trigger, as it should and does
when I restart PI.
one other thing maybe not important:
there is one stupid comment in openhab.log, what´s wrong with type List - raw type?
2018-07-06 14:45:14.259 [INFO ] [el.core.internal.ModelRepositoryImpl] - Validation issues found in configuration model 'MQTT.rules', using it anyway:
List is a raw type. References to generic type List<E> should be parameterized
Generally it is working, but I would like to have the item being activated after three signals, this is what happens when the pi is restarted.
With the current code it activates the item after the second triggering signal.
How can I reach that always three triggering signals are used?
I can find some comments maybe helpful but again I am lost how to apply to my code
Comment found:
However, because generics were added to Java 5, it is still possible to write code without them for backwards compatibility. So you can write:
ArrayList list = new ArrayList();
But you lose all the type checking benefits. Es in method signatures become simply Objects.
So it looks like it is that line of code to be adjusted:
var List timers = newArrayList
do I need to change to
var List timers = newArrayList()
why do they have a blank after new and “;” at the end is that important?