Prevent rule from being triggered by another rule

  • openHAB version: 2.4.0

Hi all,
this is not really a problem, just a question and I am pretty sure that I have read something similar in this community but I can’t find it anymore.

I am searching for an elegant way to prevent the 2nd rule to be triggered out of the 1st one.

rule "rule1"
when
        Time cron "0 0 12 ? * MON-FRI *"
then
        Switch1.sendCommand(OFF)
        Switch2.sendCommand(OFF)
end

rule "rule2"
when
        Switch1 changed
then
        Switch2.sendCommand(OFF)
end

I think this is possible this way …

rule "rule1"
when
        Time cron "0 0 12 ? * MON-FRI *"
then
        Switch_helper.sendCommand(ON)
        createTimer(now.plusSeconds(1), [ |
        Switch1.sendCommand(OFF)
        Switch2.sendCommand(OFF)
        ])
        createTimer(now.plusSeconds(2), [ |
        Switch_helper.sendCommand(OFF)
        ])
end

rule "rule2"
when
        Switch1 changed
then
if (Switch_helper.state == OFF) {
        Switch2.sendCommand(OFF) }
end

with this solution the rule is triggered but with no action. but I wonder if there is a better solution to prevent that one rule triggers another one.
And yes, in this special case I could remove Switch 2 from the first rule and let both rules be in place.

I ask because I like to understand the rule engine :slight_smile:

greetings

You are not giving much detail about your use case, so it is hard to judge what would work, but here some options:

  • postUpdate instead of sendCommand may work, as it would not trigger the rule
  • if you did not select the auto-update for your items, potentially triggering on update received rather than command received can provide an option to delineate things; note: if auto-update is selected, items are automatically updated when a command is received, which means that update or command received will trigger equally well
  • selecting a selective rule trigger changes can help (“changed from OFF to ON” or similar)
  • probably the easiest, if your use case allows it, is to create a proxy-item and work with that
  • obviously timers like you are using work, but only if you do not expect any competing events within the two second lock-out time

Just one more thought, your second rule make me wonder whether you may just want to replicate the actions of one item to another (again going out on a limp as it is unclear what you want to accomplish), this may of course not be true as right now, any action on switch2 will just switch off switch1, but still; this can also be accomplished on the Thing level through the follow profile: https://www.openhab.org/docs/configuration/items.html#profiles