How does the rules engine work. where can I start making changes

Hello everyone,

I noticed that rules aren’t always reliable. I wanted to looks at the code and make potentially some changes to it however the documentation on GitHub only provided a very high level on how rules works.

I had instances were the openHab logs shows an events but the rules doesn’t trigger. I am not sure what part of the code is responsible of the logs and which one is responsible for triggering rules and why would a rules fire sometimes but once in a while it won’t.

I would really appreciate if someone can give me some more details and explanation of each layer starting from a sensor to the rules and which part of the code handle those events.

Thanks,

1 Like

openHAB supports multiple rule systems.

It seems likely you ae talking about “classic” Rules DSL. This won’t be developed further, so there’s probably not much point in putting a lot of work into it.

It does however reliably trigger rules. Every case I’ve seen about supposed failure to trigger a rule has actually been about something else - a misunderstanding of triggers, or a badly written rule hogging rsources, or such.

what do you mean by hogging rsources? also my rules works fine 99% of the times but sometimes I get a missing events. I just want to know what part of the code trigger the rules and why sometimes logs show the event without the rules triggering?

Also what do you mean by developed further? is being replaced?

Speaking from my own experience, when my rules failed to trigger it was usually due to an IF condition that wasn’t working the way I expected. In one case, events would sometimes happen slightly out of sequence, causing an IF to be false.

Since cleaning up my IF statements, I haven’t had any trouble.

Rules DSL is what most people use, and it will be deprecated in OH3. Rules built this way will still work, but the intention is to transition to what’s called the Next Generation Rules Engine. You can start using it now if you want. A few of our members have been posting resources to help others get started.

1 Like

see here

oh… and welcome @hannaPolaskus to the openhab community

1 Like

It’s a little more nuanced than that.

Right now there are two rule engines, Rules DSL Engine and NGRE.

Rules DSL Engine:

  • only run Rules DSL Rules
  • has a limit of only five Rules that can run at the same time (the likely cause of OP’s problem)
  • does not have a REST API (i.e. doesn’t appear in PaperUI or what will be it’s replacement)

NGRE:

  • currently supports Jython, JavaScript or Groovy
  • in OH 3 will also support Rules DSL (seems to be the plan at least)
  • supports building rules using text source files or building rules through the REST API (i.e. through a GUI)
  • unlike Rules DSL Engine, will only run one instance of a given rule at a time
  • unlike Rules DSL Engine, there is no limit to the number of different rules that can run simultaneously

Rules DSL Engine is likely going away. Rules DSL as a language to write rules itself is likely going to be around for a bit pending overcoming technical difficulties.

As with all things OH 3, stuff is in flux and is likely to change several times before the first beta release.

As Andrew pointed out with the link to that thread, Rules DSL Engine can only run up to five Rules at the same time. If you have long running Rules (anything more than a couple hundred milliseconds is considered long running) you may encounter a situation where all five threads are in use meaning that later rule triggers either have to wait or get dropped.

This is a limitation of the Rules DSL Engine and, as mentioned, that engine is going away. This is not a problem with NGRE and it shouldn’t be a problem with NGRE even when using Rules DSL. Though I can’t say that for sure.

1 Like

Thanks, Rich! I didn’t realize that the NGRE supports Rules DSL. I had always interpreted the discussions to mean that the old engine would still be available for use.

It doesn’t yet but based on github comments Kai is furiously at work trying to make Rules DSL rules work on NGRE. And as I said, there may be technical or other reasons why that may not come to fruition and stuff may change. Nothing is set in stone.

I think that has more to do with the conflation of the engine and the language. Most of the time “Rules DSL” is used to reference the language rules are written in and the engine which executes the rules. But the two are in fact separate.

1 Like

If you are curious, go bounce around github a bit. From what I’ve been reading, and I could be wrong but…
the dsl rules engine is already removed from OH3
rules dsl engine will be able to be installed as an option in OH3
new generation rules engine is no longer called that, it is ‘the rules engine’.
There will be a services which will be capable of creating running rules from dsl rules files
on that last note, the developers are very aware that there are tons of dsl rules (and huge time invested) out there and not providing an (easy automated) upgrade path is not an option

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.