openHAB - negative value transformation

  • Platform information:
    • Hardware: Laptop
    • OS: Windows 11
    • openHAB version: 3.4.3
  • Issue of the topic: getting negative value from PV-installation - would like to convert it to positive value before showing on a label

I am retrieving the power generation of my PV-installation. So far so good. I am getting (absolutely logical) negative values. Again, so far so good. Now I would like to change this value into a positive value before showing it on a label.

I am a openHAB newbie - old but still willing to learn.

I searched the internet for hours, and also this community - but didn`t find a solution which I understand or beeing able to implement.

I hoped that there is a solution which can be easily implemented while working in the UI-mode - without a need to create extra files nor programing.

Any help would be very much appreciated.

Regards
Mausepaul

Do you mean only change the value for display or actually change the state of the item?

Also, display where? Autogenerated widgets, custom widgets, sitemaps?

I mean only change the value for display - no state change is planed.

I would like to create a widget just for showing this value on my overview page. it is currently running fine - just showing the (correct) negative value. Just for my “convenience” I would like to show the value as positive value.

root square of the square…

Andreas,

thanks for your valuable reply… Or was it just to show me my (already known) stupidity.

Yes I am old, yes I am an absolute beginner - but I am still willing to learn.

So, any help is very much appreciated.

Cheers

The widget expressions have access to the javascript Math object so you can use Math.abs

label: =Math.abs(@@'nameOfItem')

Justin,

thanks a lot for your reply.

Tried it - but (maybe wrongfully done) it doesn’t work:

Right, sorry. I forgot to convert the item state string to a number. That adds one more level of difficulty. To convert you have to use parseInt (or parseFloat):

label: =Math.abs(Number.parseInt(@@'Things_OpenWB_OpenWB_Pro_PV_Power_Generation'))

But the problem there is that will strip off the unit. So you can either manually add the unit back on:

label: =Math.abs(Number.parseInt(@@'Things_OpenWB_OpenWB_Pro_PV_Power_Generation')) + ' W'

or (my recommendation) just cheat and manipulate the string instead of doing actual math:

label: =(@@'Things_OpenWB_OpenWB_Pro_PV_Power_Generation').replace('-','')
1 Like

Justin,

THANKS A LOT for your effort in helping me out!

I’ll give it a try (currently no more power from PV…) - but your solution looks great for me.

This will be a next step ahead.

Really appreciate your help!

Mausepaul