Rule delays when it is first time triggered

I have a little problem with a simple rule.
it happens after reboot or after reload the rule file.

When the rule is the first time triggered it has a delay, when it triggers additional times there is no delay anymore and the rule works as expected.

I have several *.rule files with nearly similar code in it and it is the same delay on all files.
(I use a RPi3 with OH 2.2.0 newest snapshot version)

Rule:

var long whenStarted2

rule "Button Pressed2"
when
  Item Mod_2i changed from OFF to ON
then
  whenStarted2=now.millis
end


rule "Button Released2"
when
  Item Mod_2i changed from ON to OFF
then
  val whenStopped2 = now.millis
  val timeTaken2 = whenStopped2-whenStarted2
	   
  		if (timeTaken2 < 800 ) {
    		if (Mod_4.state != ON ) {
    		sendCommand(Mod_4, ON)
    		}else {
    		sendCommand(Mod_4, OFF)
  			}
    	}
  	 	if (timeTaken2 >= 800 && timeTaken2 < 2000 ){
  	 		if (Mod_5.state != ON ) {
    			sendCommand(Mod_5, ON)
    		}else {
    			sendCommand(Mod_5, OFF)
  			}
  		}
  		if (timeTaken2 > 2000 ){
  			sendCommand(Mod_4, OFF)
  			sendCommand(Mod_5, OFF)
  		}	
  			
    	   
  
end

Log File:

2017-07-29 14:15:57.802 [INFO ] [el.core.internal.ModelRepositoryImpl] - Loading model 'longpress2.rules'

==> /var/log/openhab2/events.log <==

2017-07-29 14:27:17.645 [ItemStateChangedEvent     ] - Mod_2i changed from OFF to ON
2017-07-29 14:27:18.166 [ItemStateChangedEvent     ] - Mod_2i changed from ON to OFF
2017-07-29 14:27:19.559 [ItemCommandEvent          ] - Item 'Mod_4' received command ON
2017-07-29 14:27:19.595 [ItemStateChangedEvent     ] - Mod_4 changed from OFF to ON
2017-07-29 14:27:21.923 [ItemStateChangedEvent     ] - Mod_2i changed from OFF to ON
2017-07-29 14:27:22.263 [ItemStateChangedEvent     ] - Mod_2i changed from ON to OFF
2017-07-29 14:27:22.285 [ItemCommandEvent          ] - Item 'Mod_4' received command OFF
2017-07-29 14:27:22.299 [ItemStateChangedEvent     ] - Mod_4 changed from ON to OFF
2017-07-29 14:27:26.204 [ItemStateChangedEvent     ] - Mod_2i changed from OFF to ON
2017-07-29 14:27:26.379 [ItemStateChangedEvent     ] - Mod_2i changed from ON to OFF
2017-07-29 14:27:26.398 [ItemCommandEvent          ] - Item 'Mod_4' received command ON
2017-07-29 14:27:26.431 [ItemStateChangedEvent     ] - Mod_4 changed from OFF to ON
2017-07-29 14:27:27.754 [ItemStateChangedEvent     ] - Mod_2i changed from OFF to ON
2017-07-29 14:27:28.103 [ItemStateChangedEvent     ] - Mod_2i changed from ON to OFF
2017-07-29 14:27:28.123 [ItemCommandEvent          ] - Item 'Mod_4' received command OFF
2017-07-29 14:27:28.141 [ItemStateChangedEvent     ] - Mod_4 changed from ON to OFF

Hi Jörg,

could you solve this problem?
I have the same problem on my installation (openHABian on a RPI2).

It has always been like that since the origin of openHAB. It is probably due to the technical stuff behind the rule engine and the slow machine running OH. This problem was signaled many times and it is apparently not really a bug.
On a production system, it is not a problem because you restart it only few times per year, so you can live with the delay of first rule execution.

I think it is because your new rules need to be compiled again.

You can speed this up by splitting your rules into different files. This way only your changes file will be reloaded and the rest will stay fast and without that 10s compilation time.

Split them logically for you, not technically. Split them by what they do, not every 5th rule.

And if that doesn’t sound useful, you can put the rule you are developing in a separate rule file, and put it back when you wäre finished.

Hi Christian,
No the “problem” ist still there.
The only thing i notice is that this behavior is not on all rules the same.

On this rule, i can’t notice a longer delay. (perhaps depending on the complexity of the Rule?)

rule "Test Rule"
when
    Item Mod_3i changed from OFF to ON
then
	if (Mod_8.state!=ON)      // item OFF or uninitialized
        Mod_8.sendCommand(ON)
    else
        Mod_8.sendCommand(OFF)
end

Thank you for Answer, i use OpenHab for a long time but don’t use rules that long, therefore i notice it not that long.

Yes, i do it already this way…(and also for better overview, there are many rules when you will control the whole house)

Discussed here.
It’s a Java JIT thing.
Post below the one linked to links to a python based alternative.

Cheers