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 viaPersistenceExtensions
). 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!