Very simple rule executes slow

Hey guys I have some complex rules that execute perfectly, instantaneously. but then I have this one simple rule which takes 4-6 and sometimes 10 or more seconds to execute. I’m not sure why, but I think it could be related to the timer.
the rule uses an insteon motion sensor as the trigger, then turns on the lights in my garage(insteon wall dimmer), sets a timer and turns them off again.

If anyone would like to take a look at it and offer some advice I’d very much appreciate it.

import org.openhab.core.library.types.*
import org.openhab.core.persistence.*
import org.openhab.model.script.actions.*

var Timer garageLightTimer = null
//Garage Motion Sensor
rule "Garage Motion"
	when
		Item GarageMotion received update
	then
		if(garageLights.state==0){
			sendCommand(garageLights,ON)
			if(garageLightTimer==null){garageLightTimer = createTimer(now.plusSeconds(600)) [|sendCommand(garageLights, OFF) garageLightTimer = null]}
			}
		else {garageLightTimer.reschedule(now.plusSeconds(600))}
end

You could achieve the same goal with the expire binding. No need for timers or even rules, makes life a lot simpler.

I’m not sure if expire works here, as far as I understand. Expire only works with the trigger item and wouldn’t control a separate item like my wall switch? and if I’m not understanding that properly could please show me an example of how I would use it that way?
sorry I’m not very good at this and my understanding is so limited.

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

Sorry I left out key information, I am running OH 2.1.0-01 on a raspberry pi2, Openhabian distribution.
for some reason I get no log entry when the motion sensor itself fires, so I base the timing off when the led indicator on the device itself is sending data and how long it takes from there.

Insteon in itself isn’t always as responsive as one would expect, I’m not sure if that’s a problem with Insteon or the binding (it’s been a long time since I briefly used their software). If I send commands to multiple switches/dimmers at once, I will usually see some kind of delay on one or more of them. but this rule specifically I consistently have the same timing issue.

Garage lights is a in wall dimmer, I will change the ON command to a numeric value.

Thank you so Much for your help, I don’t really have time to work on it tonight but I’ll test in the morning and hopefully will make some progress.

Thank you so much, I just got to try again. it’s much faster now. again thank you.