Problem with rule and Sonoff switches

Hello,

I am new in OpenHAB and home automation. Have one problem with Sonoff T1 and Sonoff POWR2.
POWR2 is inside in boiler so I can not reach ON and OFF button so i put on wall Sonoff T1 which is acting as proxy switch, physically is not connected to boiler.
Now can turn on boiler with BASIC UI and on switch button and also OFF.
Sonoff POWR2=BathRoomStuff
Sonoff T1=BathRoomBoiler

Problem is when suddenly press on Sonoff T1 on off on off switch start by itself blinking and turning on and off all the time, dont know it looks like some kind of loop. Then need to cut power on circut breaker to stop looping.
Can you help me with tips, i assuming something is wrong in rule code.

Added two rules:
rule “xyz”
when
Item BathRoomStuff changed
then
if(BathRoomStuff.state == ON)
sendCommand(BathRoomBoiler, ON)
else
sendCommand(BathRoomBoiler, OFF)
end

rule “xyz1”
when
Item BathRoomBoiler changed
then
if(BathRoomBoiler.state == ON)
sendCommand(BathRoomStuff, ON)
else
sendCommand(BathRoomStuff, OFF)
end

Continuing the discussion from Problem with rule and Sonoff switches:

I think you’re building yourself a loop here
You change BathroomRoomStuff that changes state of BathRoomBoiler (rule xyz)
the state on BathRoomBoiler has changed this means xyz1 triggers which means BathRoomStuff changed so xyz triggers and on and on and on

Yep. Your basic idea is sound, to copy changes from one device to another, wallswitch to boiler.
It’s not clear why you need to copy back the other way, but it’s not an unreasonable thing to do - the boiler might have some other timed control say, and you want the wallswitch to reflect the boiler state.
That’s a loop, but it should only go round one time.

Of course it all takes a finite time to do - I’d guess your problem comes along if you toggle the wallswitch quickly before the “loop” is complete. Each “half” then tries to update the other half in turn.

It’s difficult to avoid that, because you cannot eliminate the time delay.

First guess, add a blocking mechanism - don’t allow any more changes while we’re going once round the loop.
We can also improve by removing pointless commands.

// acts as a blocker to prevent multi command before complete
// have it self-expire to prevent total lockups
var Timer blockchange=null

rule "xyz"
when
   Item BathRoomStuff changed
then
   if (blockchange == null) {   // only do if not blocked
      if (BathRoomStuff.state != BathRoomBoiler.state) {   // only do if don't match
            // make a blocker that we expect to cancel later
          blockchange = createTimer(now.plusSeconds(10) | [
                 // unexpected expiry, better tidy up
              blockchange = null
           ] )
             // now do the wanted change
           if(BathRoomStuff.state == ON) {
                BathRoomBoiler.sendCommand(ON)
           } else {
                BathRoomBoiler.sendCommand(OFF)
           }
      }  // else already in that state
   }  // else blocked, don't pass on
end

rule "xyz1"
when
   Item BathRoomBoiler changed
then
   if (BathRoomBoiler.state != BathRoomStuff.state) {
      if (BathRoomBoiler.state == ON) {
         BathRoomStuff.sendCommand(ON)
      } else {
         BathRoomStuff.sendCommand(OFF)
      }
   }  // else already same
     // now let's cancel any blocker, because we did change
   blockchange?.cancel
   blockchange = null
end

rossko57 thank you very much.

Solved