Bidirectional rule creates a loop

Hello all,
I need some help while creating a bidirectional connection between my PLC/HMI and my audio receiver to control the volume.
The receiver is binded to openHAB 2.5 using the YamahaReceiver Binding and my PLC is connected using the Simatic binding.
At the PLC side I have a slider on the HMI with an integer in a DB. When I move the slider the integer value changes and when I change the integer the slider moves. This integer is connected to OH with the following item:

Number  RC1MZ_Volume  "PLC Receiver 1 Main Zone Volume"  { simatic="plc1:DB821.DBW0:word" }

The receiver is using this item:

image

These are the rules:

rule "Versterker Volume > PLC"
when
	Item Receiver1MainZone_Volume changed
then
	if (newState != NULL) {
	    RC1MZ_Volume.sendCommand(newState.toString)
	}
end

rule "PLC > Versterker Volume"
when
	Item RC1MZ_Volume changed
then
	if (newState != NULL) {
	    Receiver1MainZone_Volume.sendCommand(newState.toString)
	}
end

When I activate one of both rules the connection works in 1 direction or the other. When I activate both rules and move the slider from 20 to 30% they end up in a loop and every 1 or 2 seconds the volume changes from 20 to 30%, back to 20% and so on till I remove 1 of the rules.
How can I avoid the loop?

There’s a whole bunch of stuff that can go wrong with rule-pairs like this.
Timing factors, command-response delays. Interim responses, like a device that initially with responds with “state now” and then later “new state” - especially a problem if either implements any ramping effect.
Or mismatches in “granularity” of the devices, say one goes in 2% steps and one in 5% steps, leaving setting that can never match.

But there’s a third invisible actor at work here too - openHABs autoupdate feature is likely changing Item states in response to commands. In the first case, I would disable autoupdate on both your Items to reduce the complexity.

A snippet from your events.log will help follow along what is happening.

Hi Rossko,
Thanks for your reply. With playing with the autoupdate function the loop problem is solved, however it’s still not working the way I want.
When I’m watching televison and put the volume up (or down) a volume bar appears on the TV screen and disappear in 5 seconds as normal. Now the rule to update the slider position on my HMI is executed, still according plan. But now the other rule is activated as a result of changing the slider value. The anoying effect is that the volume bar on the TV screen pops up for another 5 seconds.
Is there a possibility to block the 2nd rule for 10 seconds after the 1st rule is executed and vice versa?
As you can see I’m a beginner with OH and cannot find any examples in solving this issue.
Thanks,

Ed.

Sure, there are timing functions for rules.
You might use a shared global variable to hold a handle for a timer. then when when you send a command start the timer. At the expiry time, timer nulls itself.
The rules should check to see if the timer is running before taking action.

General timer usage

Note, this will immediately block the kind of repeated commands you might expect from dragging a slider, and you might instead want a two-stage timer that for a period still allows commands but then blocks them for a second period.