How do I properly update the channel state in a binding?

Hi developers,

I’m a bit confused how to properly update a channel state in my binding, maybe some of you experts can help me to do that right :slight_smile:

The binding communicates to a wifi device via simple UDP messages.
The device has 3 or 8 relays which may be switched on and off, if they are not locked.

My ThingHandler has an internal state which caches the state of each relay (and whether they are locked or not).
On Thing initialization, I request the states of all relays from the device and store them in the internal state.

If the device changes the state, the ThingHandler gets notified via a UDP message, updates its internal state, and calls updateState(channelUid, newState).
That works fine, the linked items are updated.

If the ThingHandler receives a command to change the state (let’s say from OFF to ON), I’m not sure how to proceed correctly.
There are two cases:

  1. The relay is not locked.
  • Then the ThingHandler sends a UDP message to the device to switch the relay on.
  • The internal state is unset because we are waiting for a response from the device whether or not switching the relay was successful.
  • If I understand it correctly, the default autoupdate behavior automatically changes the linked item states to ON?
  • As soon as the ThingHandler receives the confirmation from the device, the internal state for that relay can be set to ON and the channel state can also be updated to ON. (which has no effect because autoupdate already set the state to ON, right?)
  1. The relay is locked.
  • Then the ThingHandler should not send anything to the device but it should, simply speaking, reject this command. That is, the channel state should remain OFF.
  • However, autoupdate still changes the linked item states to ON, right?
  • I tried to updateState(channelUid, OFF), but it does not work. The linked item is still updated to ON.
  • I tried it async: scheduler.execute(() -> updateState(channelUid, OFF)), but the linked item is still updated to ON.

How should a ThingHandler properly update the channel in such cases?

Btw, because of the fact that relays may be locked, I set the channel’s autoUpdatePolicy to ‘veto’.
It that the intended configuration for this scenario?
How does that affect how the ThingHandler should react on commands and set channel states?

I do not think you can dynamically change the autoupdate veto policy, which would reduce your options.

There’s two approaches here;
A. How the user sets up their Item (including autoupdate on/off) is none of the binding’s business. You can of course offer advice in your readme.
B. “The binding knows best” because the results of an Item command are unpredictable (locked, not locked). This would assume your binding always updates to best known state.

Generally it’s best not to force your users into a particular use or second-guess what else they may doing with their Item.

Thank you for your response!
I would say B. would provide the most precise behavior.

But how should a ThingHandler be implemented for the two cases above to properly update the state?
And what should the autoUpdatePolicy be?

Update:
I would have expected that autoUpdatePolicy=veto does not automatically set the channel state automatically when a command is received but apparently it does?!

Okay, it seems as if I didn’t check the log properly. Channel state updates are not sent, but the Web UI itself updated the widget without waiting for the state update… :roll_eyes:
So I assume that the ‘veto’ update policy does work as expected :slight_smile: