Share configuration in rules


is there an easy/convenient way of sharing configs between rules? E.g. URLs and credentials for Grafana that are needed in different rules. Is there some kind of configuration service that one could use? I would like to avoid to put those things in items or so.

No. items is the way.
(assuming you speak about rules DSL)

Yeah, rules dsl. Maybe it’s time to switch to JSR-223 then. Is there something out of the box for it?

as long as you use DSL rules being stored in files in /etc/openhab/rules/ you can use global variables.
This means that rules need to be in the same file the global variable is defined.
Global variables are not supported in rules that are done via Main UI in the browser.

Jup, also thought about that. But putting “everything” into one rule only because they share a variable sounds a bit strange :). But thanks for showing the option!

I didn’t mention that option because having a single file has disadvantages such as long compile times and the cluster risk. A single error would stop your whole Home Automation.

Here are all the approaches I know of to put config type information and make them available to rules for all languages.

Technique Description Supported Languages
Item States Simply put the data into Items. In OH 3 it’s easy to set the Item to an arbitrary text value. All
Item Names In some specific cases encoding configuration type information in the Item name itself can be useful. For example a RFID reader reports presence of a chip with a serial number so encode the serial number into the Item name. All
Group Membership Sometimes a good deal of configuration information can be represented simply as Group membership. All
Item Tags Items can be tagged with words which can represent configuration information. All.
Item Metadata Quite complex data can be attached to individual Items using custom Item metadata. All except Rules DSL
Global Variables Hard code the values as global variables. However they are only available to the one .rules file. Rules DSL in text files, not through the UI Put config information as constants defined in a library file. This file can be imported to the rules that need them. Nashorn JavaScript, Python, jRuby?, GraalVM JavaScript?
Semantic Model Some configuration information can be represented in the semantic model. There are some Actions to get the parent Equipment and parent Location for an Item. All

Each of the above are more or less suitable based on the nature of the configuration information and how it’s used. I’ve used them all at one point or another. Most recently I implemented the OAuth2 and some simple API calls to get access to my Honeywell Thermostat and I put most things like the ClientID and ClientSecret and such into Items and stuff like URLs are just hard coded locally in each rule.


Wow, that’s a nice overview, thanks!

The “best” I could come up with in DSL rules was sadly

import java.util.Properties
import java.nio.file.*

val properties = new Properties()

rule "Test"
when Time cron "0/5 * * * * ?"

    if (properties.isEmpty()) properties.load(new StringReader(Files.readString(Paths.get("/etc/openhab/rules/"))));

    logInfo("test", properties.get("grafanaToken"))


Wanted to have a single line approach (without try/catch and having to close the stream). But the imports and the global properties variable eat that somehow up again :(.

Maybe time to look at the JSR approach finally…