Recreate OH2 rules in javascript (JSR223) in OH3

Hi All,

I’m loving the new interface and semantic model in OH3.

I’m building a parallel OH3 server to replace my running OH2 server and have got most of the items (all bar z-wave) into the new OH3 and am now working on porting across the old rules and moving them into JS.

I understand the docs aren’t there yet and that’s fine, but I’ve got a couple of basic questions.

  1. Now that we have an official release of OH3, are the helper core libraries still needed. I have seen examples that use the helper core libraries, but have also seen examples that reference for example - org.openhab.core.model.script.actions.Voice. I’m unclear as to how I should proceed.

  2. I can’t find any examples of references for how to use the OH Cloud connector in JS. I have rules that used the old sendBroadcastNotification(“Alert”) and can’t see a way to get this working in the new model. Anyone got any helpful pointers?

In the spirit of giving as well as taking, whilst it might not be useful to many, I have completed the move of the functionality that scans through a group of Echo Last commands, finds the first echo showing a specific command and plays a message to it. This is useful when you want to trigger a specific OH script through Alexa and play the response to the script through the same Alexa. It obviously has issues though if more than one Alexa has the same last voice command. Our Echo’s get enough use that in the real world this hasn’t been a major issue. I’m sure it’s not perfect, but may point people in the right direction :grinning:

var EchoMembers = Java.from(itemRegistry.getItem(itemGroupName).getAllMembers());

for(var i in EchoMembers)
 {
//  logger.info("1 Echo Last Command " + EchoMembers[i].getName());
  if(EchoMembers[i].getState().toString() == "who is home")
   {
//  logger.info("2 Echo Last Command " + EchoMembers[i].getName());
    var EchoLastVoiceCommand = EchoMembers[i].getName();
    var EchoName = EchoLastVoiceCommand.substring(0, EchoLastVoiceCommand.length - 16);
    events.sendCommand(EchoName + "SpeakVolume", 50)
    events.sendCommand(EchoName + "Speak", TTSAnnounce)
//  logger.info("3 Echo TTS " + EchoName);
    EchoFound = True
    break;
   }
 } 

Thanks

Whilst I haven’t tested it yet as I’m not yet linked to the Cloud service, I eventually found an example of how to do this and am documenting it for anyone following.

Assuming the answer to Q1 is yes you should still use the core helper, then the following code proports to trigger cloud notifications.

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

NotificationAction.sendNotification("someone@example.com", "This is the message");
NotificationAction.sendBroadcastNotification("This is the message");
NotificationAction.sendLogNotification("This is the message");

Now that we have an official release of OH3, are the helper core libraries still needed.

Sharing my own experience, I am not using the helper libraries. The javascript helper libraries aren’t getting the same attention as their python counterparts. I have adopted a couple of helpers from @rlkoshak [which you can find here] for migrating all my old DSL rules built on his design pattern work.

That’s not going to help you with cloud connection, though. Can’t hurt to give those old helpers a try, referring to the python versions as needed, and see if it gets you the results you’re looking for.

1 Like

Thanks Tim!

I’m pretty agnostic (with a slight preference for Javascript), but in your opinion if the Python is getting more attention, might it make more sense to go with Python to try and stay as mainstream as possible?

“Mainstream” is a personal value judgement. For me, mainstream means native support, fewer addons is better. At the same time, there’s fever for Python in this community. I suppose that mainstream for them means Python. Different strokes!