Rule to link two switches

Hi Everybody,

I’m new to OpenHAB and using it with Insteon devices. I want to programmatically link a switch with a keypad button.

  • When the button is turned ON I want to turn ON the switch.
  • When the button is turned OFF I want to turn OFF the switch.
  • When the switch is turned ON I want the status of the button to be ON
  • When the switch is turned OFF I want the status of the button to be OFF

So I create 4 rules. One of each of the cases.

rule rule1
when
Item KeyPadButton changed to ON
then
sendCommand(Switch, ON)
end

rule rule2
when
Item KeyPadButton changed to OFF
then
sendCommand(Switch, OFF)
end

rule rule3
when
Item Switch changed to ON
then
sendCommand(KeyPadButton, ON)
end

rule rule4
when
Item Switch changed to OFF
then
sendCommand(KeyPadButton, OFF)
end

This kind of worked. But after few tries switches got into an endless loop of getting toggled on and off.

My question is if there is a more civilized way of achieving the functionality? If no, then, how to fix those rules so that the switches don’t toggle infinitely?

Thanks,
Ruben

Hmm, if you press the button, it changes its state to ON and if you release it it changes to OFF. And how should a button turned on? Could you please describe this in more detail?

Please keep in mind that the button press send a command to the switch and this triggers the next rule and with some delays between rule 2 and 3 there might be some endless loops

Thomas

The problem is that when Rule 1 is triggered by the keypad being set to ON, then a command is send to the switch, that command will trigger rule 3 which again will trigger rule 1, which again…

I would create a switch item that is not linked to any device, but instead updated in the sendCommand in your rules.

You can also reduce the number of rules to 1 by removing the “to OFF” and “to ON” and instead just act upon a change in the two items. Inspiartion for rewriting your rule can be found here: https://github.com/openhab/openhab1-addons/wiki/Samples-Rules

Yes it is openHAB 1 but the rule engine hasn’t changed significantly.

Guy, let me rephrase. Basically i want two switches to always have the same state. Either both of them on, or bith of them off. Now using any insteon inter device links and dont want to. Want to achieve this with openhab.

Looked through the rule example and could not find help there. Yes, i can combine two rules into one and check the state, but that would not eliminate the infinite loop.

Thanks

You should write rules that are triggered by received Command
That is an event that is triggered only by input button presses, not changes in the subject Item’s state.

Ok, got it. Looks like I can make one rule to be based on received command and another based on received update to break the loop.

I have one more question. Is there a property like “DesiredState” available? Lets say the state of a switch is OFF and I want to turn it on by sending the command ON. Until the confirmation of the switch state change is delivered the state of the switch will remain OFF, right? Is there another property which will hold the state ON, meaning that within OpenHab the last command to set the state was ON.

Nope. You can manage that with a proxy “shadow” item, but your primary item can only either reflect what it was told to do (autoupdate=true) or what it actually does (autoupdate=false)

The other option, and the one that I use, is instead to use the postUpdate function for the item you want to control.

This is how I do it:

rule rule1
when 
    Item KeyPadButton received command
then
    if (Switch.state != KeyPadButton.state)
    {
        Switch.postUpdate(received command)
    }
end



rule rule2
when 
    Item Switch received command
then
    if (KeyPadButton.state != Switch.state)
    {
    KeyPadButton.sendCommand(received command)
    }
end

That eliminates all the if statements, streamlines the code execution, and reduces the number of rules you need. And it keeps everything perfectly in sync. I’m using Insteon switches and keypads all over my house and this is how I keep everything in sync.

The key difference is that commands and updates are two different things. A device and receive an update without receiving a command, but if it receives a command it automatically receives and update.

Hope that helps!

2 Likes