How to migrate older ECMAScript 5.1 code to ECMAScript 2021+

I have migrated from OpenHAB 3 to OpenHAB 4. To make sure that my scripts and rules still work, I installed the “JavaScript Scripting (Nashorn)”. It says: "This add-on allows you to use your older ECMAScript 5.1 code on newer Java versions until the code is migrated to ECMAScript 2021+. "

My question is: Are there any best practices for migrating code from ECMAScript 5.1 to ECMAScript 2021+? Are there any up-to-date and recommended tutorials on this? Or maybe there are even automated ways?

This should get you going:

Check the “Changes in JavaScript Automation” section.

Thanks, but here I only found something about Blockly. I use code only and not Blockly.

This has fallen a little out of date, but the basics are here:

Other than that you should look through the docs for the JSscripting helper library:

https://openhab.github.io/openhab-js/index.html

1 Like

The way you write rules in JS Scripting is a lot different from Nashorn because the Helper Library that comes with JS Scripting is really good.

The link @JustinG provided is kind of a “make the fewest changes to my Nashorn scripts so they work in JS Scripting”. That is a valid approach.

However, you will find you will have dozens and dozens of lines of code that can be replaced with one call to a function in the helper library. Because of that and other significant changes in how the Helper Library works, you will likely want to rewrite from scratch those rules eventually to take advantage of the helper library.

And what instructions would you recommend for this? The official instructions here: JavaScript Scripting - Automation | openHAB
Or is there a more detailed guide to writing ECMAScript 2021+?

ECMAScript is JavaScript. There are tons of tutorials on the web including those hosted by MSN, Mozilla, W3Schools and others.

If you want basics of the language just as “how do I X in JavaScript” and you’ll get lots of hits.

There are also lots of free online courses in JavaScript, not to mention paid ones.

For actually using JavaScript to interact with openHAB, which is the bulk of what you’re going to do in a rule, see the add-on’s readme which is very complete and comprehensive with lots of examples.