How to do calculations using items of type Number:Temperature in custom widgets of main-ui


I have got a item which is defined as “Number:Temperature”. In my custom widget I want to calculate the difference between that item and a constant. If I try to get the state of the item, the result is not a number but a string, because of the item’s definition as “Number:Temperature”.


{ "state": "21.48 °C", "displayState": "21,5 °C" }

I’m wondering, how to use the state result for doing calculations. Using expressions in widgets there is no possibility of splitting the string to number, or am I wrong? The result of following operations will always be NaN, and not the difference between the temperatures.

=22.0 - items['KNXHeizEGKucheIstTemperatur'] --> NaN
='22.0' - items['KNXHeizEGKucheIstTemperatur'] --> NaN
='22.0 °C' - items['KNXHeizEGKucheIstTemperatur'] --> NaN

Will I have to define my temperature items only as type of “Number” instead of “Number:Temperature” if I want to do calculations in widgets or does anyone know another solution?


You can split string in widget expressions using the js string split() method. Once you have just the number part of the string you still need to cast that to a number type for calculation. So, the final result looks something like this:

=22.0 - Number(items['KNXHeizEGKucheIstTemperatur'].state.split(" ")[0])

*Typed on phone, errors possible

I’ve been told that parseInt and parseFloat simply bails on the first non-number character. So you should be able to just parse the state as is without the split. I think I’m successfully doing that in one of my widgets.

1 Like


=22.0 - Number.parseFloat(items['KNXHeizEGKucheIstTemperatur'].state)

will also work.

Okay, thanks. I’ll try that tomorrow.

Yes, that’s it. I’m was not sure which JS operations are possible in expressions. A few days ago I tried to determine the client screen size in a expression, but didn’t manage to access a screen object.