OpenHAB2 motion rule not working

I just made the change to OpenHab2. I am having problems with a rule that’s suppose to turn on the basement lights when motion is detected. It worked fine in OpenHab1. I can control the lights in my basement with OpenHab2 and the motion detection is being picked up by OpenHab2, but the rule is not executing. I am getting errors on the “==” & “!=” lines in designer. I can’t see anything wrong in the log, but I don’t know how to use debug mode. Any help would be appreciated.

 rule "Basement Motion Lights"
 when   
             Item BasementMotion changed from CLOSED to OPEN or 
             Item BasementMotion changed from OPEN to CLOSED
 then   
             val String state = if(GBasementLights.allMembers.filter(s | s.state as DecimalType == 0).size > 0) "60" else "0"
                    
                 if(basementTimer != null) {
                      basementTimer.cancel
                      basementTimer = null
                  }
        
                 // Toggle any non-overridden lights
                 if(GBasementLights.allMembers.filter(s | s.state as DecimalType == 0).size > 0) {
                     V_WhoCalled.sendCommand("BMOTION")
                     Thread::sleep(200) // give MOTION time to take effect
                     GBasementLights.members.forEach[ light |
                       if(GLightsOverride.members.filter[o|o.name == light.name + "_Override"].head.state == OFF &&
                           light.state.toString != state.toString){

                            if(light.state.toString != state.toString) {
                                logInfo("Lights", "Basement Motion turning " + light.name + " " + state.toString)
                                light.sendCommand(state.toString)
                                try {Thread::sleep(100)} catch(InterruptedException e){} // don't overwhelm "Any light in gLight triggered" rule
                            }
                         }
                    ]
               }  
                     basementTimer = createTimer(now.plusMinutes(30)) [ |
                        // OFF code
                        V_WhoCalled.sendCommand("BMOTION")
                        try {Thread::sleep(200)} catch(InterruptedException e){}
                            if(GBasementLights.allMembers.filter(s | s.state as DecimalType != 0).size > 0) {
                            GBasementLights.members.forEach[ light |
                            if(GLightsOverride.members.filter[o|o.name == light.name + "_Override"].head.state == OFF &&
                                light.state.toString != state.toString){

                              if(light.state.toString != state.toString) {    
                                  logInfo("Lights", "No Basement Motion turning " + light.name + " " + state.toString)
                                  light.sendCommand(state.toString)
                                }
                            }
                     ]
                     try {Thread::sleep(120000)} catch(InterruptedException e){}
                     if(V_WhoCalled.state == "BMOTION") {
                        V_WhoCalled.sendCommand("MANUAL")
                     }
                   }
                 ]
                     Thread::sleep(120000)
                     if(V_WhoCalled.state == "BMOTION") {
                        V_WhoCalled.sendCommand("MANUAL")
                     }
 end

First I’ll say that Designer is currently a little broken and is marking things as error which are not an error. In particular it cannot recognize Items, Actions, or now.

Secondly, I’ve found the ESH Designer needs a little bit of a nudge to figure out the types of variables in certain circumstances. So, for example it may complain that it can’t find the .state method in the line

