Two switches control one item

Hi,

I want to have both a physical and virtual switch control one item in my configuration. I have an Arduino capacitive touch wall plate with two sensors that publish on an MQTT topic each sensor state. The Arduino publishes on two separate topics for each touch sensor based if they are on or off. Based on this on or off state, there is also an LED under each sensor that lights to notify the user the device is switched on. I also want to control the same device with a switch on the OH dashboard. I am having trouble getting both switches to sync with each other. I want it so that:

  • When physical switch (capacitive touch) is turned ON → Device is turned ON and switch on OH dashboard is ON

  • When virtual switch (OH dashboard) is turned ON → Device is turned ON and LED on wallplate is ON

  • When physical AND virtual switches are both OFF → Device turns off

This last condition is the most complicated of all because the two above rules will make it so that when both switches are on and one is switched to off, it will immediately turn on again. I have included my .items and .rules files below for reference to what I have setup.

Any input is greatly appreciated!

.items

Switch room1SwitchAVirtualControl “Switch” [ “Lighting” ] {mqtt=“>[mosquitto:/home/room1/switchA/virtualIn:command:ON:1],>
[mosquitto:/home/room1/switchA/virtualIn:command:OFF:0]” }

Switch room1SwitchBVirtualControl “Switch” [ “Lighting” ] {mqtt=“>[mosquitto:/home/room1/switchB/virtualIn:command:ON:1],>[mosquitto:/home/room1/switchB/virtualIn:command:OFF:0]” }

Switch room1SwitchAPhysicalControl {mqtt=“>[mosquitto:/home/room1/switchA/physicalIn:command:ON:1],>[mosquitto:/home/room1/switchA/physicalIn:command:OFF:0]” }

Switch room1SwitchBPhysicalControl {mqtt=“>[mosquitto:/home/room1/switchB/physicalIn:command:ON:1],>[mosquitto:/home/room1/switchB/physicalIn:command:OFF:0]” }

Switch room1SwitchA {mqtt=“>[mosquitto:/home/room1/switchA/out:command:ON:1],>[mosquitto:/home/room1/switchA/out:command:OFF:0]” }

Switch room1SwitchB {mqtt=“>[mosquitto:/home/room1/switchB/out:command:ON:1],>[mosquitto:/home/room1/switchB/out:command:OFF:0]” }

.rules

rule Room1SwitchAVirtualChanged
when
	 Item room1SwitchAVirtualControl received command	 
then
	if (room1SwitchA.state != room1SwitchAVirtualControl.state) {
    	if(room1SwitchAVirtualControl.state == ON) {
    		room1SwitchA.state = ON
    	}
    	else {
    		room1SwitchA.state = OFF
    	}
	}
end
rule Room1SwitchAPhysicalChanged
when
	 Item room1SwitchAPhysicalControl received command
then
	if (room1SwitchA.state != room1SwitchAPhysicalControl.state) {
    	if(room1SwitchAPhysicalControl.state == ON) {
    		room1SwitchA.state = ON
    	}
    	else {
    		room1SwitchA.state = OFF
    	}
	}
end

Your best bet is to use an Proxy Item to represent the state of the switch in openHAB based on the states of the Physical and virtual control switches.

Also, see bob_dickenson’s original posting for how he applies this approach.

The Proxy Item lets you avoid feedback loops and the problem you are having.