Toggle Switch Rule has strange behavior

  • Platform information:
    • Hardware: Raspberry Pi4
    • OS: Openhabian
    • Java Runtime Environment: which java platform is used and what version
    • openHAB version: 4

Toggle rule does not work. I found many examples but none of them works for me.
The first ON works and the first OFF. But after that it switches ON and immediately OFF again…

  • Please post configurations (if applicable):
    I tried thes Rules:
rule "Flur 2OG Licht an"
	when
  		Item MQTTHueWallSwitch1Action received update // to "left_press_release"  
  	then
	if (MQTTHueWhiteColor1Switch.state == ON) 
          {
               MQTTHueWhiteColor1Switch.sendCommand(OFF)
         }
	else if (MQTTHueWhiteColor1Switch.state == OFF) 
          {
               MQTTHueWhiteColor1Switch.sendCommand(ON)
         }
end 

and:

	rule "Flur 2OG Licht an"
	when
  		Item MQTTHueWallSwitch1Action received update // to "left_press_release"  
  	then
               MQTTHueWhiteColor1Switch.sendCommand(if(MQTTHueWhiteColor1Switch.state == ON) OFF else ON) 
	end

Both have the same behaviour.

As i see lots of examples with these rules, i believe it works somehow.
Any Tips would be great.

Thnx in advance

Have a look into the differences of “receive update” and “change”

Hi, the item state that triggers the rule does not change. It just updates when triggered.
I assume it runs through both IF and IF ELSE and thats why it goes on and off immediately. I just dont know why…

Add some logging prior to the if statement to see how often the rule is running.

I’ll second @Matze0211 suggestion to make sure you understand the difference between a command, a change, and an update.

As written, this rule will trigger any time the Item is updated by OH. What isn’t known but very relevant is whether that Item gets updated when you send a command to the Item inside the rule. If so the rule will trigger, command the Item, then trigger again when that command is processed causing the light to toggle off again.

A received command trigger would be more appropriate perhaps, but all these Items have MQTT in the name so :person_shrugging: It all depends on how the messages flow.

Thnx for your quick answers.
The rule triggers twice! But why? The trigger item does not get updated when commanding the ON/OFF item. Maybe the trigger item itself always triggers two times.
Need to review this. Will come back here again :slight_smile:

As your item is named mqtthue… I assume you have a ZigBee device connected via zigbee2mqtt

There is a debounce option in z2m, look at this.

Hi, update on this topic.
After seeing that the switch starts the rule twice i did some more searching. At the end i found out that the status of the trigger gets updated with ‘left_press’ and ‘left_press_release’.
The rule fired twice when using

when
  		Item MQTTHueWallSwitch1Action changed to "left_press_release"  

Changing this to

when
  		Item MQTTHueWallSwitch1Action changed to "left_press"  

it works fine.

As i like to find out what the reason is for such a behavior, could it be that it takes ‘left_press’ and ‘left_press_release’ for the same?
From the Logic, the default state of the trigger is ‘left_press_release’. When triggered it changes to ‘left_press’ and then back to ‘left_press_release’. So it changes two times, but only one time it changes to ‘left_press_release’. If i had just ‘changed’ i would understand why. But ‘changed to “xxx”’ should not trigger the rule twice.

Strange…

Anyhow, lots of thanx for those who commented.