After migrating from openhab 3.4 to openhab 4, I see these errors:
2023-07-25 00:36:06.420 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: "itemRegistry" is not defined
at <js>.:program(<eval>:1) ~[?:?]
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.ScriptConditionHandler.isSatisfied(ScriptConditionHandler.java:54) ~[?:?]
at org.openhab.core.automation.internal.RuleEngineImpl.calculateConditions(RuleEngineImpl.java:1158) ~[?:?]
at org.openhab.core.automation.internal.RuleEngineImpl.runRule(RuleEngineImpl.java:995) ~[?:?]
at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
at java.lang.Thread.run(Thread.java:833) ~[?:?]
and:
==> /log/logs/openhab.log <==
2023-07-25 00:32:00.500 [ERROR] [b.automation.script.javascript.stack] - Failed to execute script:
org.graalvm.polyglot.PolyglotException: ReferenceError: "events" is not defined
at <js>.:program(<eval>:12) ~[?:?]
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.runRule(RuleEngineImpl.java:997) ~[?:?]
at org.openhab.core.automation.internal.TriggerHandlerCallbackImpl$TriggerData.run(TriggerHandlerCallbackImpl.java:87) ~[?:?]
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) ~[?:?]
at java.util.concurrent.FutureTask.run(FutureTask.java:264) ~[?:?]
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136) ~[?:?]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635) ~[?:?]
at java.lang.Thread.run(Thread.java:833) ~[?:?]
2023-07-25 00:32:00.507 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'f2ad118f70' failed: org.graalvm.polyglot.PolyglotException: ReferenceError: "events" is not defined
I suspected these have something to do with rules. I have a few of them that are triggered frequently and which have actions defined in Blockly. I took one and tried running it. And it gave me one of the above errors but with a bit more text to. I then opened the blockly design, tried to run the rule from there and saw in the log:
2023-07-25 00:46:49.379 [INFO ] [openhab.event.RuleUpdatedEvent ] - Rule '3e005b972e' has been updated.
The rule then passed fine.
So it seems that I have to go through all my rules and do this âopen blockly codeâ and âsave itâ manually to fix the generated script?
As mentioned in the list of breaking changes and documented here you need to convert your Blockly rules to be compatible with OH4 by opening each one and saving it anew. You also need to install the JS Scripting add-on.
Thank you Rich and Stefan! I read through the release notes but did not fully understand the part about breaking changes in the Javascript automation. Afterwards when searching for a solution, I didnât reread this part because I couldnât find the word âblocklyâ since in the text, the plural âblockliesâ is used. My fault! Thanks again!
Howdy. I also upgraded to 4.0.2 and getting both these errors. I created my rules using ECMAScript, how can I fix this error? I donât use âblockyâ
Install the JS Scripting add-on and rework your rules using the new ECMAScript 11 syntax using the openhab-js library.
Install the Nashorn JS Scripting add-on and change the type of your script actions and conditions to âapplication/javascript:ECMAScript5.1â (I think thatâs right, search the forum if not or just create a new script choosing Nashorn JS as the language and see what it uses for the type). You can find and change the type by clicking on the code tab of the rule.
I already tried that. Thatâs how I got the âeventsâ not defined error. I get both errors, when I use the non Nashorn I get the itemRegistry error and when I use the Nashorn I get the events error.
Ok, found out the problem. I had to edit the _Run rule as well. It is working now. I guess I will start switching to the newer ECMAScript 11 but I need to have my Alexa working since I rely so much on it.
im porting my OH3 RPI-Setup to OH4-VM-Setup and seems like im running into the migration topic with my rules.
I tried to migration backdoors mentioned here, but the connected items does not fire any MQTT-Messages.
As far as now im almost sure that it is somewhere in the OH4 version. The OH4-VM-MQTT-Broker is up and running. Im tested this by routing all my required MQTT-Traffic onto my OH4 VM-MQTT-Broker, also my OH3-RPI âproductive systemâ is connected to the OH4-MQTT-Broker.
Rules in OH3 fire MQTT-TX-msg if triggered. IÂŽve deleted some rules for testing in OH4 and rebuilding from scratch, but nothing viewable on MQTT explorer or similar.
Any suggestions? I saw https://community.openhab.org/t/sending-mqtt-command-with-script/143425/5, but im not sure if it is running on OH4
I did this, and it worked for all rules but one. The one where the itemRegistry remains even after saving (in fact, even after recreating the entire rule) is one using the âcopy state from to â block from the âCommon Blocksâ library
If this is a third party block library from the marketplace then almost certainly it looks like itâs not been updated. No blocks should reference the itemRegistery like that as one does not directly interact with the registery like that in JS Scripting. All access to Items go through items.
However, keep in mind that updates do not get installed automatically. If the original author did update their library, you would have to remove and readd the library to pick up the changes.
It depends on what âitâ is in âit wouldnât workâ but if your goal is to just update the âtoâ Item, postUpdate is the more appropriate thing to use.
If you want to forward a state update from one Item to cause a device to change, for example to stay in sync, only then does it make sense to sendCommand.
The rule does a simple thing: if one light is turned on, also turn on another light. And if a light is turned off, also turn off the other light. I guess thatâs why I need command.
In general, everytime(!) a rule is saved the code is regenerated and it uses the current implementation of these blocks. The core blocks that come with openHAB use the latest implementation and as Rich said are uptodate with the running openHAB version. However, the copy block is a block from a user library that is not maintained by me, so check if there is a newer version that does not use itemRegistry anymore. As mentioned before that same thing can be achieved with the sendCommand block which is why I never added it to the core blocks.
I find it difficult to understand what parts of my OH instance are updated automatically (i.e. when upgrading to a new OH version) and which parts need to be updated by uninstalling and reinstalling.
In this case, I donât think there is an OH4 compatible version: