Show always positive value of number item

[openHAB 4.2.1; Docker on Unraid; Main-UI]

Question
I have a number item (Power (W)) that can have negative values. Is it possible that a widget in Main UI always shows the values as positive numbers?

Examples:
Value=120; shown in widget=120
Value=-245; shown in widget=245 (no minus!)

Why?
I just integrated my photovoltaic system in my openHAB installation and can now read the current data (power production, power consumption, battery, …). Some of those items can have negative values. E.g. when I load the battery the value for “battery consumption” gets negative. Due to this behavior the negative value is also shown in the widget I use. But I want that always a positive value is shown (see screenshot below).
Important: The postivie number should only be shown, but internally it should still be a negative number, so that the negative value gets stored to the database.

Is the any configuration (State Description??) for this?

pv

Thanks
Stefan

In a custom widget such as the one shown, it is easy to change the display of the value. Wherever you have the reference to that item’s state or display state you can just use the Math.abs(...item reference...).

1 Like

Thanks for your anwser @JustinG. Unfortunately I didn’t get it to work :frowning_face:

Original code in widget:

text: =items[props.item37765].displayState

changed:

text: =Math.abs(items[props.item37765].displayState)

then nothing is displayed at all (see screenshot):
pv2

Whats wrong with the code?

Thanks
Stefan

Sorry, if you are using the displayState then that is a string that is incompatible with the Math methods. In this case, a brute force approach is probably easier and you can just eliminate the negative sign with a replace:

text: =items[props.item37765].displayState.replace('-','')

A better way of handling this if you want to use the math method is to get the number and the unit separately:

text: =Math.abs(items[props.item37765].numericState) + " " + items[props.item37765].unit
1 Like

Thanks again @JustinG :grinning:

With your second suggestion (which I think is the better one, btw) I loose the automatic unit conversion possibility (W → kW) that is defined in the items State Description, right?

Due to that, I’ll go with your “brute force” advice.

Thanks
Stefan