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.