Very simple rule executes slow

How are you determining how long it takes to execute? As written there are no log statements. If you are basing this only on how long it takes for the light to turn on before you think it should then the problem can be with the motion sensor or the binding attached to garageLights.

I do see a number of areas of concern with the rule itself.

  • You don’t say what version of OH you are running. These days, if you don’t specify what verison of OH you are running the assumption it is the 2.1 release. With that assumption…

  • You should not import anything from org.openhab in any of your rules. You need not import anything from org.joda nor should you import anything from org.eclipse.smarthome.

  • What is garageLights? You test it for 0 but then send it ON. If it is a Switch, test it for ON. If it is a Number, sendCommand 1

  • This is a personal style issue, but try not to have the most important lines of your code so far off to the right that you have scroll to see them. It’s OK to have newlines in code.

  • I agree with Mikael, Expire binding is probably a good way to go, but you won’t be able to get rid of your Rule entirely. See below.

  • You should use the method rather than the action for calls to sendCommand and postUpdate (see below)

See Expire Binding Based Timers for a detailed example of how to use the binding in place of timers.

If I assume that your garageLights only ever turns on based on this motion sensor than:

Switch garageLights ... { ... expire="10m,command=OFF" }
rule "Garage Motion"
when
    Item GarageMotion received update
then
    garageLights.sendCommand(ON)
end

The Expire binding will turn the light off 10 minutes after the last time it receives an ON command.

If you want to keep the expire binding part separate from the Lights Item (e.g. you have other ways you want to control the light that should not automatically turn off after 10 minutes):

Switch GarageLights_Timer { expire="10m,command=OFF" }
rule "Garage Motion"
when
    Item GarageMotion received update
then
    if(garageLights.state == OFF) garageLights.sendCommand(ON)
    GarageLights_Timer.sendCommand(ON)
end

rule "Garage Lights Timer"
when
    Item GarageLights_Timer received command OFF
then
    garageLights.sendCommand(OFF)
end

Depending on how your Motion sensor works you can use the Expire Binding on just that Item. For this to work your GarageMotion must receive an ON/OPEN every time it senses movement but not turn OF/CLOSED on its own.

Switch GarageMotion ... { ... expire="10m,command=OFF" }
rule "Garage Motion"
when
    Item GarageMotion received update
then
    if(GarageMotion.state == ON && garageLights.state == OFF) garageLights.sendCommand(ON)
    else if(garageLights.state != OFF) garageLights.sendCommand(OFF)
end
1 Like