Disbale rule based on switch

Dear all,

I have rules, that trigger in the morning, based on several conditions. Now I would like to disable some of those rules based on a virtual switch, which I created.

Scenario would be as follows (example):
Usually, my rollershutters open up in the morning (latest 8AM in almost all rooms - except bedroom). Now if guests are present, I would like to pause/disable this rule, as the guestroom should also remain dark to not wake up the guests.
Now I understand, i could put this virtual switch as further condition for the rule. but as there are many rules to be affected, I would like to simply deactivate a few rules (like 10-15) based on only the virtual switch (Guest_Present) being switched “ON”.

Can someone let me know how this can be done?

openHAB 3 is in use
All rules and items are created in the backend (Visual Studio Code)

This is pretty easy via the gui rule editor in OH3.
Just create a new rule. As the “Then action” select show all and then “enable or disable rules”. There you can select all the effected rules which should be enabled or disabled.

Thanks @dirkdirk

understood and tried that. Works fine.

Just for the sake of “learning”, how can this be done as code in VS?

Sorry, can’t help you with that. I am no coder, I am more the GUI user :shushing_face:

There might be a complicated approach involving deletion and recreation of xxx.rules files under control of a DSL rule.

Adding an if() statement to your rules is pretty simple, though.

Here is the start of a DSL rule used for my kitchen ventilation

rule "Kitchen ventilation control"
when
    Item rule_kitchenVentilation changed
    or
    Time cron "/30 * * * * ?"
    or
    Item VentilationMode_Kitchen changed

then

	if (rule_kitchenVentilation.state == ON) {

		postUpdate(rule_kitchenVentilation_TimeStamp, new DateTimeType())

		var Number counter
		counter = rule_kitchenVentilation_Counter.state as DecimalType
		counter = counter + 1
		postUpdate(rule_kitchenVentilation_Counter,counter)

//    your code after this line

rule_kitchenVentilation - turns the rule execution on/ off
rule_kitchenVentilation_Counter - counts how many times the rule was triggered
rule_kitchenVentilation_TimeStamp - shows the last time the rule was executed

And here is a print screen from habpanel
image

Thanks guys for all the feedback! Works well with the UI rule generation.

Final question - and maybe @dirkdirk you can answer:
If I create the rule in the UI, I do not find a spot to place the “logInfo” part.

I usually do a logInfo if a rule runs, especially to debug them, if something goes wrong. How do i do a logInfo?

Currently a logInfo always looks like this, if I do them in VS:

It cannot be done in Rules DSL.

In JavaScript it will look something like

var logger = Java.type("org.slf4j.LoggerFactory").getLogger("org.openhab.model.script.Rules.Examples");

// Find the rule
logger.info("There are " + rules.getAll().length + " rules");
for each (var r in rules.getAll()) {
  logger.info("Rule name = " + r.getName());
  logger.info("Rule UID = " + r.getUID());
}
var thisRule = rules.getAll().stream().filter(function(i){ return i.name == "Experiments" }).findFirst().get();
logger.info("This rules UID is " + thisRule.getUID());

// Disable the rule
var FrameworkUtil = Java.type("org.osgi.framework.FrameworkUtil");
var _bundle = FrameworkUtil.getBundle(scriptExtension.class);
var bundle_context = _bundle.getBundleContext()
var classname = "org.openhab.core.automation.RuleManager"
var RuleManager_Ref = bundle_context.getServiceReference(classname);
var RuleManager = bundle_context.getService(RuleManager_Ref);
logger.info("Enabled = " + RuleManager.isEnabled(thisRule.getUID()));
RuleManager.setEnabled(thisRule.getUID(), false);
logger.info("Enabled = " + RuleManager.isEnabled(thisRule.getUID()));