JavaScript rules do not work

  • Platform information:
    • Hardware: RPi 4
    • OS: Openhabian
    • Java Runtime Environment: 21
    • openHAB version: 5.1.0
  • Issue of the topic: JavaScript rules do not work

Hi Experts,

I tried to familiarize myself with the javascript rules but already having difficulties with the first steps.

The problem is that these rules seems not to be triggered. I see a log entry that rule is enqued after copying into Automation/js directory (or saving saving changes to the file) but nothing happens after this.

See the following really basic example:

import { rules, triggers } = require('openhab');

rules.JSRule({
    name: "My_test_rule1",
    description: "Test rule",
        triggers: [triggers.GenericCronTrigger("0/10 * * * * ? *")],
    execute: function(event) {
        var logger = log("My_test_rule1");
        logger.info("Hello World!");
    } 
}

I would expect ā€œHello World!ā€ logs to appear every 10seconds in the log but it is not the case. No related log, no related warning, no related error.

The basic openhab javascript scripting automation is installed:

Could you give hints what am I doing wrong?

Thanks!

BR,

KrisztiƔn

Use console logging (e.g. console.log or console.info) instead. When manually creating a logger, you also have to ensure that its logger name is handled and it’s log output is written to the openHAB log file. When using console logging, all of this is handled for you.

btw: Blockly is a great tool to get started with JavaScript: You can always check the generated code (button bottom-right) and get familiar with it.

1 Like

I wonder if the logging section of the JS Scripting readme should same more about that. As it currently reads, it doesn’t mention this potential problem and makes it seem creating a logger and useing console is equivalent.

I’m still working on composing something for the file locations. Hopefully I’ll remember to come back to this. But if someone gets to it first that would be great too.

Yeah, I’ve also thought about how to improve this situation. Actually, I think documenting the log namespace in the README might not be a great idea, because I donā€˜t think there is any real reason to use log over console logging. Console logging also allows adjusting the logger name. The log namespace is mainly used by openhab-js internally, openhab-js has a different use case for logging though than most scripts. The log namespace will still be documented in JSDoc, but not listing it in the README will create less confusion.

2 Likes

Thanks for the replies!

I tried also with console logging with same result. So, I am not sure whether if it is really something to do with the logging.

I have copied and slightly adjusted one of the example rules in the documentation to see the effect but still nothing:

const email = "my_real_email_address"

rules.JSRule({
  name: "Test rule2",
  description: "Test rule2",
  triggers: [triggers.GenericCronTrigger("0/10 * * * * ?")],
  execute: (event) => {
    var logger = log("My_test_rule1");
        logger.info("Hello World!");
        console.info("Hello!!!");
        console.log("Hello!!!");
    actions.NotificationAction.sendNotification(email, "Test message");
  },
  tags: ["Balcony", "Lights"],
  id: "BalconyLightsOn",
  overwrite: false, // defaults to false: whether to overwrite an existing rule with the same UID
  dedicatedContext: false // defaults to false: whether to run the rule in a separate dedicated context
});

It says that script is enqued (in console log) but nothing happens. Neither receiving e-mail nor a related log entry.

Get rid of these lines. You don’t use them and I’m not sure if they interfere with the console logger.

I think you should see a log statement when the file is read. Do you see such an entry?

It seems that it is indeed an issue of the JS scripting environment, not that the rules are bad.

I’ve found these exception in the log:

So, the polyglot class is not found for some reason by the openhab environment. As I see the polyglot kar file is in the path ā€œ./var/lib/openhab/tmp/kar/openhab-addons-5.1.0ā€œ. Shouldn’t it be declared in the CLASSPATH?

Ok, so: I’ve missed a log entry in the first place that explains this issue. CPU architecture is not supported (ARM). I am running openhab on a raspberry pi 4 with ARM 32bit.

Do you know if it is possible to have JS scripting for this CPU?

Just FYI openHAB 5.0.0 and newer officially isn’t supported to run on 32 bit OS. Either don’t use JS Scripting (and anything else GraalVM-based) or backup your system and reinstall it to a 64-bit openHABian version. 64-bit also brings a massive speed improvement to JS Scripting compared to running openHAB 4.3.x on 32-bit.
Alternatively use openHAB 4.3.x until you find the time to reinstall openHABian.

For clarity, as of this writing that includes:

  • JS Scripting
  • Python
  • HomeAssistant MQTT

Going forward read the readme for any new add-on installed to determine if GraalVM is required or not.

Thanks for the clarification. Yes, I missed the point that openHAB 5 is not supported on 32bit CPUs.

I’ve just ordered an rpi5 with 4GB RAM. Based on the description this will do the trick.

Thanks again for the infos!

1 Like