Disabling a rule with a switch

Hi All,

I have what I feel must be an easy questions. I am using a very simple rule to turn my espresso machine on in the early morning and off again after a few hours. I included the actual rule below for clarity. I would like to be able to turn off this rule at will with a switch in in the site map. With this if I do not want the machine to turn on the next morning or in near future the entire rule will be disabled.

Can a rule be referred to as a binding or some other method on a switch item? After thinking more on this I am pretty sure that the answer here will lie in a nested if/then or when/then but I am not coming up with too many examples of something that might work for the issue.

Any help is greatly appreciated!

rule "Coffee on 630am"
when
    Time cron "00 30 06 * * ?"
then
    sendCommand(plug5, ON)
end

rule "Coffee off 1130"
when
    Time cron "00 30 11 * * ?"
then
    sendCommand(plug5, OFF)
end
1 Like

If I’m understanding you…

rule "Coffee on 630am"
when
    Time cron "00 30 06 * * ?"
then
    if (CoffeeTimerSwitch.state == ON) {
      sendCommand(plug5, ON)
   }
end

Using a new Switch item:

Switch CoffeeTimerSwitch "Coffee in the morning?"
1 Like
  1. Define a dummy switch in your items file. viz,
Switch sw_dummy_for_coffee_maker "Dummy Switch"

Note there is no binding info for this DELIBERATELY.

  1. Surface this switch in your active sitemap so you can manipulate it through the UI.

  2. Modify your rule to check the state of the switch at execution time:

rule "Coffee on 630am"
when
    Time cron "00 30 06 * * ?"
then
   if (sw_dummy_for_coffee_maker.state == ON) {
    sendCommand(plug5, ON)
  }
end

Note that you do not need to change your cron rule for OFF.

1 Like

Wow awesome answers in minutes. Thanks for the great quick help! Will keep the noob questions to a minimum. Thanks again!

I would recommend

if( sw_dummy_for_coffee_maker.state == OFF) return false

since the switch-state on startup will be “uninitialized” and therefore no rule will fire.

Thanks for the response! I know this is probably obvious but at least at first glance I cant figure out how to integrate this suggestion. I included my updated code with the other suggestions. I did notice that the timer did flip back to off and between the on and off times I was not able to set it to on. Is that what the unintialized item takes care of? Thanks all in advance.

rule "Coffee on 630am"
when
    Time cron "00 30 06 * * ?"
then
   if (coffeetimer.state == ON) {
    sendCommand(plug5, ON)
  }
end

rule "Coffee off 1130"
when
    Time cron "00 30 11 * * ?"
then
    sendCommand(plug5, OFF)
end

You could also you mapDB persistence to keep track of your virtual switches and restore them after reboot.

Just use it like this:

rule "Coffee on 630am"
when
    Time cron "00 30 06 * * ?"
then
   if (coffeetimer.state == OFF) return false

    sendCommand(plug5, ON)

end

Or you can use this snippet to initialize the items.

3 Likes