Openhab2 rules not working for me

Background:

  • I have installed Openhab2 on Ubuntu
  • I would like to control legacy X10 devices as well as z-wave devices via OH2 and the Smartthings Hub
  • I have installed into OH2 the CM11A Binding, and a Smartthings Binding
  • I CAN turn on/off X10 lights from Paper UI
  • I CAN turn on/off z-wave lights connected to the Smartthings hub via the PaperUI
  • I can see motion from a z-wave detector connected to Smarthings (active|inactive) within my OH2 log file

Situation

I have created a rule so that the X10 lights turn off after a period of no activity of the motion detector. I saved the rule in /etc/openhab2/rules as motion.rules and can see in the log file that the script is read.

 openhab.log:2017-05-25 08:19:56.516 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'motion.rules'

###--------Items----------###
Contact BasementSensor “Basement Motion Detector [MAP(contact.map):%s]” { channel=“smartthings:motionSensor:Home:BasementSensor:motion” }

Switch SwitchC1 “Basement Stairs” (someGroup) { channel=“cm11a:switch:MyCm11a:SwitchC1:switchstatus” }

###-----motion.rules----####
var Timer timer
rule "motion for 30 seconds"
when
Item BasementSensor changed
then
logInfo(“basement_motion”, “motion was triggered”)
if(BasementSensor.state == CLOSED)
{
timer = createTimer(now.plusSeconds(30)) [|
sendCommand(SwitchC1, OFF)
]
}
else
{
if(timer!=null)
{
timer.cancel
timer = null
}
}
end

Result

I CAN see the motion detector firing in the log file:

    2017-05-25 15:42:05.226 [INFO ] [ngs.handler.SmartthingsBridgeHandler] - Smartthings updated State for device: Basement Motion Detector - motion to inactive

But the rule is NOT triggered. I’m pretty sure I’ve screwed up the install or am missing something pretty basic. Any thoughts?

Furthermore

Just for the fun of it I created a very simple rule (write to the log when a switch is active) and cannot get it to fire either when I turn on the switch.

###-----switchtest.rules—###
rule "switch test"
when
Item SwitchC1 received update
then
logInfo(“test”,“it worked!”)
end

Look in events.log and verify that BasementSensor is indeed changing when the motion is detected. The log statement is saying that it is updating the state which may not result in a change in the Item’s state in which case the rule will not fire.

See this for some similar code that works. One thing I notice with this rule, assuming that it was working, is that the light will turn off in 30 seconds no matter whether the motion sensor continues to report motion.

Did you see switchtest.rules loading in the logs?

Do you see SwitchC1 changing in events.log?

1 Like

I was able to get this working. My problem as Rich stated was my configuration. The motion detector was appearing in the openhab.log but not in the events.log. I removed the motion detector from the items and things and used discovery via the smartthings binding and was able to load it successfully. Thanks for he help.

My overly verbose rule currently looks like this:

===========

var Timer timer = null // how long until lights will be shutoff
val int timeoutMinutes = 30

rule "Basement Lights Turnoff With no Motion for 30 min"
when
Item BasementMotionDetector_Motion changed or
System started
then
//logInfo(“motion”, “BasementMotionDetector_Motion is set to —” + BasementMotionDetector_Motion + “–”)
if(BasementMotionDetector_Motion.state == “active”)
{

  logInfo("motion","active motion in basement received")

  if(timer == null) {
    logInfo("motion","creating timer for new motion "+ timeoutMinutes)
    timer = createTimer(now.plusMinutes(timeoutMinutes ), [|
        logInfo("motion","turning off C1")
        sendCommand(SwitchC1,OFF) 
        timer = null
    ])
  }
  else {
    timer.reschedule(now.plusMinutes(timeoutMinutes )
    logInfo("motion","updated minuted by "+ timeoutMinutes)
  }
  logInfo("motion","SwitchC1.state="+SwitchC1.state)

}
else
{
   if(timer == null) {
    logInfo("motion","creating timer for inaction "+ timeoutMinutes)
    timer = createTimer(now.plusMinutes(timeoutMinutes ), [|
        logInfo("motion","turning off C1")
        sendCommand(SwitchC1,OFF)
        timer = null
    ])
    }

 }

end