Textual Rules

“Rules” are used for automating processes: Each rule can be triggered, which invokes a script that performs any kinds of tasks, e.g. turn on lights by modifying your items, do mathematical calculations, start timers etcetera.

openHAB has a highly integrated, lightweight but yet powerful rule engine included. On this page you will learn how to leverage its functionality to do real home automation.

Defining Rules

File Location

Rules are placed in the folder ${openhab.home}/conf/rules. The demo setup already comes with a demo file called demo.rules, which has a couple of examples that can be a good starting point.

A rule file can contain multiple rules. All rules of a file share a common execution context, i.e. they can access and exchange variables with each other. It therefore makes sense to have different rule files for different use-cases or categories.

IDE Support

The Eclipse SmartHome Designer offers support for rules building. It includes syntax checks and coloring, validation with error markers, content assist (Ctrl+Space) incl. templates etc. This makes the creation of rules very easy!

Bugs: Please note that there are a couple of open bugs related to the SmartHome Designer. These result in error markers in the Designer, while the rules are executed nicely by the runtime.

The Syntax

Note: The rule syntax is based on Xbase and as a result it is sharing many details with Xtend, which is built on top of Xbase as well. As a result, we will often point to the Xtend documentation for details.

A rule file is a text file with the following structure:

  • Imports
  • Variable Declarations
  • Rules

The Imports section contains import statement just like in Java. As in Java, they make the imported types available without having to use the fully qualified name for them. For further details, please see the Xtend documentation for imports.

Example:

import java.net.URI

A few default imports are already done, so classes from these packages do not need to be explicitly imported:

org.eclipse.smarthome.core.items
org.eclipse.smarthome.core.persistence
org.eclipse.smarthome.core.library.types
org.eclipse.smarthome.core.library.items
org.eclipse.smarthome.model.script.actions

The Variable Declarations section can be used to declare variables that should be accessible to all rules in this file. You can declare variables with or without initial values and modifiable or read-only. For further details, please see the Xtend documentation for variable declarations.

Example:

// a variable with an initial value. Note that the variable type is automatically inferred
var counter = 0

// a read-only value, again the type is automatically inferred
val msg = "This is a message"

// an uninitialized variable where we have to provide the type (as it cannot be inferred from an initial value)
var Number x

The Rules section contains a list of rules. Each rule has the following syntax:

rule "rule name"
when
    <TRIGGER CONDITION1> or
    <TRIGGER_CONDITION2> or
    <TRIGGER_CONDITION3>
    ...
then
    <SCRIPT_BLOCK>
end

A rule can have any number of trigger conditions, but must at least have one. The SCRIPT_BLOCK contains the code that should be executed, when a trigger condition is met, see the script section for details on its syntax.

Rule Triggers

Before a rule starts working, it has to be triggered.

There are different categories of rule triggers:

  • Item(-Event)-based triggers: They react on events on the openHAB event bus, i.e. commands and status updates for items
  • Time-based triggers: They react at special times, e.g. at midnight, every hour, etc.
  • System-based triggers: They react on certain system statuses.

Here are the details for each category:

Event-based Triggers

You can listen to commands for a specific item, on status updates or on status changes (an update might leave the status unchanged). You can decide whether you want to catch only a specific command/status or any. Here is the syntax for all these cases (parts in square brackets are optional):

Item <item> received command [<command>]
Item <item> received update [<state>]
Item <item> changed [from <state>] [to <state>]

A simplistic explanation of the differences between command and update can be found in the article about openHAB core actions.

Time-based Triggers

You can either use some pre-defined expressions for timers or use a cron expression instead:

Time is midnight
Time is noon
Time cron "<cron expression>"

A cron expression takes the form of six or optionally seven fields:

  1. Seconds
  2. Minutes
  3. Hours
  4. Day-of-Month
  5. Month
  6. Day-of-Week
  7. Year (optional field)

for more information see the Quartz documentation.

You may also use CronMaker or the generator at FreeFormatter.com to generate cron expressions.

System-based Triggers

Two system-based triggers are provided as described in the table below:

Trigger Description
System started System started is triggered upon openHAB startup, after the rule file containing the System started trigger is modified, or after item(s) related to that rule file are modified in a .items file.
System shuts down Rules using the ‘System shuts down’ trigger execute when openHAB shuts down.

You may wish to use the ‘System started’ trigger to initialize values at startup if they are not already set.

Example:

rule "Speedtest init"
when
    System started
then
    createTimer(now.plusSeconds(30)) [|
        if (Speedtest_Summary.state == NULL || Speedtest_Summary.state == "") Speedtest_Summary.postUpdate("unknown")
    ]
end

Scripts

The expression language used within scripts is the same that is used in the Xtend language - see the documentation of expressions on the Xtend homepage.

The syntax is very similar to Java, but has many nice features that allows writing concise code. It is especially powerful in handling collections. What makes it a good match for openHAB from a technical perspective is the fact that there is no need to compile the scripts as they can be interpreted at runtime.

To be able to do something useful with the scripts, openHAB provides access to

  • all defined items, so that you can easily access them by their name
  • all enumerated states/commands, e.g. ON, OFF, DOWN, INCREASE etc.
  • all standard actions to make something happen

Combining these features, you can easily write code like:

