Script execution of rule with UID failed: ReferenceError: "console" is not defined in <eval> at line number 1

OpenHAB 3 on Rpi 3
ecma-262 edition 5.1

[ERROR] [internal.handler.ScriptActionHandler] - Script execution of rule with UID ‘1500c07fa2’ failed: ReferenceError: “console” is not defined in at line number 1

Unable to run simple console.log

If you are on 5.1 you need to use

var logger = Java.type(‘org.slf4j.LoggerFactory’).getLogger(‘org.openhab.rule.’ + ctx.ruleUID);
logger.info(“…”)

only with “ECMAScript 262 Edition 11” console.log() is supported.

@florian-h05 correct me if I am wrong.

Only the newer
Btw, you can always create a blockly rule and see what the blocks are creating in the code tab.

Thanks Stefan. It is working. Can we update JS version? if not any documentation for older version?

To use ECMAScript 262 Edition 11 (using the GraalJS runtime) on openHAB 3.x, just install the JavaScript Scripting add-on.

Getting documentation for the older JS runtime (it‘s called NashornJS) is really difficult, the best way to see how something works is to see what Blockly generates. But I do NOT recommend to code any new stuff in NashornJS, because it is type of deprecated as well as old and outdated. GraalJS is the opposite: Actively developed, up-to-date (supports the latest JS language features) and very well documented with a great helper library.

You can find the documentation for JS ECMAScript 262 Edition 11 (GraalJS runtime) on the add-on page: JavaScript Scripting - Automation | openHAB.

Thanks Florian, initially I was not able to find addon. Later I checked found that I was using OH3.0, after migrating to OH3.4 now I am able to use GraalJS.

1 Like

And as we mentioned, really try out blockly (see tutorial). It not only gets you up and running pretty quickly, you can also peek into the code tab which tells you how to write you own pure js code and learn from it.

1 Like