JS issues after OH4 upgrade

I have upgraded OH 3.4.4 to OH 4.0.1.
I had to upgrade the Blockly rules and therefor installed the add-on NashornJS.
Everythink worked fine until I deinstalled NashornJS after the Blockly rule upgrade.

After the removal of NashornJS the DSL rules and Blockly rules still work but the JS rules no longer work.

Within the rule engine only “rule DSL” and “ECMAScript (Edition 11)” show up - "ECMAScripst (Edition 5.1) no longer show up (caused by NashornJS removal)


The following error show up:

2023-08-07 21:09:56.762 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: “itemRegistry” is not defined
at .:program(:3) ~[?:?]
at org.graalvm.polyglot.Context.eval(Context.java:399) ~[?:?]
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:458) ~[?:?]
at com.oracle.truffle.js.scriptengine.GraalJSScriptEngine.eval(GraalJSScriptEngine.java:426) ~[?:?]
at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:262) ~[java.scripting:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.DelegatingScriptEngineWithInvocableAndAutocloseable.eval(DelegatingScriptEngineWithInvocableAndAutocloseable.java:53) ~[?:?]
at org.openhab.automation.jsscripting.internal.scriptengine.InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.eval(InvocationInterceptingScriptEngineWithInvocableAndAutoCloseable.java:78) ~[?:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.lambda$0(ScriptActionHandler.java:71) ~[?:?]
at java.util.Optional.ifPresent(Optional.java:178) ~[?:?]
at org.openhab.core.automation.module.script.internal.handler.ScriptActionHandler.execute(ScriptActionHandler.java:68) ~[?:?]
at org.openhab.core.automation.internal.RuleEngineImpl.executeActions(RuleEngineImpl.java:1188) ~[?:?]
at org.openhab.core.automation.internal.RuleEngineImpl.runNow(RuleEngineImpl.java:1039) ~[?:?]
at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:381) ~[?:?]
at org.openhab.core.automation.rest.internal.RuleResource.runNow(RuleResource.java:398) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at org.apache.cxf.service.invoker.AbstractInvoker.performInvocation(AbstractInvoker.java:179) ~[bundleFile:3.4.5]

beginning of the rule script:

var today_cloudiness, current_cloudiness, guest_status,rollo_ost_state, rollo_sued_state, rollo_west_state, sun_azimut, sun_elevation, sun_temp1, sun_temp2, sun_difftemp;
var logger = Java.type(‘org.slf4j.LoggerFactory’).getLogger(‘org.openhab.rule.’ + ctx.ruleUID);
today_cloudiness = itemRegistry.getItem(‘OneCallAPIweatherandforecast_ForecastToday_Cloudiness’).getState();
current_cloudiness = itemRegistry.getItem(‘OneCallAPIweatherandforecast_Current_Cloudiness’).getState();
guest_status = itemRegistry.getItem(‘GuestStatus’).getState();
rollo_ost_state = itemRegistry.getItem(‘RolloOstStatus’).getState();
rollo_west_state = itemRegistry.getItem(‘RolloWestStatus’).getState();
rollo_sued_state = itemRegistry.getItem(‘RolloSuedStatus’).getState();
sun_azimut = itemRegistry.getItem(‘LokaleSonnendaten_Azimut’).getState();
sun_elevation = itemRegistry.getItem(‘LokaleSonnendaten_Position_Elevation’).getState();
sun_temp1 = itemRegistry.getItem(‘Sonnensensor_Sensortemperature1’).getState();
sun_temp2 = itemRegistry.getItem(‘Sonnensensor_Sensortemperature2’).getState();
sun_difftemp = sun_temp1 - sun_temp2;
events.sendCommand(‘Sonnensensor_DiffTemperatur’, sun_difftemp);
//
// Reset der Rollo Stati erfolgt durch Rule “Rollos am Morgen öffnen” //
// Ost-Rollos teilweise schließen, falls sie morgens ganz geöffnet wurden und Bewölkung < 70% ist
if (sun_azimut < 170.0 && today_cloudiness < 70.0 && current_cloudiness < 70.0 && guest_status != ‘ON’ && rollo_ost_state == 0) {
logger.warn(‘Today + Current Cloudiness ist kleiner 70 und Guest Status ist nicht ON → Rollos teilweise auf’);
events.sendCommand(‘RollladenGastezimmerSud_BlindsControl1’, ‘65’);
events.sendCommand(‘RollladenGastezimmerOst_BlindsControl1’, ‘60’);
events.sendCommand(‘RollladenGalerieOst_BlindsControl1’, ‘60’);
rollo_ost_state = 1;
events.sendCommand(‘RolloOstStatus’, 1);
logger.warn('Sun Azimut: ’ + sun_azimut);
logger.warn('Sun Elevation: ’ + sun_elevation);
logger.warn('Sun Diff Temp: ’ + sun_difftemp);
logger.warn('Heutige Bewölkung: ’ + today_cloudiness);
logger.warn('Aktuelle Bewölkung: ’ + current_cloudiness);
logger.warn(‘Rollo Gästezimmer Ost und Galerie Ost geöffnet’);
}
//
// Ost-Rolläden ganz auf, wenn Sonne so weit im Süden steht, dass sie die Ostseite nicht mehr bescheinen kann (nur wenn keine Gäste im Haus sind)
if (sun_azimut > 170.0 && guest_status != ‘ON’ && rollo_ost_state == 1) {
events.sendCommand(‘RollladenGastezimmerOst_BlindsControl1’, ‘0’);
events.sendCommand(‘RollladenGalerieOst_BlindsControl1’, ‘0’);
rollo_ost_state = 2;
events.sendCommand(‘RolloOstStatus’, 2);
logger.warn('Sun Azimut: ’ + sun_azimut);
logger.warn('Sun Elevation: ’ + sun_elevation);
logger.warn('Sun Diff Temp: ’ + sun_difftemp);
logger.warn('Aktuelle Bewölkung: ’ + current_cloudiness);
logger.warn(‘Rollo Gästezimmer Ost und Galerie Ost geöffnet’);
}
//
// Süd-Jallousie-Lamellen halb zu, wenn Sonnen-Einstrahlwinkel > 13°, Sonnenrichtung > 145° und Bewölkung < 70% ist
if (sun_azimut > 145.0 && sun_elevation > 13.0 && current_cloudiness < 70.0 && rollo_sued_state == 0) {
events.sendCommand(‘JalousiePanoramazimmerSud_BlindsControl2’, ‘50’);
events.sendCommand(‘JalousieWohnzimmerSud_BlindsControl2’, ‘50’);
rollo_sued_state = 1;
events.sendCommand(‘RolloSuedStatus’, 1);
logger.warn('Sun Azimut: ’ + sun_azimut);
logger.warn('Sun Elevation: ’ + sun_elevation);
logger.warn('Sun Diff Temp: ’ + sun_difftemp);
logger.warn('Aktuelle Bewölkung: ’ + current_cloudiness);
logger.warn(‘Jalousielamellen Süd halb geschlossen’);
}

Reinstalling NashornJS would possibly not be the best solution to solve the problem.
Any idea how to fix the proiblem?

In fact, it *wont" fix it, at least not by itself.

In OH 3.4 the MIME type for a Nashorn JS script was application/javascript but in OH 4.0 it’s application/javascript:ECMAScript 5.1.

You have three choices (in order of preference):

  1. Rewrite the rules using JS Scripting syntax (see JavaScript Scripting - Automation | openHAB).

  2. Modify the rules to make the Nashorn JS syntax compatible with JS Scripting. That involves adding the following line to the top of your script: var runtime = require('@runtime'); and in front of every Object that interacts with openHAB (.e.g items, itemRegistry, actions, etc.) you need to put runtime. (e.g. runtime.items, runtime.itemRegistry, …).

  3. Reinstall Nashorn JS and change the mime type of each script action to application/javascript:ECMAScript 5.1 (click the “code” tab of the rule).

Thank you @rlkoshak for your quick reply! I think going for an ECMAScript 5.1 option is not a good idea. I’ll try to use JavaScript Scripting (ECMAScript 2022+) to rewrite the rules … or use Blockly, which now has more functionality

It doesn’t have to be all or nothing. You can start with 3 and gradually migrate rules one by one to 1.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.