Displaying a list of motion detection events

  • Platform information:
    • Hardware: I3 9100 / 16GB
    • OS:Windows 10
    • Java Runtime Environment:Zulu
    • openHAB version:2.5
      I want to display the 10 latest motion events in my sitemap.
      The problem is that I can’t get the rule to work.
      The idea is to log the date/time of the detection and move the older detection time to another item. I created 10 “DateTime” items (MotionTime1 to MotionTime10) in my items file.
      I have a switch item called “MotionDtected” which is linked to a channel called “motionAlarm” of the IPcamera binding. The switch works as expected, when motion is detected it switches to on and after a while it switches off again.
      I expect my issue has something to do with copying of DateTime to another variable, but I can’t figure out why. Please note that I am not a programmer, so the rule I made is based what I could find in the topics.
      Below I pasted my rule. Your help is very much appreciated.
var DateTimeType Time1=null var DateTimeType Time2=null var DateTimeType Time3=null var DateTimeType Time4=null var DateTimeType Time5=null var DateTimeType Time6=null var DateTimeType Time7=null var DateTimeType Time8=null var DateTimeType Time9=null var DateTimeType Time10=null

rule “Create timestamp of latest motion detection”
when
Item MotionDetected received command
then
if (receivedCommand == ON) {
Time10=Time9
Time9=Time8
Time8=Time7
Time7=Time6
Time6=Time5
Time5=Time4
Time4=Time3
Time3=Time2
Time2=Time1
Time1=( new DateTimeType() )

        MotionTime1.postUpdate(Time1)
        MotionTime2.postUpdate(Time2)
        MotionTime3.postUpdate(Time3)
        MotionTime4.postUpdate(Time4)
        MotionTime5.postUpdate(Time5)
        MotionTime6.postUpdate(Time6)
        MotionTime7.postUpdate(Time7)
        MotionTime8.postUpdate(Time8)
        MotionTime9.postUpdate(Time9)
        MotionTime10.postUpdate(Time10)
    }

end

You should set the variable with DateTime
and the last Time1 = new DateTime()

Hi Charley,

if I do that then I get a an error on all the MotionTimeX.postUpdate(TimeX) lines:

Type mismatch: cannot convert from DateTime to State

How did you set the Items MotionTime1, MotionTime2 …?

You could also try to update the item like this because they are set the same way

MotionTime1.postUpdate(MotionTime2.state);

What is not working? Does the rule trigger? Add a log statement to the beginning of the rule and tail the openhab.log while triggering motion. I expect you will need to change the rule to trigger on change events. Also, the global variables are not needed, since you have the Items.

import org.eclipse.smarthome.model.script.ScriptServiceUtil

rule “Create timestamp of latest motion detection”
when
Item MotionDetected changed to ON
then
    logInfo("Motion timestamp", "Rule started")
    (2..10).forEach[index|
        postUpdate("MotionTime" + index, ScriptServiceUtil.getItemRegistry.getItem("MotionTime" + (index - 1)).state.toString)
    ]
    MotionTime1.postUpdate(new DateTimeType)
end

Hi Scott,

It seems that my trigger wasn’t working correctly. I changed it from:

when
        Item MotionDetected received command
then
        if (receivedCommand == ON) {}

to

when
        Item MotionDetected changed to ON
    then
       ....

I also tried the code provided by you which also works fine and is definitely a better way of doing it, but it is not something that I could write.

Thank you very much for your help.

1 Like