openhab-addons/bundles/org.openhab.automation.groovyscripting
Jacob Laursen f4596f581e
Update license headers to 2024 (#16168)
* Update copyright year in configuration
* Update license headers
* Manually update nibeheatpump headers (.cpp/.h/.ino)
* Manually update smsmodem header
* Manually update hueemulation header
* Manually update addon-header.xml header

Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
2024-01-01 18:14:57 +01:00
..
src/main Update license headers to 2024 (#16168) 2024-01-01 18:14:57 +01:00
NOTICE [groovyscripting] Add Groovy scripting support (#8772) 2020-10-17 23:32:14 +02:00
pom.xml Apply spotless after release (#16097) 2023-12-22 23:30:38 +01:00
README.md [groovyscripting] Update Groovy to 4.0.11 (#14734) 2023-04-02 09:08:53 +02:00

Groovy Scripting

This add-on provides support for Groovy 4.0.11 that can be used as a scripting language within automation rules and which eliminates the need to manually install Groovy.

Creating Groovy Scripts

When this add-on is installed, you can select Groovy as a scripting language when creating a script action within the rule editor of the UI.

Alternatively, you can create scripts in the automation/jsr223 configuration directory. If you create an empty file called test.groovy, you will see a log line with information similar to:

    ... [INFO ] [.a.m.s.r.i.l.ScriptFileWatcher:150  ] - Loading script 'test.groovy'

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

Groovy scripts provide access to almost all the functionality in an openHAB runtime environment. As a simple example, the following script logs "Hello, World!". Note that System.out.println will usually not work since the output has no terminal to display the text. The openHAB server uses the SLF4J library for logging.

import 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 to obtain a named logger and then logs a message like:

    ... [INFO ] [.openhab.core.automation.examples:-2   ] - Hello, World!