Skip to content
This repository has been archived by the owner on May 7, 2020. It is now read-only.

Scripted Rule Support

Steve Bate edited this page Oct 7, 2018 · 18 revisions

Table of Contents

Scripted Rule Support

Basic scripted rule support documentation can be found at https://www.openhab.org/docs/configuration/jsr223.html.

Further readings with much more advanced examples:

Install in nightly openHAB 2 Build

  • Install Build
  • Create the directory [openHAB-folder]/conf/automation/jsr223 and copy your scripts into it.
  • Run openHAB, in Paper UI go to Add-ons>MISC and install Rule Engine (Experimental)

Install in IDE

This is how I got the scripted rule support running within the IDE.

Include the following bundles in the launch config:

org.eclipse.smarthome.automation.module.core
org.eclipse.smarthome.automation.api
org.eclipse.smarthome.automation.provider.file
org.eclipse.smarthome.automation.rest
org.eclipse.smarthome.automation.module.script.rulesupport
org.eclipse.smarthome.automation.module.timer
org.eclipse.smarthome.automation.providers
org.eclipse.smarthome.automation.parser.gson
org.eclipse.smarthome.automation.core
org.eclipse.smarthome.automation.module.script

Add this as a VM argument in the launch config: [openHAB-folder]/runtime/bin/[setenv|setenv.bat]

-Dorg.osgi.framework.bundle.parent=ext 

Scripts are searched for in conf/automation/jsr223.

JavaScript

The following example.js worked for me:

'use strict';

scriptExtension.importPreset("RuleSupport");
scriptExtension.importPreset("RuleSimple");

var sRule = new SimpleRule() {
    execute: function( module, input) {
        print("This is a 'hello world!' from a Javascript rule.");
    }
};

sRule.setTriggers([
    TriggerBuilder.create()
        .withId("aTimerTrigger")
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(
            new Configuration({
                "cronExpression": "0 * * * * ?"
            })).build()
    ]);

automationManager.addRule(sRule);

Jython

Download jython-standalone-x.y.z.jar from http://www.jython.org/downloads.html

Add this library to the "Bootstrap entries" on the main page of the launch config by simply putting it with its full path there.

The following example.py worked for me:

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")

class myRule(SimpleRule):
    def execute(self, module, inputs):
        print "This is a 'hello world!' from Jython rule."

sRule = myRule()
sRule.setTriggers([
    TriggerBuilder.create()
        .withId("aTimerTrigger")
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(
            Configuration({
                "cronExpression": "0 * * * * ?"
            })).build()
    ])

automationManager.addRule(sRule)

Groovy

  1. Download https://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.12.zip
  2. Extract /groovy-2.4.12/lib/groovy*.jar to /runtime/lib/ext
  3. Start runtime and install Rule Engine (Experimental) (Paper UI -> Add-ons -> Misc)
  4. Add example.groovy file to /conf/automation/jsr223 with the following contents:

The following example.groovy worked for me:

import org.eclipse.smarthome.automation.*
import org.eclipse.smarthome.automation.module.script.rulesupport.shared.simple.*
import org.eclipse.smarthome.config.core.Configuration

scriptExtension.importPreset("RuleSupport")
scriptExtension.importPreset("RuleSimple")

def sRule = new SimpleRule() {
    Object execute(Action module, Map<String, ?> inputs) {
        println("Hello World from Groovy")
    }
}
sRule.setTriggers([
    TriggerBuilder.create()
        .withId("aTimerTrigger")
        .withTypeUID("timer.GenericCronTrigger")
        .withConfiguration(new Configuration([cronExpression: "0 * * * * ?"]))
        .build()
    ])

automationManager.addRule(sRule)

Install on debian

  • using nightly deb package from repo
  • install openhab2 using the debian package (version 2.1+)
  • Create the directory /etc/openhab2/automation/jsr223 and copy your scripts into it.
  • Run openHAB, in Paper UI go to Add-ons>MISC and install Rule Engine (Experimental)

Jython

  • From http://www.jython.org/downloads.html download jython-standalone-x.y.z.jar to the directory: /usr/share/openhab2/runtime/lib/boot/.
  • Restart openhab2: service openhab2 restart