[SOLVED] Put rule code into method and call it in different rules

Hi
I managed to run a dimmer with rules.
Used the info from the following https://community.openhab.org/t/yet-another-wakeup-light-rule/41941
This works perfect.

Since I have different dimmers i would like to define a function (eg with phython) by providing in a signature item-name, min/max value dim-step.
As a result I could implement in the rules file with a 1-liner call complex operations.

A how-to-tutorial would be great. I (newbie) didn’t find something.
Thanks
Vik

Rules DSL doesn’t work like that. Your two best options are A simple scene management: finally a good use of scripts or Design Pattern: Separation of Behaviors.

There is also Reusable Functions: A simple lambda example with copious notes but the lambda can only be called from the same .rules file and it is not thread safe.

Depending on the nature of the Rule, you might be able to centralize all your Dimmers or whatever into one single Rule. See Design Pattern: Associated Items.

Finally, you can write your Rules in Python to begin with. See https://openhab-scripters.github.io/openhab-helper-libraries/index.html.

1 Like

Thanks. I’ll work through the links

What became of good ol’ OOP? I made the jump from the DSL to Jython mainly because of that reason, so i could practically reuse packaged functionality.

Because a majority of OH users are not programmers. Forcing them to lean OOP principals to write Rules adds that much more that non programmers bed to learn in order to write anything but the simplest if rules. If you know what OOP is, Rules DSL wasn’t made for you in the first place.