Need help at JS Scripting Rules


I started to learn the new rule engine with JS Scripting.

So I created my first test script under /etc/openhab/automation/js called it Testrule.js with inside:

  name: "Test rules",
  description: "Test rule",
  triggers: [triggers.GenericCronTrigger("0/40 0 8 * * ? *")],
  execute: (event) => {
        console.log("testing done", "yes")
  tags: ["test", "Test"],
  id: "test"

But the rule is never loaded…or triggered…

any help for a beginner appreciated :slight_smile:

Have you installed the JS Scripting automation addon?

Yes, it is installed double checked it :slight_smile:

And in the log I see this error

Error: Invalid CommonJS root folder: /etc/openhab/automation/js/node_modules

What’s in that folder?

Only the script

/etc/openhab/automation/js $ ls -la
total 12
drwxr-xr-x 2 openhabian openhab 4096 Dec 27 17:11 .
drwxrwxr-x 4 openhab    openhab 4096 Dec 27 13:15 ..
-rw-r--r-- 1 openhabian openhab  250 Dec 27 17:11 testf.js
openhabian@smarthome:/etc/openhab/automation/js $

Hmmmm. There might be a bug. @florian-h05, is it possible that the Js Scripting add-on always expects there to be a node_modules folder? If so it should be created when the add-on is installed. If not do you have any ideas?

@milo, in the mean time, create that node_modules folder and see if the error goes away.

Still not loading…

Can you please provide some information:

  • java -version
    uname -a
  • openHAB Version

Be sure to restart openHAB just in case it won’t notice the folder is there until it restarts.

Just to eliminate one more thing, ssh to the machine and from the automation/js folder run npm install openhab_rules_tools. This should add a new folder to node_modules and create a couple of json files. This is my library and won’t force you to keep the openhab-js library up to date manually.

You don’t need to keep it up to date manually, a version of openhab-js is included inside the addon itself and upgraded with each new openHAB release.

I am using latest openhabian

openjdk version "11.0.16" 2022-07-19
OpenJDK Runtime Environment (build 11.0.16+8-post-Debian-1deb11u1)
OpenJDK 64-Bit Server VM (build 11.0.16+8-post-Debian-1deb11u1, mixed mode)
Linux smarthome 5.15.76-v8+ #1597 SMP PREEMPT Fri Nov 4 12:16:41 GMT 2022 aarch64 GNU/Linux

The openHAB version is missing, you find it in the WebUI under “Help & About”

  |_|                  openHAB 3.4.0 - Release Build

Right, but in the debugging I wanted @milo to install some NPM library. I chose to have him install mine instead of openhab so he can continue to get the updates with the add-on but still have the package.json and package-lock.json files get created.

The error is complaining that node_models is malformed so I’m grasping at straws.

1 Like

Restarted the whole pi nothing changed…file is loaded now

@rlkoshak There was such a problem one time, but it was before my time as JS Scripting maintainer so I don’t have many information about this.
I am wondering why it still complains with the folder being created. there must be something wrong with the permissions or else.

Looking at the code, the addon is trying to create the node_modules directory and will log a warning if that fails.