How to configure motion sensors (PIR's)

Hi,
your rule turns LED on with every change of the motion sensor. This means, the LED is switched on by detection of movement AND is switched on by detection of no movement.
You need your rule to differentiate between turning on and off.

rule "Motion detected, turn LED on"
when
  Item MotionSensor changed from OFF to ON
then
  RaspiLED.sendCommand(ON)
end

rule "no Motion detected, turn LED off"
when
  Item MotionSensor changed from ON to OFF
then
  RaspiLED.sendCommand(OFF)
end

Sometimes you get in trouble with using sendCommand action, so it is advised to use the method of your item. (see sendCommand() Documentation)

Andreas

1 Like

Can you try modifying
Switch MotionSensor{ gpio ="pin:18" }
by
Contact MotionSensor{ gpio ="pin:18" }

hi andreas,
my LED doesnt turn on when motion is detected, it simply gets turned on when i switch my sensor from OFF to On

and what should be the rule federic?

Iā€™m not sure how your PIR works, does it send an ON and OFF signal? First try the original rule you had, but that should only switch the LED ON and never OFF. Then you can try to change the rule to:

when 
  Item MotionSensor changed to ON
then
  sendCommand(RaspiLED, ON)
end

if that doesnā€™t work you need to inspect the state of Motion sensor to see if it is producing ON/OFF states or some numerical value, you can do that in the console or by printing the state in the log.

if that worked, then you need to add code to your rule to turn the light OFF either based on a timer or based on the sensor switching back to its OFF state.

no
changed to ON didnt work for me

How can i inspect state of my pir sensor?

does the original rule work?

when 
  Item MotionSensor changed
then
  sendCommand(RaspiLED, ON)
end

No. It doesnt turn LED ON.

Are you sure you have wired the PIR correctly and that it is working?
If you want to inspect the state of an item you can do so by:
telnet ip.address.of.openhb 5555
then
openhab status MotionSensor
typing openhab will provide a list of available commands.

However, I think your PIR may not be wired properly or to the correct GPIO

when i do gpio -g read 18, it shows 1 when motion is detected otherwise 0. can i equate MotionSensor value to 1 in my rule?

By the way, I havenā€™t used the GPIO binding myself but did you follow the installation process and made sure your addons folder has the binding jar file? Can you verify with another item that the binding works (is RaspiLED also a gpio item?)?

yes the gpio binding is working fine since i tested with the RaspiLED.

1 Like

After reading the binding documentation the states should be OPEN/CLOSED.
try to define your item like this:
Contact MotionSensor { gpio="pin:18 activelow:yes" }
and try to inspect its state through the console

no. didnt work.

can i define a rule that when gpio pin is 1ā€¦raspiLED should turn ON?

I donā€™t think so, the binding should translate that to OPEN/CLOSED

what is the output of the console when you do:
openhab status MotionSensor

It shows:

HTTP/1.1 400 Bad Request
Content-Length: 0
Connection closed by foreign host

I thought i would look at this to see if it would solve my issue before posting a new topic, but im a bit stumped on the syntax, if appears that there is either a } missing or a ) in the wrong place. Im not sure either what || represents. Iā€™m wondering if the rouge ) should be after timer == null

The rule above is missing the square brackets around the lambda passed to the Timer.

rule "Handle PIR Events"
when
    Item Pir_Sensor received update
then
    if(timer == null || timer.hasTerminated) {
        Pir_Proxy.sendCommand(OPEN)
        timer = createTimer(now.plusMinutes(5), [ |
          Pir_Proxy.sendCommand(CLOSED)
          timer = null
        ])
}
else {
    timer.reschedule(now.plusMinutes(5))
}
end

Hello @rlkoshak
I created a rule using your exemple but i received this error:

17:26:08.240 [ERROR] [org.quartz.core.JobRunShell          ] - Job DEFAULT.Timer 4 2020-07-10T17:25:44.564+03:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
  <null>.offTimer = <XNullLiteralImplCustom>
} ] threw an unhandled Exception:
java.lang.NullPointerException: null
        at org.eclipse.smarthome.model.script.engine.ScriptError.<init>(ScriptError.java:65) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:140) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:954) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:235) ~[?:?]
        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.evaluateArgumentExpressions(XbaseInterpreter.java:1205) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1135) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        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) ~[?:?]
        at com.sun.proxy.$Proxy163.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:?]
17:26:08.272 [ERROR] [org.quartz.core.ErrorLogger          ] - Job (DEFAULT.Timer 4 2020-07-10T17:25:44.564+03:00: Proxy for org.eclipse.xtext.xbase.lib.Procedures$Procedure0: [ | {
  <XFeatureCallImplCustom>.sendCommand(<XFeatureCallImplCustom>)
  <null>.offTimer = <XNullLiteralImplCustom>
} ] threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
        at org.quartz.core.JobRunShell.run(JobRunShell.java:213) [bundleFile:?]
        at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [bundleFile:?]
Caused by: java.lang.NullPointerException
        at org.eclipse.smarthome.model.script.engine.ScriptError.<init>(ScriptError.java:65) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:140) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:991) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._doEvaluate(XbaseInterpreter.java:954) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.doEvaluate(XbaseInterpreter.java:235) ~[?:?]
        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.evaluateArgumentExpressions(XbaseInterpreter.java:1205) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter._invokeFeature(XbaseInterpreter.java:1135) ~[?:?]
        at org.eclipse.xtext.xbase.interpreter.impl.XbaseInterpreter.invokeFeature(XbaseInterpreter.java:1081) ~[?:?]
        at org.eclipse.smarthome.model.script.interpreter.ScriptInterpreter.invokeFeature(ScriptInterpreter.java:151) ~[?:?]
        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) ~[?:?]
        at com.sun.proxy.$Proxy163.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) ~[?:?]
        ... 1 more

The rule is functional but i receive the error in Openhab console.
Can you help me to solve this ?
My rule is:

var Timer offTimer = null
rule "Handle PIR Events"
when
    Item Motionsw1 received update
then
    if(offTimer == null || offTimer.hasTerminated) {
        LightA23.sendCommand(ON)
        offTimer = createTimer(now.plusMinutes(1), [ | 
            LightA23.sendCommand(OFF)
            offTimer = null
        ])
    }
    else {
        offTimer.reschedule(now.plusMinutes(1))
    }
end

Thank you!

Is that supposed to be offTimer?