Migration rules DSL to ECMA Scripting - please help for jumpstarting

OpenHAB3

As I decided to setup my new OpenHAB3 system from scratch instead of migrating my OpenHAB2 stuff I also want to migrate my DSL rules to ECMA Script.

I used to code a bit in Javascript, but it was quite some time ago and I’ve to admit that I still have a lot to learn. That makes it pretty hard for me to find out what’s possible in Javascript and where to look for the available functions in OpenHAB.

May be this is a silly question, but for example I don’t find anything about how to get the status of a thing in ECMAscript as I have no clue where to look. May be it’s in front of me, but I don’t see it.

So what I’m looking for is kind of a reference where to find the methods and data structures available. It would be very nice if someone could give me just some hints where to look for what to understand what’s available

Example for sending a Telegram message:
In rules DSL it would be something like this

val telegramAction = getActions(“telegram”,“telegram:telegramBot:BOT”)
telegramAction.sendTelegram(“MSG”)

and in ECMA it would be something like this

var telegramAction = actions.get("telegram","telegram:telegramBot:telegram");
telegramAction.sendTelegram("Message");

If not someone had told me in this thread, I never would have had the idea how to do it in ECMA Script.

Any help would gladly be appreciated and please forgive me if I just don’t see it.

Thanks a lot

For now, the Helper Libraries doc are your best source for information. I have a series of posts you can find by searching “getting started with ngre” but those are old and there might be some differences with what’s current. Also, looking at the Helper Library code can be helpful.

He @dolittle , agreed that more examples would be helpful.
Have a look here as this list might be helpful to identify the different modules to load.
Also this one as a start and this page helped me to get started.

Thanks very much for your replies. It’s good to see that I’m not too far off track.
@rlkoshak Thank you very much.
@chrismast The Javadoc was an important one.

I hope a lot of community members share their ECMA rules to help the ones looking for information.

Could you get that running? Even with the pages I’m a bit stuck

@bjoernbrings Yes. Actually it’s pretty simple like most things as soon as you know how to do it :wink:

This is my rule code

var logger    = Java.type('org.slf4j.LoggerFactory').getLogger('org.openhab.rule.' + ctx.ruleUID);
var ThingUID  = Java.type("org.openhab.core.thing.ThingUID");

var telegramAction = actions.get("telegram","telegram:telegramBot:Telegram_Bot_Default");
var triggeringThing = things.get(new ThingUID(event.thingUID)).getLabel();

telegramAction.sendTelegram(triggeringThing +': <strong>' + event.statusInfo + '</strong>');
logger.info(triggeringThing + " änderte den Status zu " + event.statusInfo );

It’s triggered when the thing changes to “ONLINE” or “OFFLINE”. The new state is in event.statusInfo. You can see the available object on this page. It doesn’t seem to be accurate for OpenHAB 3 beacause I wasn’t able to use any of the “Scripted Automation” Objects that have no Rules DSL equivalent.

E.g. event.item didn’t work for me instead I had to use itemRegistry.getItem(event.itemName).

Hope that helps

1 Like

That worked. Thanks a lot :slight_smile: