Rule actions in OH2 javascript. Where can I find informations about the API (items, busevents, timer, log) available inside the script?

I spend several hours to follow up what’s going on with rules and scripting in OH2. I had some success and got a simple rule running: Switch on a light by item name from javascript.

BUT is extremely hard for me to find and understand the information pieces.

Maybe it’s a good time to start some documentation on scripting, even if something are not final today. For me (and maybe for some of others too) scripting is one of most imported features of OH.

My personal biggest issues is find some information for the current api / class binding I can use in a script. Even going through the source code doesn’t help me, because I obviously didn’t grab the file that implements this binding. Maybe someone could give me some direction where to look, or even some documentation :wink:

There is a second import point: I should to be able to “include” some simple js-libraries. Even for this topic I can’t get the essence of the discussions.

Would be great to post some more success here ….

2 Likes

The Rules engine is largely unchanged from OH 1 so you should be able to use the OH1 wiki and rules examples in OH 2.

Beyond that documenting all aspects of OH 2 is in progress.

You should not that what you are looking at is the new rule engine that is currently under heavy development at ESH and only included in OH2 as an experimental feature.

The current documentation is at
https://www.eclipse.org/smarthome/documentation/features/rules.html
and
https://www.eclipse.org/smarthome/documentation/development/rules/sample.extension.java.html

You are very welcome in joining the development by implementing missing features, fixing bugs or enhancing the documentation!

Would like to know if the javascript example posted on the wiki here: openHab JSR223 script engine script wiki is still valid/working for OH2-beta3 ?
I’m trying to find some good javascript rule examples to build on, and not having much luck. I followed @lewie instructions from this post: port-jsr223-bundle-to-openhab-2 to get the engine installed into OH2-beta3; setup a rule definition in the paper-ui; and tried to add my javascript file in the …/conf/scripts directory; but I just get this error when I stimulate the item to change state:

[ERROR] [internal.handler.ScriptActionHandler] - Script execution failed: sun.org.mozilla.javascript.internal.EvaluatorException: illegal character (#1) in at line number 1

Could someone please provide either a link or a nice javascript example of something relatively simple to get started with?

Have been playing around some more with this. But not really getting anywhere.
Do you have to configure the rule thru the paperUI in order to get the javascript rule to execute?
If so, what is the linkage between the 2 ?

I tried also to use/test scripts.
I found actually no compatibility to oh1 at all.

I use an item file with a Switch item “item1” and sone others
Here my current findings…
I use this action script inside of a timer rule:

print (“Timer >>> START”);

print (“things:”);
var thxs = things.getAll();
print(thxs);

for each (var thx in thxs) {

// enumerating properties don't work: for each (var el in thx) print(el);

print ("Label=" + thx.getLabel());

print ("Channels");
for(var channel in thx.getChannels()) {
  print (channel);
}

}

print (“Items”);
print (items);

try {
for each (var item in items.keySet()) {
print (item);
}
} catch (ex) { print (ex.message); }

try {
print (“try to get ‘item1’:”);
// don’t work!!!
// gets the state of the item, but not the item itself
//
var item = items.get(“item1”);
print (item);
} catch (ex) { print (ex.message); }

// don’t work …
try {
print (“Send command to item1 - A”);
events.sendCommand (“item1”, “ON”);
} catch (ex) { print (ex.message); }

// don’t work …
try {
print (“Send command to item1 - B”);
events.sendCommand (“item1”, “OPEN”);
} catch (ex) { print (ex.message); }

// don’t work …
try {
print (“Send command to item1 - C”);
events.sendCommand (“item1”, OPEN);
} catch (ex) { print (ex.message); }

// print (“rules:”);
// print(rules);

// don’t work
//print (“tigger:”);
//print(trigger); //.event);

// ok
print (ON);
print (OFF);

print (“Timer >>> END”);

====================
I found the defininition of the globals for the script like “items” “ON” “OFF” etc in: DefaultScriptScopeProvider.java

The members of an item in: ItemRegistryDelegate.java

The members of a thing in: ThingImpl.java

“Events.sendCommand etc” in: ScriptBusEvent.java

So far rules/scripts seem to work only very basic.

Any additional infos or tips how to evaluate the possibilities of scripts are welcome.
For my oh1 mini scene/sequence manager I personaly need most:

** Fully Access to the itemsRegistry (enumerate items with pattern etc).
** Creation of Timers with callbacks
** send/postCommand