@5iver OK this issue came up in many different scenarios, but here’s one example. I have an item:
String Button_Click { channel="mqtt:topic:mosquitto:mybutton:click" }
This button click can receive an update of single
, double
, hold
, release
(via mqtt). So if I press the button it will send an update “single”. If I press it again the second time, it will send another update “single”.
So in this instance, I cannot use the trigger “changed” and need to use “received update” instead.
Hence my rule would be:
@when("Item Button_Click received update")
def handle_button(event):
if event.itemState.toString() == 'single':
if items.MyLight == OFF:
sendCommand('MyLight', 'ON')
else:
sendCommand('MyLight', 'OFF')
elif event.itemState.toString() == 'double':
# do something else here
The problem is sometimes I fiddle with the .items file and cause a syntax error. When this happens, the item “Button_Click” gets wiped off from openhab, and its link to the channel is removed by openhab.
Then when I fixed the syntax error and saved the .items file, OpenHAB would load the item and re-link it with the channel.
At this point, I will see in Karaf log: Item Button_Click changed from NULL to single
- because the channel still holds the value ‘single’ from the last time I pressed the button, so it sets the item to that value. This does not represent an actual button press though.
However, this does trigger my rule, as a result it thinks a button is pressed, and it will toggle the light - remember that this is caused by the reload of the item, not because the button is actually pressed.
Suffice to say that this caused havoc around the house whenever I played around with the items file past midnight.
I don’t think I can change my trigger to “Item Button_Click changed” because if I press the button again and again, the value of “Button_Click” item doesn’t change. The value remains as “single” so the changed trigger would not fire.
My current workaround is changing the trigger type to “change” instead of “received update” but then I would update the item state to a blank string, just so that I can detect the “change” if another “single” is received.
So this is my hack / workaround:
@when("Item Button_Click changed")
def handle_button(event):
command = event.itemState.toString()
postUpdate(event.itemName, '')
if isinstance(event.oldItemState, UnDefType):
return # ignore if the oldstate is null
if command == 'single':
if items.MyLight == OFF:
sendCommand('MyLight', 'ON')
else:
sendCommand('MyLight', 'OFF')
elif command == 'double':
# do something else here