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.
||Simply put the data into Items. In OH 3 it’s easy to set the Item to an arbitrary text value.
||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.
||Sometimes a good deal of configuration information can be represented simply as Group membership.
||Items can be tagged with words which can represent configuration information.
||Quite complex data can be attached to individual Items using custom Item metadata.
||All except Rules DSL
||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.
||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.
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
val properties = new Properties()
when Time cron "0/5 * * * * ?"
if (properties.isEmpty()) properties.load(new StringReader(Files.readString(Paths.get("/etc/openhab/rules/config.properties"))));
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…