if (Temperature.state < 20) {
    Heating.sendCommand(ON)
}

Manipulating Item States

Rules are often used to manipulate the state of an Item, for example switching lights on and off under certain conditions. Two commands can change the value or state of an Item within rules:

  • MyItem.postUpdate(<new_state>) - Change the status of an Item without causing any implicit actions. Can be used to reflect changes that may be caused by other means.
  • MyItem.sendCommand(<new_state>) - Change the status of an Item and trigger potential further actions, e.g. send a command to the linked device/binding.

In relation to event-based rule triggers the manipulator commands sendCommand and postUpdate act differently. The following table summarizes the impact of the two manipulator commands on the rule execution due to the used trigger:

Command \ Rule Trigger received update received command changed
postUpdate ⚡ rule fires (depends)
sendCommand ⚡ rule fires (depends)
Change through Binding ⚡ rule fires ⚡ rule fires (depends)

Beware: Besides the specific manipulator command methods MyItem.sendCommand(<new_state>) and MyItem.postUpdate(<new_state>), generic manipulators in the form of sendCommand(MyItem, <new_state>) and postUpdate(MyItem, <new_state>) are available. The specific versions is normally recommended.

MyItem.sendCommand(“new state”) versus sendCommand(MyItem, “new state”)

Using the methods MyItem.sendCommand(<new_state>) and MyItem.postUpdate(<new_state>) is often preferable. These are methods of Objects that can accept a variety of types.

Contrary, the Actions sendCommand(MyItem, "<new_state>") and postUpdate(MyItem, "<new_state>") can only accept strings as arguments.

The reasons lie within Java, the object-oriented programming language on which openHAB is built. Java and the Rules DSL have two basic types, primitives and Objects. A lower case letter data type after a var or a val statement, for example var int, indicates a primitive type. An upper case letter data type after a val and var statement, for example var Number indicates an Object. Objects are more complex than primitives.

Objects have methods associated that among others can make many necessary type conversions. Using Myitem.sendCommand(new_state) or Myitem.postUpdate(new_state) can in most cases convert new_state into a type that Object myItem can apply.

The Action sendCommand(MyItem, new_state) does not provide the same flexibilty. For example, if new_state is typed as a primitive (e.g., var int new_state = 3) and myItem is of the Object type Dimmer:

  • the following command will fail: sendCommand(MyItem, new_state).
  • However, the following command will work: MyItem.sendCommand(new_state).

Using MyItem.postUpdate(new_state) or MyItem.sendCommand(new_state) will create the most stable code. It provides by far the best option for avoiding most problems. This syntax ensures that any conversion (typing) of the new_state is done in a way that is most suitable for myItem.

Exception: Actions are useful when the name of the Item is only available as a String. For example, if the name of the Item to receive an update or command was calculated in the Rule by building up a String:

val int index = 5
sendCommand("My_Lamp_" + index, ON)

Implicit Variables inside the Execution Block

Besides the implicitly available variables for items and commands/states, rules can have additional pre-defined variables, depending on their triggers:

  • receivedCommand - will be implicitly available in every rule that has at least one command event trigger.
  • previousState - will be implicitly available in every rule that has at least one status change event trigger.

Early returns

It is possible to return early from a rule, not executing the rest of the statements like this:

if (Temperature.state > 20) {
	return;
}
Heating.sendCommand(ON)

Caveat: Please note the semicolon after the return statement which terminates the command without an additional argument.

Concurrency Guard

If a rule triggers on UI events it may be necessary to guard against concurrency.

import java.util.concurrent.locks.ReentrantLock

ReentrantLock lock  = new ReentrantLock()

rule ConcurrentCode
when
    Item Dummy received update
then
    lock.lock()
    try {
        // do stuff
    } finally{
        lock.unlock()
    }
end

Logging

You can emit log messages from your rules to aid debugging. There are a number of logging methods available from your rules, the java signatures are:

logDebug(String loggerName, String format, Object... args)
logInfo(String loggerName, String format, Object... args)
logWarn(String loggerName, String format, Object... args)
logError(String loggerName, String format, Object... args)

In each case, the loggerName parameter is combined with the string org.eclipse.smarthome.model.script. to create the log4j logger name. For example, if your rules file contained the following log message:

logDebug("kitchen", "Kitchen light turned on")

then the logger you would have to configure to have your messages appearing in the console would be:

log:set DEBUG org.eclipse.smarthome.model.script.kitchen

Summarizing Example

Taking all the information together, an example rule file could look like this:

var Number counter

// setting the counter to some initial value
// we could have done this in the variable declaration already
rule Startup
when
    System started
then
    counter = 0
end

// increase the counter at midnight
rule "Increase counter"
when
    Time cron "0 0 0 * * ?"
then
    counter = counter + 1
end

// tell the number of days either at noon or if a button is pressed
rule "Announce number of days up"
when
    Time is noon or
    Item AnnounceButton received command ON
then
    say("The system is up since " + counter + " days")
end

// sets the counter to the value of a received command
rule "Set the counter"
when
    Item SetCounterItem received command
then
    counter = receivedCommand as DecimalType
end

Further Examples

Many more examples can be found in the Tutorials & Examples category of the community forum. They are community provided and new ones are added constantly.