[SOLVED] Need Help Creating a Virtual Three-Way Switch (TP-Link) with Rules

Hi all,

I am running OH 2.2 on DS218+ with DSM 6.1.7-15284. I have two tp-link HS200s switches installed in a room. One of them, carries the load for the lights, and the other switch has no load at all (just installed as a dummy). I need to figure out how to make the two switches update to the proper state when one is changed e.g. turn switchA on, and switchB’s state automatically updates to match switchA, and vise versa. I have yet to write a successful rule for this and cannot seem to figure it out any other way. I tried assigning both things to a single switch item but that just creates and endless loop of on-off-on-off.

I think I could probably figure out the correct pattern for writing the rule IF i knew what commands to use to retrieve and update switch states.

Any help would be greatly appreciated as this has been an issue of mine for several weeks now.

Thanks!

Hello @Ravenswift

Have a look at:

Have a go at writing your rule and if you get stuck, post your code here.

rule "switch1"
when
	Item switch1 changed
then
	if (switch2.state != switch1.state) {switch2.postUpdate(switch1.state)}
end

rule "switch2"
when
	Item switch2 changed
then
	if (switch1.state != switch2.state) {switch1.postUpdate(switch2.state)}
end
1 Like

Look OK to me. Does it work for you?

That’s about what i had before. It’s the right idea but something is missing because it still created that endless loop of switching themselves on and off. maybe if there was some kind of delay command between the two?

I will assume that Switch1 carries the load.
Try that:

rule "switch1"
when
	Item switch1 changed
then
    val switch1state = switch1.state
    switch2.postUpdate(switch1state)
end

rule "switch2"
when
	Item switch2 changed
then
    val switch2state = switch2.state
    switch1.sendCommand(switch2state)
end

I belie e this will still create a loop. You need to set some type of flag so you don’t go through the second rule.

I had this problem also with some lights unfortunately I don’t think you can fix the way I did with the lights. Prior to fixing i was w tiring a rule something like this.

If sw1 or sw2 changed
If sw1 changed Set sw1 change flag
If sw2 changed Set sw2 change flag
If sw1 change flag then deal with sw2 ***logic from above
If sw2 changed flag then deal with sw1***logic from above
End

This is not pro per syntax but a suggestion of a flow i think will work. You have to check if either changed then nest the other stuff inside that.

Danny I think that flow would also work but vzorglub what you posted there seems to be working. just need to go down and see if it actually turns the lights. I like what you did there and I see how it works now. I think it would have taken me a very long time to figure that out, if ever! Thanks!

Okay actually I just noticed that the loop doesn’t start, BUT if I change the state of switch1, then switch2 changes to match the state of switch1, then switch2 changes back to what it was before, and then switch1 changes back to match the beginning state of switch2. but it stops there.

on the other hand if i change the state of switch2, it works perfectly.

other than that. no loop.

I know why
The Switch2 tp link changes state after the postUpdate but then the binding puts it back to where it was.

Try that:

rule "switch1"
when
	Item switch1 changed
then
    val switch1state = switch1.state
    switch2.sendCommand(switch1state)
end

rule "switch2"
when
	Item switch2 changed
then
    val switch2state = switch2.state
    switch1.sendCommand(switch2state)
end
1 Like

strange that the binding puts it back to where it was. (or is it not strange and im just stupid?). thanks for all your help!!

That’s what the binding is supposed to do. Give feedback to OH of its actual state. Since we only update the state, the binding will shortly after (depending on refresh rate) update the item with its actual state. Does that make sense?
By doing sendCommand we change the state of the actual switch on the wall not just the state in OH.

Does it work now?

1 Like

ohhh okay I understand. yeah that makes perfect sense. yep! it works perfect! thank you so much! maybe this little bit of knowledge will help me write any future similar rules i do!

Please mark the solution. Little trick mark under the correct post. Thanks

No problem, I was looking for that earlier. Thanks again!