Best practice for .rules

Hello!
whats the best way for .rules files? One big .rules with many rules inside or many little .rules files with only on or two rules?
Whats best practice for openhab?

Thanks,
Christoph

There are different topics to think about.
When you have the need to switch rules on and off ( Christmas lighting etc…) it is better to have them seperated.
The use of global variables is possible only within the scope of the file where they are created.

I have a similar question, what is the best practice for multiple similar rules. When i want the same rule apply for different items ? E.g. i want to control lights in the same way in multiple rooms, the only things that will differ in those rules will be switch and light items. Now i just copy the rules and change items in them, the rules are very simple so making a function will not change anything because still i need to define rules with proper items.

Hi @rozpruwacz,

In that case I’d suggest the Design Pattern: Associated Items which will enable you to re-use the same rule with differing trigger and actions.

Cheers
James

yeah, that’s pretty neat :slight_smile: thanks !

@rozpruwacz, in addition to Associated Items, I think

and

may also be applicable to your situation.

For the lambdas, @opus’s warning applies, only rules in the same .rules file as the lambda is able to call the lambda.

The following will also be useful:

@chzimm, to elaborate on @opus’s response. I usually recommend separating your .items and .rules files into separate files based on their functionality. For example, put all your lighting rules into one file and all your presence rules in another. This will naturally co-locate those rules that are most likely to need access to the same global vars and vals and lambdas. When you have cross cutting concerns (e.g. alerting), the Separation of Behaviors Design Pattern above will let you centralize that code into its own file but let all your rules access it by commanding Items.