One rule to rule them all

I just started using the iCalendar binding to bring calendar events into OH2.5. I have a vacation event on my calendar and I would like a set of vacation rules to be active when the vacation event starts and deactivate when the vacation event ends. I have 7 or 8 of these rules and, of course, I can use a conditional for each to control their execution, but I was wondering if there could be a single rule, (or something) that would collectively activate/deactivate these rules? I hope my question makes sense. Currently, these rules exist in one of my rules files. I manually uncomment/comment them as needed. Any ideas?

Ah! I’ve just found this:

In 2.5, the answer depends on whether or not you are using JSR223 rules or Rules DSL. If using Rules DSL the answer is no, you can’t. You’ll have to add an if statement to each rule to test for the vacation switch.

If using JSR223 and the Helper Libraries you can enable/disable rules. Here is an example in Python.

@rule("Christmas Mode",
      description="Enable/disable rules based on the state of vChristmas",
      tags=["christmas"])
@when("Item vChristmas changed")
@when("System started")
def xmas_enable(event):
    """
    Because smart plugs are reused for Christmas lights, disable the rules that
    normally use those plugs when Christmas mode is enabled and enable the
    Christmas lights Rule.
    """

    import time
    time.sleep(40) # Wait until all Rules are loaded

    xmas_enable.log.info("Turning {} Christmas mode"
                         .format(items["vChristmas"]))

    lightRuleId = [rule for rule in rules.getAll()
                   if rule.name == "Christmas lights"][0].UID
    humidifierRuleId = [rule for rule in rules.getAll()
                        if rule.name == "Humidifiers"][0].UID

    ruleEngine = (osgi.get_service("org.openhab.core.automation.RuleManager") or
               osgi.get_service("org.eclipse.smarthome.automation.RuleManager"))
    ruleEngine.setEnabled(lightRuleId, items["vChristmas"] == ON)
    ruleEngine.setEnabled(humidifierRuleId, items["vChristmas"] != ON)

There is more description at the tutorial you already found.

For OH 3, it’s a little easier. The above still applies in rules in text files. But you also have the option to quickly enable/disable rules through the UI. Here is the above rule I’ve built through the UI in OH 3. No “code” required.

triggers:
  - id: "1"
    configuration:
      itemName: TisTheSeason
      state: OFF
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      enable: false
      ruleUIDs:
        - christmas_lights
    type: core.RuleEnablementAction
  - inputs: {}
    id: "3"
    configuration:
      enable: true
      ruleUIDs:
        - humidifiers
    type: core.RuleEnablementAction

and

triggers:
  - id: "1"
    configuration:
      itemName: TisTheSeason
      state: ON
    type: core.ItemStateChangeTrigger
conditions: []
actions:
  - inputs: {}
    id: "2"
    configuration:
      enable: true
      ruleUIDs:
        - christmas_lights
    type: core.RuleEnablementAction
  - inputs: {}
    id: "3"
    configuration:
      enable: false
      ruleUIDs:
        - humidifiers
    type: core.RuleEnablementAction

Ok, great thanks. I also found your design pattern post regarding this.

“My Break-Dancing days are over, but there’s always the Funky Chicken” – The Full Monty