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?
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...).
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:
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.