Need help with simple rule for Motion sensor

Hello,

i want to create a rule in openhab2 that send a mail, if no one moving in the room for example 4 hours. or between 2 pm - 7 pm.

That’s my item file:

Number Living_Motion “Motion [MAP(sensor.map):%s]” (Living_Mot) {mqtt="<[mosquitto:tinkerforge/bricklet/motion_detector/wfh/motion_detected:state:JSONPATH($.motion)]"}
String Living_Motion_UTC “Last motion detection [%s UTC]” (Living_Mot) {mqtt="<[mosquitto:tinkerforge/bricklet/motion_detector/wfh/motion_detected:state:JSONPATH($._UTC_timestamp)]"}

rule:

rule "MOTION_MAIL"
when
Item Living_Motion changed
then
if (DU1_E35_Motion.state == 1){

	sendMail("mymail@gmail.com", "Motion message" , "blabala")

}
end

You want to operate off a NON-event. (eg nothing happened for 4 hours).

  1. Create a virtual item to hold a latch variable for the state of the motion detector
  2. Use the EXPIRE binding to set a timeout value and state on the latch variable.
Switch MotionLatch {expire="4h,command=OFF"}

whenever you detect motion, in that rule predicate:

MotionLatch.sendCommand(ON)   //or postUpdate(ON)

after 4 hours of no detected activity, the Expire binding will switch the state of MotionLatch, so:

rule "motion latch goes OFF"
when
   Item MotionLatch changed to OFF
then
  //react to no motion as you wish
end

Hey bob many thanks for your help!

do i have to write in the items file:

Switch MotionLatch {expire=“4h,command=OFF”}

in which file do i have to write:

MotionLatch.sendCommand(ON) //or postUpdate(ON)

yes . write the item definition in items file exactly as listed.

the MotionLatch.sendCommand(ON) goes into a rule file which is triggered by however your motion detector indicates motion occurred. Guessing from your fragment it would be something like:

rule "Motion ON"
when
    Item Living_Motion changed
then
    if (DU1_E35_Motion.state == //however it indicates detected motion ){
        Motion_Latch.sendCommand(ON)
end

Hey Bob, i tried so many things, but nothing happens. Can you help me, what is wrong? I only installed Expire binding in Paper UI, is that ok?

That’s my items File:

Number Living_Motion “Bewegung [MAP(sensor.map):%s]” (Living_E35_Mot) {mqtt="<[mosquitto:tinkerforge/bricklet/motion_detector/wfh/motion_detected:state:JSONPATH($.motion)]"}
String Living_Motion_UTC “Letzte Bewegungserkennung [%s UTC]” (Living_E35_Mot) {mqtt="<[mosquitto:tinkerforge/bricklet/motion_detector/wfh/motion_detected:state:JSONPATH($._UTC_timestamp)]"}

Switch MotionLatch {expire=“10s,command=OFF”}

My Rules File:

//this rule works
rule "MOTION on"
when
Item Living_Motion changed
then
if (Living_Motion.state == 1){

	sendMail("my@gmail.com", "Hello" , "Motion detected")
	        MotionLatch.sendCommand(ON)

}
end

////this rule doesn’t work
rule "Living_Motion goes OFF"
when
Item Living_Motion changed to OFF
then

       sendMail("my@gmail.com", "Hello" , "No Motion detected")

end

You need that rule to trigger on the MotionLatch item not Living_Motion

Hi ray, i try that, but it doesn’t work.

You need to examine the events.log file and verify what commands are happening and when.

After calling:

MotionLatch.sendCommand(ON)

you should see a related entry:

…Item ‘MotionLatch’ received command ON` or it could be changed from OFF to ON

You’ve set a sensible short 10second timer for testing so waiting 10 seconds and you should see that event go through the events.log

…Item ‘MotionLatch’ received command OFF`

Verify the basic bits are working and move up from there. I find making lots of changes to items and rules can upset OH so also worth restarting the service sometimes to make sure all is well. Also as it starts monitor the openhab.log and make sure no errors are coming up.

Hey Ray, i reboot my raspberry many times and after than i don’t see in the event. log file:

MotionLatch.sendCommand(ON)

but I see :

2017-11-09 19:57:01.785 [ItemStateChangedEvent ] - Living_Motion changed from NULL to 0
2017-11-09 12:58:55.696 [ItemStateChangedEvent ] - Living_Motion_UTC changed from NULL to 2017-11-09 08:32:46

And in the Openhab.log i have many errors like:
2017-11-09 21:34:45.123 [WARN ] [el.core.internal.ModelRepositoryImpl] - Configuration model ‘HSLU_AAL.items’ has errors, therefore ignoring it: [4,28]: mismatched input ‘�’ expecting RULE_STRING
[4,29]: missing ‘}’ at ‘10s’
[4,32]: extraneous input ‘,’ expecting RULE_ID

2017-11-09 21:19:50.596 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Living_Motion’ for widget org.eclipse.smarthome.model.sitemap.Text
2017-11-09 21:19:50.600 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Living_Motion’ for widget org.eclipse.smarthome.model.sitemap.Text
2017-11-09 21:19:50.602 [ERROR] [ui.internal.items.ItemUIRegistryImpl] - Cannot retrieve item ‘Living_Motion’ for widget org.eclipse.smarthome.model.sitemap.Text

What do you think is that so? Do i need a Binding in Paper ui for sendcommand?

What is in HSLU_AAL.items? Although OH has labelled it [WARN] it’s also clearly telling you that the file is being ignored due to problems found in it. It’s generally a bad idea to ignore errors, sure sometimes it’s not relevant and you can get away with it but looking at what has been suggested in this thread it is really a ‘simple rule’ and unlikely a bug in OH therefore you need to pay attention to the detail of your error logs.

[4,28] I think refers to line 4 column 28 so go fix it in HSLU_AAL.items. Get your start up clean and error free, make sure the items you’ve created are visible in PaperUI, sitemap, Habpanel or whatever GUI tool you’re using and then go back to testing the execution and monitor events.log for MotionLatch entries.

I agree with Ray on debugging the error you are getting. Depending on what else is in that .items file, it might just clear everything up.