Updating the UI

I’ve been adding some new fuctionality to an existing binding - support for a new Thing. I’ve got the command side working ie I flip a switch on paperUI, the command gets sent and the Thing does its thing. The Thing sends back a status which I have parsed. The bit I am missing is how to I update the controls on paperUI from the returned status?
Perhaps some kind soul can point me to ab example?

Thanks,
Steve

I assume you are asking how to update the state of a channel in your ThingHandler because the Paper UI should update on its own when the state of a channel changes.

If your ThingHandler is extending BaseThingHandler you can use these methods:
BaseThingHandler#updateState(ChannelUID channelUID, State state)
BaseThingHandler#updateState(String channelID, State state)

If you are not extending BaseThingHandler you can use this method:
ThingHandlerCallback#stateUpdated(ChannelUID channelUID, State state)

N.B. State is a marker interface you will actually use something like OnOffType or DecimalType.

e.g.:

public class MyThingHandler extends BaseThingHandler {
    // handleCommand etc. not shown

    public final void updateState() {
        final double batteryVoltage = getBatteryVoltage();
        this.updateState("battery-voltage", new DecimalType(batteryVoltage));

        final boolean powerOn = getPowerOn();
        this.updateState("power", OnOffType.from(powerOn));
    }
}
1 Like

Hi Ross, thanks for the reply.

The idea I was not sure about was that the UI will automatically update it the channel updates.

The binding I’m working on does extend the BaseThingHandler but not as directly as in your example. I think that the ThingHandlerCallback might work for me though. I’m try it out.

The UI should always update when the channel state updates. If it doesn’t something has probably just gone a bit wonky and a browser refresh should fix it.

You don’t have to directly extend BaseThingHandler to use its methods, just inherit from it at some point.

The BaseThingHandler methods use the ThingHandlerCallback method under the hood, they are just convenience methods so you don’t have to chain method calls: this.getCallback().stateUpdated(...);

So just use the method that is the most convenient for you, they all work out the same in the end :slight_smile:

1 Like