Rule for mutually exclusive switches

Hey guys,

Struggling with my rule to control a pool heater. I need a rule that is able to intercept a switch ON command and it act to turn OFF something else before processing the ON command. I want to make 2 switches mutually exclusive, meaning only one switch can be ON at any single time.

I create a simple rule that when a switch receive a ON command, if the other switch is ON it will turn it OFF. Visually, it seems to work. The problem is according to the logs, the first ON command take place before the OFF command is sent to the second switch. For a few milliseconds, both switches are ON.

rule “Pool Heater Turns Spa Heater OFF”
when
Item channel3 received command ON
then
if (channel4.state == ON) {
sendCommand(channel4, OFF)
}
end

Any idea will be greatly appreciated.

A third item may work for you, with a rule that prevents both heaters from being on at the same time. You may not need this though… how are your items defined? Specifically, what are they linked to?

If that is too long for both switches to be on, you will have to use proxy items for the switches and write rules so that the physical switches (The ones with the bindings) toggle in the right order.

1 Like

They are GPIO switches which are going to be wired directly into the pool heater board.

I am not sure if these milliseconds are tolerable or not. I was also thinking on trying to implement this at the hardware level.

Thanks again!

If you can implement it at the hardware level, since it sounds like it can be a matter concerning the safety of the equipment, I’d implement this check there.

Beyond that Scott and Vincent’s idea of using proxy items are a much. OH Sitemap and Rules send the command to the proxy Item which triggers a Rule and then issues the commands necessary to turn off that which should be off and on that which should be on through the Items linked to the GPIOs. You will probably want a brief (100 msec) pause between turning one off and the other on just to make sure everything got processed. OH is not a real time system. If you have two sendCommands one after the other in a Rule I don’t think you have a guarantee that they will both be processed in that order every time.

Thanks Rich for the feedback.

I believe another approach could be enabling/disabling switches as they turn on/off. Is possible in OH?

For example:

As the Pool Pump (switch1) turns on, enabling pool heater switch (switch2) and spa heater switch (switch3).

Similarly, as Pool Heater switch is on, disabling Spa Heater switch and so on.

Thoughts?

Thanks again!

Yes, again with proxy items:

Switch switch1_enable
Switch switch2_enable
Switch switch3_enable
Switch switch1_proxy
Switch switch2_proxy
Switch switch2_proxy

Only the proxy items are exposed to the UI

rule "Switches"
when
    switch1_proxy changed
then
    switch1.sendCommand(switch1_proxy.state) //Turns pump on/off
    switch2_enable.postUpdate(switch1_proxy.state)
    switch3_enable.postUpdate(switch1_proxy.state)
end

rule "Switch2"
when
    switch2_proxy changed
then
    switch2.sendCommand(switch1_proxy.state) //Turns pump on/off
    Thread::sleep(500) // Ensures no overlap
    if (switch2_proxy.state = ON) {
        switch3.sendCommand(OFF)
        switch3_enable.postUpdate(OFF)
    } else {
        switch3_enable.postUpdate(ON)
    }
end

Sitemap:

Switch item=switch2_proxy visibility=[switch2_enable=ON]
Switch item=switch3_proxy visibility=[switch3_enable=ON]

Awesome. Thanks Vincent!

Let me go and do some OH proxy readying.

You need another rule for switch3… Good luck

Hi Vincent,

Thanks for all your help! I am close to implement what what I wanted to achieve.

One more question. While fine tuning the rules, I am trying to an issue trying to turn on all switches when the pump is turned off.

This code works to turn everything else OFF, but when I turn the pump ON again; those switches that were previusly to the pump off, will be shown as ON. If I tried to modify the proxy switch directly within the rule, it will actually show as OFF, but it will be visible on the screen (last screen image).

rule "Pump ON/OFF Proxy"
when
        Item channel1_proxy changed
then
        channel1.sendCommand(channel1_proxy.state) //Turns pump on/off

        //Turning Off Spa Mode (valve) if Pump if turn OFF
        if (channel1_proxy.state == OFF && channel2_proxy.state == ON) {
                channel2.sendCommand(OFF)
                channel2.postUpdate(OFF)
                // Adding channel2_proxy.postUpdate(OFF) makes this switch visible even when the pump is OFF
                //Spa heater
                channel4.postUpdate(OFF)

        }
        //Turning Off Spa Heater if Pump if turn OFF
        if (channel1_proxy.state == OFF && channel4_proxy.state == ON) {
                // Do something
        }

        channel2.postUpdate(channel1_proxy.state)
        channel3.postUpdate(channel1_proxy.state)

end

oh1
oh2
oh3
oh4

Any suggestion would be greatly appreciated. Thanks again for all your help!

Can you show me your sitemap file, please?

Here it is, it follows your example:

sitemap home label="My home automation" {
    Frame label="Date" {
        Text item=Date
    }
    Frame label="Pool" {
Switch item=channel1_proxy icon="pump"
Switch item=channel2_proxy icon="faucet" visibility=[channel2==ON]
Switch item=channel3_proxy icon="temperature_hot" visibility=[channel3==ON]
Switch item=channel4_proxy icon="temperature_hot" visibility=[channel4==ON]
Switch item=channel5 icon="flow"
}
}

You didn’t quite foolow my example
You need to use the item_enabled switches too

Vicent,

Sure I did, I am only using them inside the rules and thanks for that help!

Note:
Your switch_enable = my channelx
Your switch_proxy = my switchx_proxy

It does work for ordered ON / OFF :slight_smile:

What I am trying to address the use case where switches are OFF out of order i.e. the pump is turn OFF, then I would like all ON switches to be OFF.

I managed to get turn them off but the UI is displaying all switches when the pump is OFF that should be visible only when the pump is ON.

i.e.
Pump OFF --> Spa Mode (not visible)
Pump ON --> Spa Mode (visible)
Pump ON --> Spa Mode ON
Pump ON --> Spa Mode OFF
Pump OFF --> Spa Mode (not visible)
All good and it works. Here is the issue
Pump OFF --> Spa Mode (not visible)
Pump ON --> Spa Mode (visible)
Pump ON --> Spa Mode ON
Pump OFF --> Spa Mode (switch does turn OFF but still visible. Since the pump is OFF this switch shouldn’t be visible)

Hope it is more clear now.

No, you need

channel1
channel1_proxy
AND
channel1_enable (This is only for visibility)

in sitemap:

sitemap home label="My home automation" {
    Frame label="Date" {
        Text item=Date
    }
    Frame label="Pool" {
Switch item=channel1_proxy icon="pump"
Switch item=channel2_proxy icon="faucet" visibility=[channel2_enable==ON]
Switch item=channel3_proxy icon="temperature_hot" visibility=[channel3_enable==ON]
Switch item=channel4_proxy icon="temperature_hot" visibility=[channel4_enable==ON]
Switch item=channel5 icon="flow"
}
}