# Convert ADC value to battery voltage

I have setup a nodemcu with DS18B20 to monitor the temperature of my spa. It runs from a 18650 Lithium battery being charged by a solar panel. As well as the temperature I want to monitor the voltage of the battery. I use a voltage divider across the battery and at 4.02v it reads 980, therefore, I need to divide the ADC reading by 244 to display the battery voltage.
This is the line from my sitemap which displays the ADC value.
Text item=nodemcu3Voltage label=“Battery Voltage [%.1f V]” icon=“battery”
Where do I divide this value by 244, in this line or elsewhere?

You have some options, including:

• Make a rule triggered when `nodemcu3Voltage` is changed, do the math in the rule, then update a new Item with the new number.
• Use a Transformation Service (probably JavaScript) to apply the math at the Item or Sitemap level:

Can you not transform the data before you send it to MQTT from the nodemcu?

What software are you running on the nodemcu

I’m using Tasmota and I think transforming the data in Tasmota will the easiest way to go.
I didn’t realise that a simple division of a value would be so complicated.
Now to work out the rule.

I now have my readings in the range 0 - 4200 mV using AdcParam 6, 0, 1023, 0, 4200 in Tasmota ( works in V9.0.0.3 but not V8.5.1).
How do I display this in openhab as volts to 1 or 2 decimal points.
Can I do it with formatting in sitemap?
Edit: I’ve read some more and it’s not simple. I’ll stick with displaying mV.
Thanks to those that offered help.

You can do this using a transformation. Your first transformation seems like a monster step; once you’ve done one you’ll wonder why you didn’t do it sooner. I’d use a JS javascript type here that just does a divide-by-a thousand, then formats decimal places.

Thanks rossko. You make it sound so simple. Trouble is I have very little experience in programming, mostly hardware and network administration, so I would have to teach myself javascript first. At 72 the mind isn’t as sharp as it used to be but I will give it a try.

Luckily for you I’ve just been playing around with this as I was interested!

Assuming your Item looks something like this:

``````Number nodemcu3Voltage "Battery voltage"
``````

``````Text item=nodemcu3Voltage label="Battery Voltage [JS(mV2V.js):%s]" icon="battery"
``````

And in your `transform` folder you need to create a new file called `mV2V.js` and copy/paste the following:

``````(function(x) {
return (parseInt(x)/1000).toFixed(1) + " V";
})(input)
``````

That will give you something on your Sitemap which looks like (given a reading of 2900mV):

(Actually, the above would be shown for any reading between 2850mV and 2949mV).

1 Like

Thanks. All working within 2 minutes. I changed it to 2 decimal points just because I could.
I will still teach myself javascript because I can see how beneficial it will be to me.

1 Like

Sounds like a good idea! Perhaps the first thing to try might be too roll back the transformation you applied at the Tasmota level, then modify mV2V.js to perform your original request!

I’ll never catch you up but I’m only a half dozen steps behind. Yes it all takes longer these days
Just about every bit of javascript I’ve used is stolen from some snippet on the web, but a little programming background does help to stitch fragments together.

The only non-obvious trick with transformations in openHAB is that everything passes in and out in string form.
You’ve got to parse an input to number before doing maths on it.
You can’t format a transform output as though it were a float e.g. `%.1f`, it is a string so use `%s`

1 Like

While that formatting worked fine in ClassicUI it doesn’t in HABpanel.
How do I do the same in HABpanel?

There’s some kind of widget option to select ‘use server side formatting’, and you will need to remember it is a string.

I think this will only work if the transform is applied to the Item itself, not the Sitemap level as in my example, right?

1 Like

Very much so. HABpanel is not in the least interested in sitemaps.

@lesterb that effectively means you would change the Item configuration to:

``````Number nodemcu3Voltage "Battery voltage [JS(mV2V.js):%s]"
``````

Of course, you would have to remove the transform from the Sitemap definition in this case, if you wanted to continue using ClassicUI and HABPanel.

Interestingly, @rossko57, I notice the docs for Transformation Service has this as an example:

``````Number  Livingroom_Temperature_F "Temperature [JS(convert-C-to-F.js):%.1f °F]"   {/*Some Binding*/}
``````

Presumably this shouldn’t work, because the JS spits out a string?

Yup, I reckon that is duff, and a poor example too after the invention of Number:Temperature types.

That worked perfectly.
Thank you.