events.sendCommand does not execute in external JavaScript file

Hello community,

followed question.
I load an external JavaScript Code:

this.OPENHAB_CONF = (this.OPENHAB_CONF === undefined) ? java.lang.System.getProperty("openhab.conf") : this.OPENHAB_CONF;
load(OPENHAB_CONF + "/meine_scripts/heizungssteuerung.js");

and the code in the javascript works fine.

Now I want to set a value to an select-item:


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?

Thank you for your hint!

OpenHabian OpenHAB 3.3.0

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

const { items } = require('openhab');


thank’s for the quick answer!

Now I have insert the code in my function:

function heizungssteuerung(heizkoerper, quelle, wert) {
 const {items} = require('openhab');
 items.getItem('raumtemp_og_bug_mode').sendCommand ('4');

But an error occured!?

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 didn’t read/understand my reply.

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.