if(GBasementLights.allMembers.filter(s | s.state as DecimalType != 0).size > 0) {

But will be happy if you add the type for s:

if(GBasementLights.allMembers.filter(NumberItem s | s.state as DecimalType != 0).size > 0) {

Finally, you do not say exactly what is not working about this rather complex rule. You need to add some logging to narrow the problem down further to a specific line of code in the rule. The Designer errors in this case are red herrings (misleading and/or irrelevant clues for the non-English speakers) and should be ignored.

If there really is an error here you don’t need the debug log. The error will show up in the default openhab.log file at the Info or above level.

I’m trying to get my Monoprice PIR motion sensor to work and am having a heck of a time at it. I say I have it 60% right now and will let you know how I got there and some strange things I’ve seen.

Set up as a contact for the item, and a text widget in the sitemap, it was displaying ON at all times and briefly OPEN when tripped (returning to ON when browser refreshed, note: OPEN updated the browser automatically). I could not get a rule to fire at all with this setup.

Set up as a switch item (hint… I looked at Chris Jackson’s channel listing as for burgler-alarm) and text widget for the sitemap, the switch always shows ON once initially tripped. I have been able to fire a rule with the “received update” designation.

The motion sensor sends two signals… once when it is tripped, and again after 3 minutes of no motion. The manual says it should be ON and then OFF but my switch item never changes, it stays as ON. I can confirm that two signals are being sent as the rule fires twice so there are two updates. Why Basic UI doesn’t update the second one I don’t know.

Next is to try to set the rule as “received update ON” to see if it fires, and hopefully… only once.

To add the confusion, in Habmin, the status when it fires is listed as “Alarm”.

In OH1 I had these set up easily, I’m not sure why these silly motion sensors have had me banging my head against a wall for the better part of a week.

Rich,
Here is the portion of the log where I walked down into my basement. I didn’t see any errors. Can you see anything wrong from this?

 ==> /var/log/openhab2/events.log <==
2016-10-28 07:43:48.635 [ItemStateChangedEvent     ] - FoyerMotion changed from CLOSED to OPEN
2016-10-28 07:43:48.671 [ItemCommandEvent          ] - Item 'Foyer' received command ON
2016-10-28 07:43:48.683 [ItemStateChangedEvent     ] - Foyer changed from OFF to ON
2016-10-28 07:43:49.012 [ItemStateChangedEvent     ] - Partition1 changed from Ready to NotReady
2016-10-28 07:44:01.662 [ItemStateChangedEvent     ] - BasementMotion changed from CLOSED to OPEN
2016-10-28 07:44:01.699 [ItemCommandEvent          ] - Item 'Basement' received command ON
2016-10-28 07:44:01.720 [ItemStateChangedEvent     ] - Basement changed from OFF to ON
2016-10-28 07:44:01.749 [ItemCommandEvent          ] - Item 'V_WhoCalled' received command BMOTION
2016-10-28 07:44:01.752 [ItemStateChangedEvent     ] - V_WhoCalled changed from MANUAL to BMOTION
2016-10-28 07:44:01.770 [ItemStateChangedEvent     ] - FoyerMotion changed from OPEN to CLOSED
2016-10-28 07:44:02.030 [ItemStateChangedEvent     ] - Partition1 changed from NotReady to Ready
2016-10-28 07:44:19.535 [ItemStateChangedEvent     ] - FoyerMotion changed from CLOSED to OPEN
2016-10-28 07:44:19.570 [ItemCommandEvent          ] - Item 'Foyer' received command ON
2016-10-28 07:44:19.982 [ItemStateChangedEvent     ] - Partition1 changed from Ready to NotReady
2016-10-28 07:44:22.278 [ItemStateChangedEvent     ] - BasementMotion changed from OPEN to CLOSED
2016-10-28 07:44:22.355 [ItemCommandEvent          ] - Item 'V_WhoCalled' received command BMOTION
2016-10-28 07:44:30.001 [ItemStateChangedEvent     ] - FoyerMotion changed from OPEN to CLOSED
2016-10-28 07:44:30.290 [ItemStateChangedEvent     ] - Partition1 changed from NotReady to Ready

That is the events log which shows your Items being updated. If there are any errors they will appear in the openhab.log file.

Based on the events, BasementMotion is changing so your rule should be firing.

This is all I see in the openhab.log file when I try to trigger the rule.

 2016-10-31 10:06:06.468 [WARN ] [org.openhab.io.net.http.HttpUtil    ] - Method failed: HTTP/1.1 401 Unauthorized
 2016-10-31 10:06:06.472 [ERROR] [inding.ecobee.internal.EcobeeBinding] - Error retrieving tokens: authorization_pending
 2016-10-31 10:06:06.473 [WARN ] [inding.ecobee.internal.EcobeeBinding] - Periodic poll skipped for 'DEFAULT_USER'.

The Ecobee error is due to the fact that I haven’t set up my Ecobee thermostat with OpenHAB2 yet. I am only testing OpenHAB2 out when I have a few spare hours. I am mainly still running OpenHAB1. Not sure on the HTTP error. I get them in OpenHAB1 also.
V_WhoCalled changes from “MANUAL” to “BMOTION” and then back to “MANUAL”. So the rule is running, but not turning on the lights. The lights are Zwave. I can use the sitemap to turn the lights on, so they do work in OpenHAB2.
Any other ideas?

OK, so this is what we know:

  • the lights work because you can toggle them on and off using Switches bound to them
  • You know the rule is being triggered

So all that remains is there being a logic error in the rule somewhere. It’s a complex rule and very difficult to follow on a phone so I’ll tell you how I would debug this.

Add lots and lots and lots of logging to the rule to narrow down how far the rule is getting and the relevant states of Items and variables at each stage so you can see what is happening. You should be able to pinpoint down to the specific line of code where it is either taking a wrong or unexpected path or failing. Once you know where it is failing you should get an idea of why.

When I migrated some of the gotchas that hit me were Items that were uninitialized which were not back when I wrote the rule in the first place. Something like that could explain why it works in OH 1 but not when you migrate to OH 2.

Not sure what happened but the rule started working again. I was removing some of the imports from my rules files left over from OpenHAB1. Not sure if that fixed it or not.