Disable rule from another DSL rule

How do i disable a another rule from a DSL Rule?

You don’t. You do not have access to the parts of the API you need to do this from Rules DSL.

But… :wink:

It would be possible to create an API call and send this as a http request, or use an external script and use curl.

At least it’s way to complex to do it this way… setup an additional switch item to enable/disable the rule and use but-only-if. The Rule is still enabled, but won’t execute.

1 Like

Wouldn’t it be easier to have rule One reference a switch item?

And only proceed if the Switch is True / ON ?

1 Like

It depends. For example, I have a number of small humidifiers in the house (musical instruments don’t like 15% humidity). These are on smart outlets of various forms and I’ve sensors in the rooms and rules which turn on the humidifiers when humidity falls below 35% and off when they reach 40%. I’ve half a dozen sensors reporting at least once a minute each. I’ve other rules which alert me if there is an extended period below 35% despite the humidifier being on indicating it’s empty.

However, at Christmas time the whole house humidifier runs (only runs with the heater) so I repurpose those smart outlets to control the Christmas lights and decorations.

In this case it makes a lot more sense to disable the humidity rules at Christmas time and enable the Christmas lights rules because it saves a whole lot of needless churn with the humidity rules being triggered six+ times a minute littering the log with events and otherwise noisily doing nothing.

Both sets of rules are simpler too, since if the rule can trigger at all I can assume it’s supposed to run and do away with a condition or if statement.

It’s also quite nice to see which rules are running and which ones are disabled in the rules list.

Disabling rules can also be a way to implement a sort of periods of processing. Let’s say you have a bunch of rules that should only run when you are away and a different set of rules that should only run when you are home. You could add a condition or if statement to all of those rules but then you are spreading this behavior across all these many rules. If you enable/disable the rules instead you can centralize the behavior in one place/rule making that behavior much easier to maintain in the long run.

Put another way, each rule should mostly care about doing one thing and doing it simply and well (e.g. controlling the lights) . If you can avoid dirtying it up with external concerns (e.g. only controlling the lights when you are away) the rules become simpler and easier to understand and maintain.

Can you do all this with an Item and if statements/condition? Of course. Is it easier? In most cases it’s a one-liner to enable/disable another rule and there are actions so you may not even need code with UI rules. But in certain circumstances enabling/disabling rules comes with real benefits.

2 Likes

That’s what I do with most of my rules, which check the mode my system is in (e.g. home, away, sleep, etc.) before doing anything. However, I’ve never thought of it from the perspective of “enabling/disabling” the rule. Perspective is everything. :wink:

I’ll have to remember this in case I ever get around to moving my rules into MainUI.

1 Like

You can disable a rule even if it’s defined in a text file. However, the next time the file is loaded the rule will become reenabled. So it becomes a little less useful in that context.

1 Like