How to migrate from OH3 with Jython to OH4 with jsscripting

Hi,

I seem to have a chicken/egg problem. I can’t upgrade to OH4 keeping my Jython based rules, which is why I decided to migrate to jsscripting.

My approach was: 1/ rewrite rules in OH3 using jsscripting, 2/ upgrade to OH4.

Unfortunately I face the issue, that I can’t easily rewrite my rules with JS in OH3, because I’m currently accessing the config of custom metadata namespaces using Jython, which only seems to be available in jsscripting in OH4…

Only upgrading jsscripting to v4 on an OH3 installation also didn’t seem to work easily due to dependencies.

Does anyone have an idea how to solve this without rewriting everything twice (Jython → Rule DSL → jsscripting)?

Thanks!

Did you try? As far as I know if you hae Jython working in OH 3 it should work as is in OH 4. There really haven’t been any significant changes between those versions that would break Jython.

That too seems like a reasonable approach.

You cannot run add-ons on a different version of OH than what the add-on is built for.

However, you might be able to run the latest version of the helper library on OH 3. The helper library is maintained independently from OH versions. I don’t know if there were any changes to the API that would make the latest library not work with the 3.4 add-on.

If it doesn’t work, you can create your own library and use the raw openHAB metadata API to access the Item metadata configs (see how the helper library does it for examples or look at the Nashorn examples of working with metadata on the forum).

You could install the version of the openhab-js library that corresponds with OH 3.4 (install version 3.2.4) and then update the Item class in the $OH_CONF/automation/js/node-modules/openhab folder with the new functions created in the 4.0 version of the library. Then you won’t have to make any changes when you upgrade to 4, but this might be a good bit of work.

You can use the Remote openHAB add-on and keep your rules running on OH 3 and move everything else to OH 4. Then migrate rules from OH 3 to OH 4 once by one by rewriting them on the OH 4 instance.

Since Rules DSL doesn’t have any access to Item metadata that would be a step backwards.

I’m still running some Jython rules in OH 4.1. Although there may be some issues, it does work for the rules I have.

The openhab-js has a section on compatibility:

All openhab-js versions until 4.7.0 are fully compatible with openHAB 3.1.0 or newer!openhab-js 4.7.1 or newer requires openHAB 4.1.1 or newer for full compatibility!

openHAB 3.4.0 or newer requires at least openhab-js 3.1.0! openHAB 4.0.0(.M2) (or >= SNAPSHOT #3391) or newer requires at least openhab-js 4.2.1! openHAB 4.1.1 or newer requires at least openhab-js 4.7.2 for full compatibility!

1 Like

May be a little off topic but did you have a look at Habapp?
I first, a long time ago, migrated from DSL to Jython. An now few months ago migrated all the Jython to Habapp which is pure Python. The migration was rather simple, though depending on how your rules are written. I had a little exotic usage of rule implementation, but because it is pure Python I was able to write my wrapper classes and lift and shift more or less the code. A big help was ChatGPT too.
Finally, I can debug the code inside Visual Studio Code which probably is not so easy if running the rules inside JSR.

You can do Jython in 4.1. I just converted all my rules to JS. I created a custom Chat GPT using OpenHAB documentation. Chat wrote most of the rules and I converted the code in very little time even though I have never written a drop of JS code. It’s amazing how Chat made it incredibly easy.

How is the overall quality of the code? Do you understand how the code works? I do know the generic free ChatGPT and Bard produce truly awful rules code. It is encouraging that it can be primed to produce workable code.

The addition of being able to customize your own bot has been very helpful. It can be hard to get chat to forget about old OpenHAB information. To give you an example, I converted my Jython module to manage occupancy to JS in several weeks without a clue about JS, node, NPM, etc. See occupancymanager - npm for several thousands of lines of mostly Chat generated code. Also, CoPilot in VS can be very helpful. Yes, its not perfect, it makes mistakes, uses information from older versions of OpenHAB but overall, it made converting all of my scripts over significantly less painless.

1 Like