Using one rule file code for different item setups

Hmm, i have been thinking about a way to implement that stuff smart, but until now i have not come up with a solution.

I have different rooms, those are equiped with a motion detector a light and a lux sensor.
now i wrote a simple rules file to react to lux, motion and brightness changes. it includes stuff for brightness changed, motion changed and stuff. and adjusts the brightness to certain levels depending on certain conditions.

now i want to implement the exact same algorithm and everything in a different room, that has the same sensor functionality, but of course other sensors. so i can copy my whole rule file and just replace the items with the other items, but as soon as i make a change to one rule file, i have to copy it over to the other rule file.

in c++ i would just create a class that is called for example light_control and give it all items needed to control the lights as construction parameters. this way whenever i change the source code for the light_control, it affects all rooms at once without me having to copy and code…

is there a smart way to implement this in openhab ?
it tried to put variables at the start of the rule file like

var NumberItem item_brightness = Brightness_Living_M

it works to use this item inside rules, but triggering a rule with the above item like

rule "Light Brightness Changed"
when    
    Item item_brightness changed	
then

does not get triggered if the source item, here Brightness_Living_M gets changed.

i have a different idea to create a virtual item and then use a rule

rule "Light Brightness Changed"
when    
    Item Brightness_Living_M changed 
then
     item_brightness.sendCommand(25)
.....

with this solution i could at least use my code and copy it completely over to other rules files, just keeping the top of the file which has those rules to the original items, that then call my rules by triggering an update.

this somehow still feels a little clumsy, so before i go ahead with this i thought to ask here if someone knows an elegant solution ?

this would be also usefull for other people who have for example lets say the same hardware setup and just want to use my algorithm, then they could just set motion_item to their motion sensor, light_item to their light brightness and lux_item to the lux sensor and bam the whole thing would work. lets say like programming a library in c++ with a class that does exactly what you want, you just provide the public methods to control the class and do not worry about the source code inside. Would make it easy to just upload my updates someplace.

Take a look here…

oh, this looks cool, ok, will take me some time to read it, but looks very promising, thanks for that link.

1 Like

Or search for the design patterns, associated items and working with groups

The Design Pattern: DRY, How Not to Repeat Yourself in Rules DSL has a number if techniques for this.

OK, now i got input for at least a month :rofl: :scream:
Thx for the links, i will try to read up peace by peace and then figure out which technique suits me best.