Switch to enable/disable a rule

I have a rule that sends me an email and plays a sound when my door sensor switches open/closed. I would like to have a button in Habpanel that will enable or disable this rule. Is this possible?

Here is my current rule

rule “FrontDoorDogEmail"
when
Item FrontDoor changed from CLOSED to OPEN
then
sendMail("xxxxxx@xxxxx.com”, “Alert-Front Door Opened”, “Front Door is OPEN and dogs are attacking”)
executeCommandLine(“aplay /srv/openhab2-conf/sounds/dog45.wav”)

end
rule “FrontDoorDogEmail” when
Item FrontDoor changed from OPEN to CLOSED
then
sendMail("xxxxxx@xxxxx.com", “Alert-Front Door Closed”, “Front Door is now Closed”)

end

Yes,
This is possible. See example below.
You need to create a new switch item (in this example called “YOURNEWSWITCH”) which you can switch on/off.

rule "FrontDoorDogEmail"

when
      Item FrontDoor changed from CLOSED to OPEN
then
    if (YOURNEWSWITCH.state == ON) {
      sendMail("xxxxxx@xxxxx.com", "Alert-Front Door Opened", "Front Door is OPEN and dogs are attacking")
      executeCommandLine("aplay /srv/openhab2-conf/sounds/dog45.wav")
    } else {
        /* Do Nothing */
    }
end
1 Like

The rule by itself could not be disabled, but you should be able to create a virtual switch in OH and with this switch you can disable the mail and sound commands

rule "FrontDoorDogEmail" when
Item FrontDoor changed from OPEN to CLOSED
then
  if (EnableSwitch.state == ON) then {
    sendMail("xxxxxx@xxxxx.com", "Alert-Front Door Closed", "Front Door is now Closed")
  }
end

Thomas

1 Like

Thanks, this works perfect!

Helped me today, thank you!

Thanks!