Need help with rules UI contribution

Hello everyone!

I’m a bachelor’s student and I am thinking about contributing to OpenHab as a part of my thesis.
What I am mostly interested in, is to improve the way that you can create rules in the paper ui e.g. by using the experimental rules engine.
My goal is a UI that can be used to easily create complex rules without needing to script anything.

I have already started by experimenting with OpenHab on a Raspberry Pi and digging through the code, but with a project this big I am somewhat overwhelmed.
Could anyone point me in the right direction to where I could start, or who I could ask a few questions regarding the rules engine? Or is there anyone currently working on the experimental rules engine, so I could get in touch?

I would appreciate any advice :slight_smile:

1 Like

Someone already started such a UI: https://github.com/ghys/flowsbuilder

I’d start looking there. I think development on it stalled waiting for the Experimental Rules engine to mature a bit more.

Thanks a lot! I will definitely have a look :slight_smile:

Do you by coincidence have any idea what state the experimental rules engine is currently in?
Since the last commit was about one and a half years ago, I wonder if it makes sense to resume developing now or if it makes more sense to wait even more.

I’ve no idea. I know there have been updates to the Experimental Rules Engine more recent than a 1.5 years ago. But I also know it is not complete by any means. The last I looked it lacked support for logging and calls to Actions. It also lacks adequate documentation.

I use the Experimental Rules Engine with JSR223-Jython. I’ve never tried creating rules through Paper UI, but it looks like some triggers that I’ve created may be showing up…

If you’re looking for the current code, start here and here.

I’ve read posts where people have suggested that there are “issues” with the new rules engine, but I haven’t seen any specific reports. I’m guessing that any issues, if they still exist, are in the UI. Current issues are here, but due to their age and recent PRs, I question if all of them are still valid. Adding the PaperUI tag filters them to UI reported issues.

It is not entirely clear how one would use the ERE with JSR223 written Rules. When I choose the spot for writing the Rule Body I have no option to choose anything but JavaScript and given a blank text pane where I can write said JS code. I see no way to point the Rule to, for example a Jython written Rule. Examples would be helpful if you know a way.

You mean in the UI? I just took a quick look, and I think I see what you mean. Putting in an Action to run a script, I don’t see anything in the dropdown or a way to link a file, but putting a script in the box worked…

The script was…

var log = Java.type("org.slf4j.LoggerFactory").getLogger("org.eclipse.smarthome.automation.module.script.rulesupport.internal.shared.SimpleRule");
log.info("This is a 'hello world!' from a ERE Javascript Action.");

2018-09-19 02:06:57.509 [DEBUG] [org.eclipse.smarthome.automation.core.internal.RuleEngineImpl] - The trigger '1' of rule 'ccb02c96-6d3f-414b-a060-c46994cc13e2' is triggered.
2018-09-19 02:06:57.515 [INFO ] [org.eclipse.smarthome.automation.module.script.rulesupport.internal.shared.SimpleRule] - This is a 'hello world!' from a ERE Javascript Action.

I assume the JS could just read from a script file. I can also select a ‘run rules’ action, and chose one of my Jython rules. Unfortunately, they are all named the same thing, but I’m currently working on a massive change to the Jython triggers.py module that will hopefully correct that.

That is what I meant. So theoretically we could write a rule in any of the JSR223 Rules and use the “Run rules” option. I wonder if it allows one to define a Rule without any triggers at all. If we are controlling how they get triggered using the Experimental Rules Engine then we don’t need triggers on our Rules files at all.

I could find no way to load a JS from a file short of copy and paste.

The lack of documentation makes it really difficult to guess how all the parts interact with each other. But if I can call JSR223 rules but set up the triggers using the Experimental Rules Engine, that would go a long way towards making the ERE more useful in the near term since the missing pieces (i.e. logging and ability to call Actions) will be available in Jython et. al. even if they are not available to the JS entered in the Script text box in PaperUI.

I don’t think there are missing pieces… just a lack of documentation! The example JS script I used had logging, and I don’t see why you couldn’t use Actions in the script. I use them in Jython. I’ll circle back to UI ERE in a bit with a more involved example and writeup. Also, you can create a UI ERE rule without triggers, and execute it from another rule. And there are templates!

1 Like

Nice, the flowsbuilder looks very promising, thank you! :slight_smile:

Do I understand it correctly that JSR223 needs to be installed separately and makes it possible to write python Code in the ERE Script Text Fields?

For now all I managed to do using ERE with scripts is to evaluate the current time using this Script:

(function() {
   var date = new Date();
   var h = date .getHours();
   if(h > 21 || h < 4) {
      return true;
   } else {
      return false;
   }
})()

Anyway I’d rather not code at all in the UI and make everything accessible through just clicking things together.

Based on what Scott described, it seems you can call Python Rules that you have written as text files from the ERE. As far as I can tell you can only write JavaScript in the ERE Text Fields.

Unmanaged Javascript rules show up too.