Which JavaScript?

My question is more about clarification of names…

I’m planning to migrate from 3.4 to 4.x
Before I do, I want convert my DSL rules to JavaScript.

There is talk about ECMA, ECMAscript 2021 and 2022, and JavaScript on the forum, which has me confused.

  1. What do I need to enable to run the ‘proper’ JavaScript version?
    Proper should mean the latest and most supported (and ideally future-proof) version.

  2. The other question is: where to put the rules? Use the UI or put them in files? I like the latter being able to use versioning and VScode.

  3. There was also the hint of using Blockly, which seems to generate JavaScript?!

My personal constraint is, I am an old-fart structural type of programmer or rather hack, that is currently learning OOP, more so in C++, and is prepared to learn one more language for the continued and expanded use of OH. Since OH seems to be a Java-based software, at least to me, it appears the closest (as in most compatible) language is JavaScript.
I never liked the rule-DSL, hence, my wish to migrate away from it.

As usual, any hints appreciated.

[added far later, after post #9]
This (Confused on what kind of rules / rules engine I should be using) also captures some of my frustrations (for a better word) and also contains (as usual) great response from Rich.

There are only two flavours of JavaScript in openhab:

  • Nashorn JS: old, don’t use
  • Modern JS: this is provided by the addon called jsscripting. This was also called ECMA 2021 but then they had a newer version so now ECMA 2022, and so on. In any case it’s maintained through the jsscripting automation addon, and not the nashorn.

IMO, it’s great to continue to exercise the mind, especially as we age. The moment we stopped learning, our brain starts deteriorating!

I’d like to throw in my favourite scripting language, especially for openhab, and here’s some comparisons:

2 Likes

I suspect for JavaScript it’s the yellow JS, while JRuby has two options (in my 3.4 install).

On a side note: Ruby seems simpler when following your “Rule conversion” link — interesting. (Can’t say much more, as I have no experience in either).


The JavaScript Scripting states:
This add-on provides support for JavaScript (ECMAScript 2022+) that can be used as a scripting language within automation rules. It is based on GraalJS from the GraalVM project.

Also included is openhab-js, a fairly high-level ES6 library to support automation in openHAB. It provides convenient access to common openHAB functionality within rules including items, things, actions, logging and more.

If I go JS, which of the two to use (to satisfy my longevity / support requirement)?

Anyway, I should read the documentation that popped up when hitting “read more” :slight_smile:

For JRuby, long story short: you should install the official one that says “openHAB [blue tick]”

The other one is from the marketplace, which comes with jruby 9.4, at the time it was decided to keep openHAB 3.x with jruby 9.3 in the official distro.

openHAB 4.x distro comes with jruby 9.4 by default.

This doesn’t affect your script unless you plan to use new Ruby 3.1 syntax. The helper library is compatible with both.

Since you’re going to upgrade to openhab 4 anyway, it doesn’t matter, and it would be simpler if you just chose the official addon (the one with the blue tick) because then it would be upgraded for you when you upgraded to oh4.

Which two are you referring to?

The more you go into jruby (and its openhab helper library) the more you’ll find its wonderfully “simple” syntax that does a lot for you. It includes a lot of boilerplate code so you don’t have to (re)write them again and again. Things that you’d have to struggle to write in other languages (dsl, js) are much simpler and more elegant in jruby, and consequently also much easier to read, write, understand, and maintain.

Thank you :slight_smile:

It says included are both: GraalVM and openhab-js…

I am sure I figure it out, once digging into the docs…
I am ATM researching the differences between both…
This may help others… https://www.developers.dev/tech-talk/which-is-better-ruby-on-rails-or-javascript.html

Slightly related, I wan to make use of the UI for display purposes, e.g. replace the sitemap… this uses the F7 framework – I also need to look at – however, is JS or JRuby related to the F7 framework? Or in other words, is either one better related or beneficial for F7…

Again, I am trying to figure out how to best tackle the current and future OH (by using the most efficient [ideally related] tools or frameworks).


[added later]
My website is written in PHP (which I did 10+ years ago); the latter can be replaced with jRuby… (=bonus) :slight_smile:

GraalVM is the VM, and openhab-js is the helper library. They are the components that make up the jsscripting addon.

You don’t need to worry about these details though. Just install the jsscripting addon, and start writing your rules.

This is not a relevant comparison. They are comparing Ruby on Rails (RoR) vs JavaScript, which is actually like comparing apples and oranges. In any case, neither is relevant to your openhab scripting, because RoR is not Ruby.

Ruby is a language
RoR is a framework for building web applications, implemented in Ruby programming language.
JavaScript is a language.

RoR to Ruby is like Django is to Python, or Laravel is to PHP.

So a better comparison is Ruby vs JavaScript, not RoR vs JavaScript.

Using JRuby for openhab has absolutely nothing to do with RoR.

1 Like

Thank you very much! This is really helpful for a noob in this area; much appreciated.

I am now researching ‘closer to home’, that is the OH forum… exciting times ahead :slight_smile:

Why not try both jsscripting and jruby.

They can coexist. You can write 50 rules in jsscripting and 50 other rules in jruby and 50 other rules in rulesdsl. Then you’d be in a better position to truly understand the ins and outs of each and make a more informed decision.

1 Like

Yep :slight_smile: what I figured and may well do…

Rather than go full throttle into one language, do some in JS and some in jRuby and see what happens, as in what makes the most sense to me…

Don’t hesitate to post any questions here if you get stuck!

Here are some useful links for jruby

Also you might find (or may not realise) that your old code could be written significantly simpler in jruby, so don’t hesitate to post your rule and seek feedback. I’m saying this because you could still write the old tedious code even in jruby without realising that there’s a much easier way.

1 Like

Both js and jruby automation addons don’t currently deal with the MainUI, yet.

JRuby however, allows you to generate your sitemaps from code. See examples here:

When dealing with Main UI with F7, you’ll can use a simplified version of JavaScript, again, although they’re the same language it’s not the same as what you’d write in the jsscripting automation for rules.

1 Like

I just wanted to kind of summarize some of what’s been already said here just in case someone in a hurry misses something.

JavaScript and ECMAScript are the same thing.

OH supports ECMAScript 5.1 through the Nasorn JS Scripting add-on. Do not use this, it’s there to support those who got started with Nashorn JS a long time ago and can’t or won’t move to the newer add-on. Nashorn JS does not come with a helper library (though there is an incomplete third party one on GitHub) meaning that all your interactions with OH will use the raw Java JSR223 APIs. This means writing rules will be tedious, long, and have to deal with Java stuff a whole lot.

OH supports ECMAScript 2022+ through the GraalVM JS Scripting add-on. This is the most recent version of the language (I think 5.1 was release something like 8 years ago but could be even older, I’ve not looked it up). The GraalVM JS Scripting add-on comes with a helper library called “openhab-js”. This library abstracts the raw Java JSR223 API to openHAB in rules to provide a nice and clean and pure JS API for interacting with openHAB.

jRuby similarly comes with a helper library to provide a nice Ruby interface with openHAB.

I want to reemphasize @jimtng’s answer here. MainUI does use JavaScript, but except for some of the very basics, there is little overlap between the sorts of JS you’d be doing in MainUI and the JS you’d be doing in a rule. For one thing, there’s no openhab-js. For another, it’s a completely different environment (rules run on the server, MainUI runs on the browser). So while there is a tiny bit of overlap, there is really no compelling reason to chose JS over jRuby just because you will encounter a few lines of JS in MainUI.

And I want to emphasize that last bit even more too. Mostly what you’ll be doing in MainUI is configuring widgets. You are not writing JS code except a limited set of well documented expressions that, for example, lets you set the color and icon of a widget based on the states of Items. You will only be using one-liners here, not writing blocks of JS code. openhab-js is not available there.

One nice thing that might help with learning JS is that Blockly “compiles” to JavaScript. So you could build up some code using Blockly and look at the resultant JS code it generated to learn how to do things in JS. Often it can be easier to plug the blocks together or see the options available on a block and look at the code that it is to review the reference docs.

Beyond that, both JS Scripting and jRuby each have a good helper library that simplifies the interaction with OH and presents a language pure interface (e.g. when you access the members of a Group Item, you get a language native list/array that can be treated like any other list/array you’d encounter in the language; without the helper library you’d end up with a Java List Object and would have to iterate, search, etc. using Java techniques and methods, not JavaScript or jRuby standard methods). And you need do nothing special to get the library; they come with the add-ons.

2 Likes

I think nearly everything has been already said here, thanks Jim and Rich!

I just want to add that Main UI provides, in addition to Blockly, another help for writing JavaScript scripts and rules: the integrated code editor has full syntax highlighting and autocompletion for the openhab-js library!

2 Likes