Is my javascript install working as expected?

Hello, I am new in the forum, as of course I am asking for help in order to get my openhab working properly.

I have latest stable version (3.1) installed in a QNAP NAS using docker image from openhab repository in docker.

I have installed it and it seems to wokd properly. I can configure things, items and create and modify pages and rules.

I am trying to create some rules to take control of my heating system.

But I am quite confused with all the options about scripting.

I know a bit of javascript, son I use javascript as scripting engine when creating rules with actions based in script (from th UI interface)

But there seems to be two kind of javascript engines, the ECMASCRIPT (JSR223 in the documentation?) in the rules scipting and another javascript you have to install in settings/automation (I have tried to install it).

The problem is that the examples that are explained in documentation about rules, they don’t work.

For example it does not seem to have a “items” variable.

when I try to use “items.someItemName.sendCommand(‘command’)” it does not work, or when I try “var somvar = items.someItemName” to get the state, it dos not work, it seems items is not a known vaiable (but in the documentation says things and items are variables with the corresponding collections).

The only way it seems to work is using “events.sendcommand(‘name’,‘command’)” But I need to access other properties of things and items.

Rules copied from the in javascript don’t work, either.

What am I doing wrong?

Thank you.

Yes, you’ve got it.


Thanks a lot.

So, if I understood it well from the provided link, I should uninstall the automation javascript addon (JSScripting 3.1)?
Would it be just uninstall and reboot?

If I understand well openHAB 3.1 has an integrated javacript compliant with standard ECMASCRIPT 5.1.
The problem is that I have installed it after thinking the default was not working correctly.

There seems that some of the default variables that should be accesible from a rule are not there.

I have tried to get items from ItemProvider with no luck.

UPDATE: I uninstalled the automation/javascript addon. I have rebooted and it seemed the problem persisted.
I have deleted the docker container and recreated it. But so it is now as default install (I conserve the addons, userdata and config directories).
But it seems that I have no access to things variable yet.

I think I am missing something, may be the documentation about rules in openHAB web is outdated, or it is for using other javascript engine.

I am using the web UI to create the rules, so it creates ECMASCRIPT scripts by default.

Is the JSR223 yet another javascript environment?
There seems to be 3 JSVMs: the integrated ECMASCRIPT, JSR223 and the GraslVM you talked about (that seem to be the one you install using the settings/automation/javascript).
Are the examples in rules and scripts section based in ECMASCRIPT or JSR223?

Where can I find additional examples?

I find the documentation quite confusing about this, as examples spread on it don’t tell you which JS VM do they use, and there is not a clear explanation about differences between them (or I could not find it).

With the exception of one or two threads on the forum, all the examples you will find will be Nashorn JS. This is ECMAScript 5.1 and it is the default that comes with openHAB.

Furthermore, almost all of the examples you find will be using the Helper Libraries.

JSScripting is a GraalVM implementation of the latest version of JavaScript. It also has a number of differences from Nashorn in that nothing really is injected into the script by default. This is a deliberate choice to avoid variable conflicts. So for the most part, Nashorn JS code is not compatible with JSScripting code. (see below for how to make a Nashorn script work with JSScripting).

So you either need to not install the JSScripting add-on or you need to modify the examples you see so that they will work with JSScripting.

That first attempt would never work under any circumstances. You should not have ever seen an example like that. For one items.someItemName gives you only that Item’s current State. You can’t send a command to a State. Furthermore, it’s only in Rules DSL (and with the help of libraries) that one can sendCommand on an Item. Item doesn’t actually have that method.

In Nashorn to send a command to an Item you would use:

events.sendCommand("ItemName", "command");

Notice both the Item and the command are Strings.

In Nashorn the second one will work. In JSScripting it will not because items doesn’t exist until you import it.

You’d need to post how you tried to do that. In Nashorn even without the Helper Libraries you can get an Item using ir.getItem("ItemName"). Definitely look at the docs at the github repo I posted above and pay attention to the “But how do I…” section. But remember, Item doesn’t really have a sendCommand so even once you get the Item from the ItemRegistry, you can’t call sendCommand on it, you have to use events.

JSR223 is the name of the feature way back when that allowed the running of different languages like Jython and JavaScript embedded in Java Programs. It has since been replaced with a Java Scripting API but unfortunately we’ve not updated to use the new name here so we still call it JSR223.

Nashorn and Jython and Groovy follow everything you will see documented at JSR223 Scripting | openHAB.

JSScripting uses a different mechanism called GraalVM to integrate with openHAB. And it does things differently. There is a lot of work on it’s helper library going on right now and that helper library will be installed with the add-on by default making it much easier to use. It also implements the latest JavaScript versions.

It is possible to make a Nashorn script compatible with JSScripting. At the top of the script you’ll need to add:

var runtime = (typeof(require) === "function") ? require("@runtime") : {
  itemRegistry: itemRegistry,
  events: events,
  items: items,
  DateTimeType: DateTimeType

You need to add each of the variables that your script uses from this table to that list.

Then everywhere in your script where you reference one of these variables you need to use runtime. For example runtime.items.

That will make the script compatible with both Nashorn and JSScripting.

However, if you want to just start with JSScripting you can either wait a couple weeks or so for some of the current work being done on it to be completed which will include better docs and an included Helper Library. Or you can get started now knowing you’ll need to make some minor changes in a couple weeks.

To get started now:

  1. Install the JSScripting add-on
  2. Following the instructions here to install the helper library using npm.

The docs for how to create rules (which you can ignore for the most part if you are using the UI) as well as how to access stuff like Items and Things are documented there as well. The add-on has (or will have soon) an option that will enable it to inject all the helper library stuff into your Script for you so you don’t need to import it manually every time.

Thanks a lot. You have helped me a lot.

I was mixing examples in my mind (I had even realized some of the demo scripts were in DSL scripts, it seems more easy to interact with items, but I don’t want to learn yet another language, I have enough with learning javascript, I allways have been a C and C# programmer, but that was a time ago).

Will read all this carefully and se if I can make some basic rules work.
Till now I only could use events.sendCommand(…).

For now I will keep with ECMASCRIPT/JSR223 and the default installation.

Once I get a bit more confident, I will try JSScripting as it seem the direction for future realeases.
Meahwhile the ongoing work will be finished and may be it will be easier to integrate with rules.