openHAB rules files are lazy loaded. When a rule is used the first time, the entire rule file is loaded and compiled, which apparently is single-threaded and takes a couple seconds. Even on a reasonable powerful Rasperry Pi 4. This happens both after restarting openHAB and after changing the rules file. It also means that whatever the rule in question is supposed to do has to wait a couple seconds when requested for the first time. Imagine you have a light switch, enter a room, press the switch, and it takes 5-10 seconds until the light comes on.
To work around this problem, I add a piece of code in all rules files which triggers an immediate action right after loading the file. This way the rules file is already pre-loaded (which again takes a couple seconds, but likely at a time when the rule is not needed right now).
The code which I insert into every rules file:
|
|
The “System started” trigger in openHAB is misleading: it’s not only fired when the entire system starts, it is also fired when the rules file is loaded. That’s useful in my case, because it allows me to set a variable. And that’s enough to trigger the entire compilation of the file.
There is a second trigger which fires every 30 minutes, and which refreshes the rules file compilation.
<name>
is a unique name I use for every rules file. Since the variable rules_last_updated_<name>
is local to evey rules file this should not matter. However it allows me to expand this rule and log into the openHAB log when the rules file is compiled.
Now I don’t really add the entire code to every rules file, because I’m even more lazy. My installation is deployed using Ansible and templates, and in every rules file I just include another template with above code. Here is the template, in my case the name is performance.j2
:
|
|
And in every rules file I insert the following small code fragment:
|
|
When this is deployed, Ansible inserts the template from performance.j2
into every rules file, and when openHAB loads the file it instantly triggers the compilation.