General question about rule engine optimization and performance

Hi guys,
would like to ask few questions about rules engine, just to understand what is better in performance (memory etc)

  1. is it better to have as less as possible rules or number of rules doesnt matter?
    for example, is it bad to have simple rules like
    when item 1 changed to ON
    do something
    And then another rule when item OFF do something else. Is it better to have one rule when item 1 changed, then if =ON do X, else if =OFF do Y. ?

  2. if I have routines that can be triggered by 2 different ways, lets say physical button and virtual button for sleep routine. Is it better to have all the same repeated commands in both rules (switch off this, that and the other) or is it better to define such routine in 1 place, and then the other rule just says, if they press physical Sleep button, switch on VirtualSleep button (and he then executed the commands)
    In that case, one rule would trigger another rule. And in first case, each rule would have all the commands it needs, what is better optimized in general?


The differences in memory and CPU usage are negligible so choose what you can do best.

Just to elaborate on Marcus’ very correct answer, almost never does performance like discussed matter in a home automation context. Even an RPi is plenty powerful enough to process most events and most rules in < 200 msec which is plenty fast enough for home automation. Therefore what really matters is your optimization and performance.

What is easier to write? More importantly, what is easier to understand? Most importantly, what is easiest to maintain in the long run? That is the performance that you need to be concerned about.

Given that’s the case, you will want to apply DRY: Do Not Repeat Yourself. You should almost never have blocks of code that repeat, be they slight modifications on what to do in a big switch statement or a bunch of rules that do something slightly differently. Duplicated code means X times more work later on if you have to change something.

Finally …

This implies a lack of understanding about how rules actually work. Why have two rules with one that calls the other? Why not just the one rule that triggers by either event? openHAB is an event based system. Any given rule can be triggered by any number of different events.

1 Like

Hello and thank you for the meaningful explanations. I’m learning OpenHAB3, I’m trying to write rules, not everything works out … How to delete unnecessary rules?

Select the rule on MainUI and scroll to the bottom of the page, a red “Remove Rule” should appear. Click that one.

Thanks for the reply, but I do. Requests:
Are you sure you want to delete AutoOFFWallLight?Then “hangs” for a while without removing …

thanks for the info. Indeed, normally I use multiple triggers in my rules

Just tested the removal on my system, working ( no question by system)! That is for a rule created via the MainUI, if you try to remove a rule that is created from a (old?) .rules file it migth
be different and not even possible that way.