How to read the current state of a channel within a ThingHandler

I’m working on implementing a new binding to a lighting system. I have a lot of dimmers which can be controlled through a hardware device that I can send/receive commands from using the serial port.

The system is limited in that I can query for the on/off state of each dimmer but cannot get the dimmer level from the system. I can send a message to the system to set a specific dimmer level and also to turn a dimmer on/off which will turn the dimmer on to the previous state.

I’m trying to figure out the best way to keep openhab up to date with the state of the dimmers. The system sends a message with the on/off state of every dimmer whenever any dimmer state changes. My binding handles this message and uses the updateState method in the ThingHandler to update openhab. Its easy when a dimmer is set to off since I can just set the dimmer percentage to 0. I can also do something similar for when a dimmer is on and set the percentage to 100, but this can lead to some inconsistency with the physical state which I’m trying to avoid.

Here’s an example

If I previously used openhab to set dimmer1 to say 70% and then I toggle on a different dimmer, say dimmer2, I will get a message that tells me dimmer1 is on and dimmer2 is on. If I use updateState to update the dimmer level in openhab based on these messages, I end up setting the dimmer to 100% on dimmer1 even though it didn’t really change.

Is there a way to read the current state of a channel within a ThingHandler? I’m thinking I can check if the current state of the dimmer level is greater than 0, then I won’t change it in openhab when the message tells me the dimmer is on.

Is this possible? I’m new to openhab so maybe there is a better way to handle this entirely.

Thanks in advance

You could store the state of your dimmer in a map. When a message is received, just compare if the state changed and send an update within openhab. If the state did not change, do nothing.

Thanks I took your advice and hooked into the handleUpdate method to keep track of state changes and was able to accomplish what I wanted.

Seems odd that there isn’t an API to read the state within a ThingHandler but this seems to work fine for my use.