JSScripting engine not loading globals (itemRegistry, actions are undefined)

Hello everyone,

I’m facing a very strange issue on my openHAB installation (Version 4.3.5) and I’m out of ideas.

My Goal:
I’m trying to create a simple rule to calculate daily energy consumption from a power Item (in Watts).

The Problem:
Any attempt to use JSScripting fails because the standard openHAB global objects are not being loaded into the script’s scope. When a rule runs, I get errors like ReferenceError: "itemRegistry" is not defined or TypeError: undefined has no such function "averageSince" when trying to use actions.Persistence.

It seems my JS Scripting engine is running in an isolated state, without the necessary openHAB integrations.

Troubleshooting I’ve Already Done:
I have already tried the following steps multiple times, without any success:

  • Verified that my persistence service (RRD4J) is working correctly. I can see the data in charts and query it via the API Explorer.
  • Attempted multiple JSScripting variations to access persistence (item.history, actions.Persistence, and even direct Java calls via PersistenceExtensions). All of them fail because the required global objects are undefined.
  • Completely uninstalled and re-installed the JS Scripting automation addon.
  • Restarted openHAB multiple times.
  • Cleared the cache using the openhab-cli clean-cache command.

Here is the last version of the code I tried, which fails with ReferenceError: "itemRegistry" is not defined:

var ZonedDateTime = Java.type(“java.time.ZonedDateTime”);
var PersistenceExtensions = Java.type(“org.openhab.core.persistence.extensions.PersistenceExtensions”);

var powerItemName = “Tesla_Powerwall_Instant_Home_Power”;
var powerItem = itemRegistry.getItem(powerItemName); // This line fails

if (powerItem) {
var startOfDay = ZonedDateTime.now().withTimeAtStartOfDay();
var avgPower = PersistenceExtensions.averageSince(powerItem, startOfDay, “rrd4j”);
// … rest of the logic
}

Has anyone seen this behavior before or has any idea what else could be causing this? Any help would be greatly appreciated.

Thank you!

You can checkout my rule template as an example.

Please use code fences when posting code.

```
code goes here
```

Which JS add-on are you using?

The code you’ve posted is a raw JSR223 Nashorn JS type script. It will not work with JS Scripting. It can be made to work with JS Scripting by requiring @runtime, but the whole point of JS Scripting is to use the helper library so you don’t have to mess with raw Java classes and Objects and you get a pure JS interface to OH instead.

Assuming the latest OH 4.3 with the JS Scripting add-on and not Nashorn your code should be:

var name = "Tesla_Powerwall_Instant_Home_Power";

if(items[name] !== undefined]) {
  const startOfDay = time.toZDT("00:00");
  const avgPower = items[name].persistence.averageSince(startOfDay, 'rrd4j');
  // rest of the logic
}

There is no Java.types and no direct interaction with the itemRegistry which isn’t directly available in your rule in JS Scripting. Just using the API as documented