Next-Gen Rules: definition via text files?

Tags: #<Tag:0x00007f617c529ec0> #<Tag:0x00007f617c529d80> #<Tag:0x00007f617c529c40>

I’m planning a whole new refactoring of my rules, which are DSL rules atm.
Right now I’m diving into Next-Gen rules and I’d like to know, if there’s a way to define the rules via text files? I don’t find anything on it, so I assume, they have to be created in PaperUI right now.
I’m a text file guy, because that allows me to backup the configuration files and simply put them in a fresh vanilla openHAB installation and OH2 will built itself anew, which quite frankly saved my ass quite a few times, as I messed up something or as I switched hardware from Pi2 to Pi4…

So: is there a way to describe Next-Gen Rules via text? or: how can I backup/save rules created in PaperUI?

For sure!
@rlkoshak & @5iver are the experts for that here.

Check out this post: [beta testers wanted!] Jython addon w/ helper libraries (requires OH 2.5.x)

2 Likes

I saw you liked my start at documenting the JSONDB Rules. Those docs are still reasonably relevant but not at all up to date. You can code in Jython, Nashorn JavaScript, or Groovy in text files. Jython has the most docs and most mature helper libraries.

Just because I’m compelled to counter statements like this… JSONDB is just a text file too. Everything you can do with text files in /etc/openhab2 you can do with JSONDB including checking them into git, easily transfer them to another OH instance, backup and restore, etc.

There is nothing special about JSONDB. It’s just the same information encoded using JSON instead of the custom Xtext defined format and stored in a different folder.

There are plenty of reasons to not want to use JSONDB (namely it’s not as easy to manually edit) but almost all of the reasons I see for people not using it (backup and restore, checking into source control, etc.) work pretty much the same for JSONDB as they do for text based configs.

  • sudo openhab-cli backup and sudo openhab-cli restore
  • copy all the files in /var/lib/openhab2/jsondb to backup, copy them back to restore
1 Like

Thanks! As usual a clear and great answer.
but now, one step at a time… :wink:

JSONDB
do I need to do something on JSONDB? The docs are a bit unclear on this “JsonDB provides a system database for storage of configuration data.”; does that mean, it is already used ootb? or do I have to configure this in some way - I guess the latter, as there’s much information going on in the docs…

BACKUP
I did a check on this a while ago, I guess I can have a closer look again.

Next-Gen Rules
I know, I can code rules in different languages - and invoke them from the Next-Gen Rules.
Just to get it straight: I write the “Action” in let’s say python and trigger that from the “Rules”-Part in PaperUI?

Everything you create through the REST API (e.g. through PaperUI) gets saved to the JSONDB. You don’t have to do anything. It’s already there and it will be used. Furthermore, it gets automatically backed up for you as you make changes. See the /var/lib/openhab2/jsondb/backup folder. For example, all automatically discovered Things automatically get saved into the JSONDB.

You could do that but I don’t see a reason to. You can just write everything as text and not use the PaperUI JSONDB stored Rules at all. The link JimT provided should get you started with Jython. I also wrote a series when I moved my Rules from Rules DSL to Jython at: Journey to JSR223 Python 1 of 9. You will notice that I don’t use JSONDB stored Rules at all in that thread.

I do plan on moving them all to JSONDB but wont start on that until OH 3 matures a bit more. I’m hoping the new UI is more usable than PaperUI is.

1 Like

Thanks alot! The JSR223 is on my reading list already! :wink:

…but perhaps I’ll postpone my whole refactoring for OH3 and only do like one or two with Next-Gens…

OH3 is still a long way away. You’d be glad to convert now.

What are you referring to?

It would be a real shame if we can no longer use VS Code to edit our rules!

Nothing should change with how your Rules work now. But based on the experience I’ve had with moving to pure JSONDB Rules on HestiaPi the advantages far outweigh the disadvantages, even with PaperUI’s crappy UI for building Rules. They load faster, the “but only if…” clause forces the Rules developer to think differently about their Rules in good ways, and it has support for Rule Templates which I think will become really huge. No more will users be required to copy and paste Rules from the forum or download them from somewhere. Users will be able to just import them and generate an instance of the Rule from the template. And they are much easier for new/non-programmer type users to get up and running with than anything that will require the editing of text based configs.

And I believe that the PaperUI replacement uses embedded VSCode so even though you are in the browser, you would be using VSCode to edit your Rules code.

Note, JSR223 is a legacy term that really isn’t correct anymore. Instead we have “Scripted Automation” (defined through text files in /etc/openhab2/automation) and “JSONDB Rules” (defined through the OH REST API), both of which are run by the Next Gen Rules Engine.

In OH 3, my understanding is this will not change much but that Rules DSL will also be run using the NGRE.

Regardless, when you see “JSR223” think “Scripted Automation”. For now, it’s all running on the NGRE except for Rules DSL. In OH 3 it will all be running on the NGRE as I under stand it.

1 Like