If I put this code directly in OenHAB (Rules-> rule name-> execute a given script (ECMAScript) → edit) the command works fine.
If I put this code in my external JavaScript Code (“/meine_scripts/heizungssteuerung.js”) this code is not working.
I do not understand why? Both codes parts are in JavaScript and the load command works also fine?
ECMAScript 5.1 I’m assuming and not ECMAScript 11 through the JS Scripting add-on.
Yes. It’s because events is part of the context that openHAB injects into your rule (a Script in MainUI is just a special case of a rule). But when you load your library, it doesn’t automatically have access to that injected stuff like events.
I never used text file ECMAScript 5.1 rules, but in Jython, there was something you’d need to do in your library to pull in the openHAB stuff (I don’t remember what it was though). But it’d probably be more standard to pass events to your library functions instead.
When using JS Scripting, you don’t mess with the OH stuff directly. It’s all handled by the included helper library. So you’d import items in your external library and call
2022-11-01 20:15:07.585 [ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID 'heizung_og_bug_balkontuer' failed: SyntaxError: /etc/openhab/meine_scripts/heizungssteuerung.js:31:1 Expected an operand but found const
You are not using the JS Scripting add-on. You are not using ECMAScript 11. You are using ECMAScript 5.1. That ancient (relatively speaking) version of JavaScript doesn’t have const, require and a ton of other language enhancements that have been made over the past 13 years.
I only provided that example because lots of people will run across this thread and might become confused.
You are not using JS Scripting so please ignore everything in the above post after “When using JS Scripting…”.
You’ll need to follow the advice in the top part of the reply.