[SOLVED] Change the Power of a Battery into Percentage

I have a small problem and I need some help with it.

I am using a Raspberry Pi B+ as openhabian Server (2.5.10) to measure Weather Information on different locations within and around my house. For this I built myself some Solar Powered Weather Stations which send their Data via MQTT to my openhab.

Everything works fine and basically i am happy with it. But there is one thing that bugs me a little.
The Weather Station transmit the Battery Level as Voltage (5.28V = Battery Full 3.3V = Battery Empty). I want to convert the V into % to get a more… common display of the current Battery Charge.

The Item is
Number Wetterstation7_Battv “Batterie[%.2f V]” (SWS_07, gbattv) [“Batterie”] {channel=“mqtt:topic:SWS_07:Battv”}

I already tried multiple attempts to change the number with a rule, but nothing happens when the Number changes. My assumption is, that the calculation fails because the MQTT sends the number with a dot, and not a comma.

The good news is, I figured out a way to change the number.

I changed my Item to:
Number Wetterstation7_Battv “Batterie[%.2d %%]” (SWS_07, gbattv) [“Batterie”] {channel=“mqtt:topic:SWS_07:Battv”}

and I defined a rule saying this:
rule “Wetterstation7_battv”
Item Wetterstation7_Battv received update
//Wetterstation7_Battv.postUpdate(Wetterstation7_Battv.state as Number * 19)

The Bad news are, when the Number is changed, the Value goes nuts. Because the Item updated and my rule updates my Item again which will go on forever if I don’t stop it manually.

My log after running this for a couple of seconds:
2020-11-20 12:16:19.697 [vent.ItemStateChangedEvent] - Wetterstation7_Battv changed from 161202719406392407727452143830050971106562812964940714301683437324630987983049927175628442423493968642818505444334147470041696264212053906291403490520857835289619717922401029587437031922666446503594082.87 to 3062851668721455746821590732770968451024693446333873571731985309167988771677948616336940406046385404213551603442348801930792229020029024219536666319896298870502774640525619562161303606530662483568287574.53

I think you have more than one option to solve this.

1.) In your weatherstation thing you have defined the battery channel. In the channel definition under advanced options is a option to transform incoming values.

2.) Just change the display of your item using a javascript transformation

3.) Create a Battery percent item that is updated with a rule from the voltage item

I would prefer the solution 1. Directly convert the incomming voltage to a percent value.

Thank you for the quick response. I totally agree with your opinion that Solution 1 is the best way. Unfortunately I have absolutly no idea what I could/should enter in the Incoming Value Transformation to get the desired result.

You will need to use a Transformation Service, probably the JavaScript one.

Essentially, your transformationPattern will send the voltage value to a JavaScript script, which will do some maths and send back a percentage value.

You could could also use that same JavaScript script to do option 2 from @Dibbler42 suggestions, instead of option 1. Your choice!

I think we can help on this. As @hafniumzinc wrote you need to install the Transformation Service JavaScript first. Than you have to create the transformation file it self in the transformation folder in OH config.

The file should named sometin like this: voltage2percent.js and it content should follow this structure:

(function(i) {

 // i is the incoming variable with your voltage value

// Here you should do some math magic to convert the value in to a percent value

// an here you return the value
return myBatteryLevel


In the mqtt section you should enter JS(voltage2percent.js)

I am not familiar with javascript, so i start always searchung the forum on javascript transformations and the i start the development process.

First my transformation only passes the incoming value to the outgoing and in a secons step is add some easy math like adding 100 or something like this. And in the thrid step i apply the right math. So i coud be sure that the transformation works right.

If you have problems put some screen shots and script example in you post. This makes it easier to help

Thanks a lot for this.
I was struggling with the Javascript as I didn’t know what variable would be the incoming MQTT value and what variable OH expects as return.

Just as you said, I did some math magic

(function(i) {
i = i * 18,9393
i = i.toFixed(2)
return i

and voila. The 5.28 converts to 99.999504 which is then rounded in the next step to 99.99.

1 Like