This add-on uses a standalone Nashorn Engine (opens new window) to run ECMAScript 5.1 code. The Nashorn Engine was pre-installed in openHAB 2 and openHAB 3 because it was part of Java. Since Java 15 the Nashorn Engine has been removed from Java.
Alternatively, you can create scripts in the
automation/jsr223 configuration directory.
If you create an empty file called
test.nashornjs, you will see a log line with information similar to:
... [INFO ] [.a.m.s.r.i.l.ScriptFileWatcher:150 ] - Loading script 'test.nashornjs'
To enable debug logging, use the console logging commands to enable debug logging for the automation functionality:
log:set DEBUG org.openhab.core.automation
For more information on the available APIs in scripts see the JSR223 Scripting documentation.
# Script Examples
As a simple example, the following script logs "Hello, World!".
console.log will usually not work since the output has no terminal to display the text.
The openHAB server uses the SLF4J (opens new window) library for logging.
var LoggerFactory = Java.type('org.slf4j.LoggerFactory'); LoggerFactory.getLogger("org.openhab.core.automation.examples").info("Hello, World!");
Depending on the openHAB logging configuration, you may need to prefix logger names with
org.openhab.core.automation for them to show up in the log file (or you modify the logging configuration).
The script uses the LoggerFactory (opens new window) to obtain a named logger and then logs a message like:
... [INFO ] [org.openhab.core.automation.examples ] - Hello, World!