Rounding using displayState on Number:Temperature items using Units of Measurements

  • Platform information:
    • OS: latest stable docker container
    • openHAB version: 4.1
  • Issue of the topic: Sometimes displayState is not shown correctly

Hello,
previously I used the math rounding function and added “°C” manually to the label text in widgets and cells.

With the Units of Measurement concept I defined now all the items with the according unit, e.g:

Number:Temperature Raumtemperatur_EG_WC "EG WC Temperatur [%.1f %unit%]"        <temperature> (gRaumtemperatur, gAvgRaumtemperatur)  ["Measurement", "Temperature"] { channel="knx:device:bridge:rtemp:Raumtemperatur_EG_WC", unit="°C"}
Number:Temperature Raumtemperatur_EG_Wohnen "EG Wohnen Temperatur [%.1f %unit%]"    <temperature> (gRaumtemperatur, gAvgRaumtemperatur)  ["Measurement", "Temperature"] { channel="knx:device:bridge:rtemp:Raumtemperatur_EG_Wohnen", unit="°C"}

[%.1f %unit%] should display the temperature value in °C with one digit after the comma.

In a simple widget I use:

  label: =items[props.vItemTempIst].state

As expected the rounding is not applied as the state is used:

As you can see the OG Büro value has too many digits.

Thus I modified the label in the custom widget to:

label: =items[props.vItemTempIst].displayState

On some widget instantiations the value is not displayed:

The value of “OG Bad” is undefined.
In the console:

openhab> openhab:items list Raumtemperatur_OG_Bad                                                                                                                                                             
Raumtemperatur_OG_Bad (Type=NumberItem, State=22.1 °C, Label=OG Bad Temperatur, Category=temperature, Tags=[Measurement, Temperature], Groups=[gRaumtemperatur, gAvgRaumtemperatur, Bad])
openhab>

This behavior is sporadic.
Any Idea how I can debug this further?

You’ll have to setup the state description pattern instead of doing it in the label:

Number:Temperature Raumtemperatur_EG_WC "EG WC Temperatur"        <temperature> (gRaumtemperatur, gAvgRaumtemperatur)  ["Measurement", "Temperature"] { channel="knx:device:bridge:rtemp:Raumtemperatur_EG_WC", unit="°C", stateDescription=""[pattern="%.1f %unit%"]}
Number:Temperature Raumtemperatur_EG_Wohnen "EG Wohnen Temperatur"    <temperature> (gRaumtemperatur, gAvgRaumtemperatur)  ["Measurement", "Temperature"] { channel="knx:device:bridge:rtemp:Raumtemperatur_EG_Wohnen", unit="°C", stateDescription=""[pattern="%.1f %unit%"]}

The state description is only applied to display state, not to state.
If state = displayState, then the display state is not available for performance reasons.

Theirefore in your widget you have to check if display state is available, otherwise use state

Yep, and this is common enough that there’s a shortcut for it in the widget expressions:

label: =@props.vItemTempIst
2 Likes

Hello Udo,
I changed the item configuration and added a state description:

Number:Temperature Raumtemperatur_EG_Essen     "EG Essen Temperatur"     <temperature> (gRaumtemperatur, gAvgRaumtemperatur, Esszimmer)    ["Measurement", "Temperature"] { channel="knx:device:bridge:rtemp:Raumtemperatur_EG_Essen", unit="°C", stateDescription=""[pattern="%.1f %unit%"]}
Number:Temperature Raumtemperatur_EG_Zimmer    "EG Zimmer Temperatur"    <temperature> (gRaumtemperatur, gAvgRaumtemperatur)               ["Measurement", "Temperature"] { channel="knx:device:bridge:rtemp:Raumtemperatur_EG_Zimmer", unit="°C", stateDescription=""[pattern="%.1f %unit%"]}
Number:Temperature Raumtemperatur_OG_Diele     "OG Diele Temperatur"     <temperature> (gRaumtemperatur, gAvgRaumtemperatur)               ["Measurement", "Temperature"] { channel="knx:device:bridge:rtemp:Raumtemperatur_OG_Diele", unit="°C", stateDescription=""[pattern="%.1f %unit%"]}

I am still getting sometimes incorrect displayStates:

So it is recommended to use state descriptions instead of having it in the item text?

Hello Justin,
thank you.
This solves the problem with the sporadic undefined displayStates. I am just wondering why it happens that the state is valid but not the displayState.
BR
Marco

The state description is something which is under the hood. I am not sure when it actually got introduced, probably somewhere around 2.4 (?) and it partially took over the item label role. Still, for compatibility reasons item label is considered. I don’t remember exact details, but I remember stateDescription concept been present in OH 3.0 already.

This was an intentional decision on the part of the main UI designers. There is a lot of information that is passed from the server to the widgets and one place to help optimize UI performance is to not send duplicate data. So, any item with state description metadata will have both a state and display state value sent to the widget UNLESS those values are identical. In that case, the display state is not sent.