PIR Sensor rule in OpenHAB

Hi, i have recently started working with OpenHAB. I have an LED and a PIR Motion Sensor connected to my Raspberry Pi. I want to define a rule so that whenever motion is detected by the sensor, the LED is turned on. On using

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

turns the LED on whenever i turn on the switch for my motion sensor and not when motion is detected by the sensor. Any help would be appreciated.

You really need to specify your configuration a little more to get help. For instance, how are the ‘switch for motion sensor’ and ‘motion is detected by the sensor’ defined in the items file, and how are they actually triggered.

One thing is noticeable though, is that the line

Item MotionSensor changed

will trigger the command when the motion sensor item goes from true to false and false to true. I think most motion sensors will be true when motion is detected so should read

Item MotionSensor changed to TRUE

But again, your motion sensor may be different.

If MotionSensor is a Contact item, you would probably want to trigger on OPEN:

Item MotionSensor changed to OPEN

It is a contact item but
changed to OPEN didnt work for me

my items file has:

Switch RaspiLED{ gpio pin:17 }
Switch MotionSensor{ gpio pin: 18}

Rules:

rule "MotionDetected"
when
Item MotionSensor changed to TRUE
then
sendCommand(RaspiLED, ON)
end

but neither changed to True or On worked for me. I tried with MotionSensor as a Contact and defined in terms of changed to OPEN, but that also didnt work for me

For debugging purposes try

Rule "test"
when
    Item MotionSensor changed
then
    logInfo("test", MotionSensor.state)
end

or check the events.log to see the states the object passes.

It shows
Error during execution of the rule ‘test’: The name ‘LogInfo (,<XMemberFeatureCallImplCustom)’ cannot be resolved to an item or type.

Damn you autocorrect. It should be logInfo with a lowercase first letter. I have adjusted the code.

so when the sensor is on, it shows nothing in log when motion takes place. Although from pi terminal i can read the value of gpio pin to have changed from 0 to 1 when motion takes place.

when i turn on or off the switch of sensor from openhab , it shows "Received unknown command ‘TOGGLE’ for MotionSensor

Maybe you should share your item, rule and sitemap…

So it is not clear what is the current setup…
“MotionSensor” should be a contact and only set to OPEN or CLOSED by changing pin 18 on Raspberry

Example of the Wiki (GPIO Binding · openhab/openhab1-addons Wiki · GitHub)
Contact PIR "PIR" { gpio="pin:4 activelow:yes" }

The first difference to your item is the binding config…
Contact MotionSensor {gpio="pin:18"}

If your PIR is set to low voltage at Motion, you should add activelow:yes as specified in the example.

So please try the following items:

Contact MotionSensor "PIR" { gpio="pin:18" }
Switch   RaspiLED  "LED" { gpio="pin:17"}

and for the rule:

rule "LED"
when
  Item MotionSensor changed
then
  if (MotionSensor.state == OPEN) {
    RaspiLED.sendCommand(ON) }
  else {
    RaspiLED.sendCommand(OFF) }
end

And finally for the sitemap:

Switch RaspiLED
Text MotionSensor

The sitemap should show a switch which represents the state of the LED (ON or OFF).
The MotionSensor is displayed as text with its state “OPEN” or “CLOSED”.

Tests:

  • toggling the switch should set the LED on or off.
  • with detected motion of your PIR the state should change from “CLOSED” to “OPEN” (or vice versa dependent to the voltage Levels at the PIR-Output)
  • The rule should change the LED when Motion is detected

Andreas

defining MotionSensor as text is not able to retreive a widget for sensor

ok so i did what you told

but my motion sensor is displayed as a text without its state OPEN or CLOSED.
when motion takes place, LED is not turning on

I’m sorry… some changes…

item:

Contact MotionSensor "PIR [%s]" { gpio="pin:18" }
Switch   RaspiLED  "LED" { gpio="pin:17"}

Without [%s] the text OPEN / CLOSED is not shown…

sitemap:

Switch item=RaspiLED
Text item=MotionSensor

Sorry…

Can you switch the LED by switching on in the sitemap?

Are the Pin-Definitions correct? The GPIO-Numbeer is not the same as the Pin-Number on the connector!

Maybe this Project gives you some more hints?

my led is working perfectly fine and also in accordance with the rules i defined for led. Only the sensor is not turning on the LED when motion is detected

Just found another Topic…

and the hint to this Topic

ok…even I am using Jessie. I think that might be the issue then. Thanks Andreas :slight_smile: