Context for JS actions in the OH3 Rules UI

Just wondering, if you write a rule in the built-in editor in OH3 using ECMAScript, what kind of context do you get access to? For example, would it be possible to write a library of classes for utility functionality (outside of the UI, stored somewhere in a well-known location) and refer to those from script actions?

There are JS helper libraries located here…

So this means I can add my own code under automation/lib/javascript/core and that will be available to any rule, correct?

Correct, but that directory is just my suggested location for storing libraries to keep installations consistent… libraries can go anywhere. Well… don’t put them under /automation/jsr223/ or they will be run as scripts.

How does openHAB know where to pick the libraries from then?

You need to specify the libraries you want to load in your scripts.

Actually, this directory is for the core helper libraries. Your personal libraries should go into automation/lib/javascript/personal/.

You need to specify the libraries you want to load in your scripts.

As a path under the root I suppose, using import? I’ll look at the examples :slight_smile:.

Here is an example of accessing the actions.js library…

var OPENHAB_CONF = Java.type('java.lang.System').getenv('OPENHAB_CONF');
load(OPENHAB_CONF + '/automation/lib/javascript/core/actions.js');

Many more examples in the helper library documentation.

1 Like