Change switch state based on contact

  • Platform information:
    • Hardware: RPI 4 / 2 G RAM
    • openHAB version: 2.5.2
  • Issue of the topic: I need to change the switch state based on contact state

I need a little help, please.
My setup:
I use a mcp23017 connected to a 8 channel relay board witch command the lights, but I also use the actual wall switch (3 way). I also use this board to read actual light status.

Switch living_room_light_switch_A5 "Living Room Light" ["Lighting"channel="mcp23017:mcp23017:chipA:output#A5"}

Contact living_room_light_status "Living room Light Status " {gpio="pin:10 debounce:10 activelow:yes"}

and I created a rule:

rule "LivingRoom light status"
        Item living_room_light_status changed
    if (living_room_light_status.state == OPEN){
    if (living_room_light_switch_A5.state == OFF){
    postUpdate(living_room_light_switch_A5, ON)
    if (living_room_light_status.state == CLOSED){
    if (living_room_light_switch_A5.state == ON){
    postUpdate(living_room_light_switch_A5, OFF)

My problem:
with this setup, if the light is switched ON from the wall switch, then I have to press twice in OH to switch.
Can anyone help me please?
Thank you.

Not enough to work with.

Tell us about this relay board - this is controlling the lights directly, or controlling further relays? Impulse relays?
Which device does the wallswitch control act upon?

Thank you for the quick reply.
Yes, the relay is controlling the lights directly and acting as a 2 way switch together with the wall switch(also 2 way switch).
The “Optocoupler” board is reading the actual status of the light bulb (acting as contact).
I tried to make a schematics. :grin:schematics

It works great, but if I switch the light from wall, the OH dose not know, that’s why I use the “Optocupler” board, to read the actual status of the light, but with my rule, when I switch the light from the wall, it changes status, but I have to press twice in OH to execute switch.

Your problem is that an indication of your relay being ON does not mean your light is on - because of your two-way switching.
Equally, commanding your relay ON might result in the light turning on or off, depending on the wall switch position.

I’d abstract this.
Make a proxy Switch Item, this is what to use on your sitemap. You have to use a Switch here, because you cannot send commands to Contact.
A rule like you already have to reflect the actual state of the light into that proxy Item (not the relay control).
Another rule that listens for commands to proxy, and only if the command is not the same state as actual, changes the state of the relay control.

EDIT - I forgot, I would set autoupdate false on the proxy, so it reflected reality not last command.

1 Like

OK. I’l try that approach and let you know how it works. Thank you.

EDIT- I followed your advice.
I created an Item

Switch living_room "Living Room" {channel="autoupdate=false"}

and the following Rule

rule "LivingRoom proxy"
                Item living_room received command
        if(living_room_light_switch_A5.state != ON) living_room_light_switch_A5.sendCommand(ON)
        if(living_room_light_switch_A5.state != OFF) living_room_light_switch_A5.sendCommand(OFF)

rule "LivingRoom status"
                Item living_room_light_status changed
        if(living_room_light_status.state == OPEN){
        if(living_room_light_status.state == CLOSED){

It works great.
Thank you.

1 Like