Extracting Rule bodies from OH3 Backup files

Hi there,
Just a quick question that I haven’t found a good answer to despite a few hours forum search.
I’m running OH3 and do semi-regular openhab-cli backups making the xxx.zip files.
All my 40+ rule bodies must certainly be part of these backup files, and my question is simply:
Is there a way to extract a particular rule body from an older backup file?
Reason is, I need to gain access to an earlier version of a complex rule.
Thanks
Bjorn

Yes. Managed rules are stored in $OH_USERDATA/jsondb/automation_rules.json.

Note, there are limitations with JSON which means your code is going to be a bit ugly in the raw JSON. You might need to do some formatting of it to make it human readable or, my recommendation, insert the rule temporarily using the REST API and you’ll be able to see everything in MainUI.

I’m not entirely certain what you mean by “body” and am assuming you mean the Script Actions.

Here’s an example of what it’ll look like:

  "alarm_script": {
    "class": "org.openhab.core.automation.dto.RuleDTO",
    "value": {
      "triggers": [
        {
          "id": "2",
          "configuration": {
            "channelUID": "mqtt:topic:broker:sleepasandroid:event",
            "event": "alarm_alert_start",
            "thingUID": "mqtt:topic:broker:sleepasandroid"
          },
          "type": "core.ChannelEventTrigger"
        }
      ],
      "conditions": [
        {
          "inputs": {},
          "id": "3",
          "configuration": {
            "script": "var curr \u003d items.TimeOfDay.state;\n\ncurr \u003d\u003d \u0027NIGHT\u0027 || curr \u003d\u003d \u0027BED\u0027",
            "type": "application/javascript"
          },
          "type": "script.ScriptCondition"
        }
      ],
      "actions": [
        {
          "inputs": {},
          "id": "1",
          "configuration": {
            "script": "var { Deferred } \u003d require(\u0027openhab_rules_tools\u0027);\n\nvar hour \u003d time.toZDT().hour();\nvar currToD \u003d items.TimeOfDay.state;\n\nconsole.debug(\u0027Current ToD \u003d {} Current Hour \u003d {}\u0027, currToD, hour);\n\n// Puppy lights\nif(currToD \u003d\u003d \u0027NIGHT\u0027 || time.toZDT().isBefore(time.toZDT(\u002704:45\u0027))) {\n  console.info(\"The alarm went off and it\u0027s night time, turning on the lights for ten minutes\");\n  items.TOD_Lights_ON_MORNING.sendCommand(\u0027ON\u0027);\n  Deferred().defer(\u0027TOD_Lights_ON_MORNING\u0027, \u0027OFF\u0027, \u0027PT5M\u0027, true);\n}\n// MORNING TIME\nelse if(currToD \u003d\u003d \u0027BED\u0027){\n  console.info(\u0027Good morning!\u0027);\n  items.TimeOfDay.sendCommand(\u0027MORNING\u0027);\n}\n// Should never reach this becuase of the rule template\nelse {\n  console.warn(\"The alarm went off but it\u0027s not BED or NIGHT\")\n}",
            "type": "application/javascript;version\u003dECMAScript-2021"
          },
          "type": "script.ScriptAction"
        }
      ],
      "configuration": {},
      "configDescriptions": [],
      "uid": "alarm_script",
      "name": "Alarm Clock Execution",
      "tags": [
        "Schedule",
        "test"
      ],
      "visibility": "VISIBLE",
      "description": "Called when the alarm clock goes off"
    }
  }

You’ll find the “code” under “actions” and “conditions”.

What you will notice is that certain characters are replaced with the unicode encoding (e.g. = converts to \u003d) and newlines are replaced with \n.

Rich, you’re good! I found my old rule. Thanks a million!!

A more general question on this if I may:
I started my OH journey with OH2 files only, MS Code, WinSCP and an RPi.
So, off-line file build, edit, access, save, archiving and subsequent transfer to prod unit. (Very easy and transparent off-line file and system management!).

On OH3 I find this not equally easy. The only historical transparency option seems to be “openhab-cli backup” unless I go the files only route and accept the file locking, which may not be all that bad.

But I’m just a user now, so how do you professionals go about to make it easy for yourself when you need to dive into past?
Just curious.

Thanks Bjorn

I’m doing it as you described. I use file based configuration and have all files in a versioning system.
That way I can go back and forth in time. Most of the time I remember e.g. in which file the change was because they are named accordingly. That way I find the change (most of the time) very quickly.

Hi Sebastian,
Interesting!!

I have been playing with OH3 for almost a year now and I still find myself missing the OH2 files only concept.

I guess I have a rather small system with 150+ Items and 40+ Rules, but there are times when I feel that the OH3 GUI gets too fragmented (40 rules vs 1 rule file), and searching and updating gets less efficient when two or more rules are communicating via Items.

Just my thoughts, and yes, I’m considering returning to files only.
But, I wonder what others feel and do ?
Bjorn

I check all my configs into git. I run GitLab at home but there are lighter weight options like Gitea or Gogs or you can use a publicly hosted service like Github (most of these support private repos). But you can even just set git up without a nice web front end.

Everything configured through MainUI gets reflected in the files under userdata/jsondb (almost everything), userdata/etc (logging configs mainly), and jsondb/config (which addons are installed, regional settings, etc).

I try to commit (sometimes fail) any changes I’ve made with reasonable messages so it’s easy to back track and compare between versions.

I can compare changes between versions usually pretty easily as well.

Obviously it’s not perfect. Because JSON doesn’t support newlines the whole script gets merged into one line so you have to do a little bit of mental parsing to see the differences, but the actual changed bits are highlighted so it’s not hard by any means.

Usually people want to separate their rules into multiple files.

Definitely explore the developer sidebar. It has a search and pin feature so that you can collect all the Things, Items, Widgets, and Rules you are working on in one list and easily navigate between them and manipulate them. You can also search for Items with a given Item metadata and I think I saw a PR recently to find which Things and Links use a given transformation.

If those rules mention those Items by name, searching for the Item Name will give you everywhere that Item is used. It does a plain text search inside the rule.

For example, I was working on integrating Virtual Solar Light Sensor [4.0.0.0;4.9.9.9] into my system. So I pinned all the relevant Items, Things, and Rules:

As you can see, I can navigate to any of these by clicking the pencil icon. Those Items that are configured to be editable I can click the > to bring up the widget to edit it (e.g. toggle a Switch, great for testing). I can enable/disable Rules and Things and manually run rules, all from that sidebar.

image

With the sidebar I’m able to be more productive than I ever was with text files because it’s all right there. (Note, that’s just one of the tabs in the sidebar, the other three are just as useful).

Rich,
Very enlightening as usual. Will give it a try.
Thanks again.
Take care.
Bjorn