Combining Things, Items and Rules into one JRuby file

JRuby has an Item builder and a Thing builder, although the Thing builder is not actually documented in the main openHAB add on documentation.

This opens up the possibility of configuring Things, Items, and Rules in one file for each topic. There could be lights.rb, aircon.rb, extractors.rb and so on, each of them having the ‘full picture’ of Things, Items and Rules. It could alternatively be arranged like kitchen.rb, lounge.rb, or whatever you like.

This is appealing because one of my frustrations with openHAB is the way that Things, Items, and Rules that relate to the same logical part of the home automation, are currently spread across three different directories. Four if they need any transformations.

What do people think of doing this (defining Items and Things into .rb files)? Is it a good/bad idea? Does the transient nature of Things and Items defined like this, get in the way? Is handling a Bridge like MQTT a problem?

@jimtng I would particularly like your thoughts on this. And I think I read somewhere that you define your Things or Items in YAML, then supply them to JRuby? This sounds interesting, if you’d like to elaborate?

That has been included in the 5.0 docs:
https://next.openhab.org/addons/automation/jrubyscripting/#thing-builder
The full documentation is at https://openhab.github.io/openhab-jruby/

The way you structure them is probably a matter of preference.

Firstly it is definitely good to create all the same type of things (e.g. all lights) in one script, that way you only have one place where the same type of things are created. This avoids code duplications.

However, I like having a separate rule script for each room especially when they have different logic, so that doesn’t quite fit in the one script for all devices.

I like to have one script that creates all my things and items. So whenever I want to add/remove/adjust things and items, it’s the only place to look. Also by separating the items creation script from the rule script, the items don’t get unloaded/recreated whenever I need to change my rules (which happens often).

I also have a bit of abstraction for my things/items creation, which is an adaptation of Another Things and Items file Generator, but instead of creating .things and .items file, I now create the things and items directly in JRuby.

I haven’t “cleaned” it up to make it presentable, but I’ll try to post some existing code in the next post.

It is actually the same in principle to things and items created from either .things/.items files, or from jsondb.

When openhab first started, there are no things / items. Then a process comes along and reads .things files and creates the Things as it reads the files. Another process comes along and reads the jsondb and creates the Things known as “Managed Things”. Then another process comes along and reads the .rb file, and creates the JRuby created Things.

The only thing to be aware of is that when the .rb script is reloaded (e.g. when you modified and saved it), the script gets unloaded first, then the new version of the script is loaded. During the unload, all the Things/Items created in that script got removed, and when the new script is loaded, it then runs the code which then creates the Things/Items again.

It’s no different to, say if you restarted the openhab bundle that reads .things/.items file (i.e. org.openhab.core.model.item). It would do exactly the same process.

The difference is that your Item looks like this in .items file:

Switch MySwitch "My Switch" { channel="xxxx" }

vs it looks like this in a ruby file

items.build do
  switch_item MySwitch, "My Switch", channel: "xxxx"
end

But now as you might have realised, creating your items using Ruby means you can programmatically do all sorts of fancy stuff that isn’t possible using .items file.

I’ve just a couple of things to add.

In OH 5 it is looking like you will be able to combine Items, Things, and perhaps rules definitions into one file with the new YAML file format that is actively being worked.

For those using managed Items, Things, et al tags and descriptions are a very convenient way to group related stuff together. For example, all the Items, and rules, and Things you can search for and pin related stuff in the developer sidebar to get a holistic collection of related stuff.

Things don’t have tags but the search includes the Thing description so you can add keywords there for developer sidebar to find.

@jimtng I understand then that with JRuby, a script-created Item’s state will be lost when the script reloads. I can see why you’d put them in a separate file.

@rlkoshak thank you for the insights. I can’t find an overall discussion (only on specific PRs), but I would like to commend @jimtng’s concept of using templates for different device types, to apply DRY principles.

I’m in no rush to take this step, so I’m thinking I’ll wait for this feature in 5.0 and see how well it works.

Scratch that, I found the issue now: RFC: YAML configuration · Issue #3666 · openhab/openhab-core · GitHub

Correct, you would lose the state, unless you’ve set up persistence store/restore. For me this has never been an issue. My items get state update from the relevant bindings usually pretty quickly.

I think “when there’s a will, there’s a way”. There could be a strategy of saving the item state into shared cache prior to reloading, and re-assigning the state back upon creation can be easily done too.

Come to think of it, the same issue applies to .items file. Try modifying an item - its state will reset to NULL.

And indeed, the same applies to UI based Item. Even without changing anything, and just click “Save” the item would lose its state.