Converting voltage to percentage in a value template

I have my solar system integrated into home assistant. I would like to be able to see the current battery voltage displayed as a percentage and to be able to easily use that percentage in automations and other stuff. From my googling I have concluded that I need to use a Template. But I am not sure how I would do that.

0% = 48.4V
100% = 50.92V
anything above 50.92 is 100%+ charging

Thanks in advance!

Sounds like something like this, assuming your sensor is called sensor.battery_voltage:

{% if states('sensor.battery_voltage')|float > 50.92 %}
  Charging
{% else %}
  {{ ((states('sensor.battery_voltage')|float-48.4)/0.0252)|round(1) }}
{% endif %}

You can test in Developer Tools -> Templates

3 Likes

EDIT: Resolved :slight_smile:

Hi @Tinkerer

May I ask how you did get the number 0.0252 used ?

I’m guessing from your edit you worked it out, if not let me know.

Hi,

i want to use the template for my solarbattery.

But i cant figure out, how you get the number “0.0252”
How to calculate the value?

It would be very nice, if someone can explain it to me.

In my case the values are:
0% = 48V
100% = 57,6V

But im interested, how to get thise spezial value.

Easy… maths :wink:

The OP had a range of 50.92-48.4, or 2.52. You need to scale it so that 2.52 is 100%.

So, once they had the current voltage, subtract 48.4 and then divide by 0.0252 (2.52/100) to scale it up.

In your case you have a range of 57.6-48, or 9.6. You need to scale that so that 9.6 is 100%.

That means you subtract 48 from tthe current voltage then divide by 0.096.

1 Like

Sound good!!! Thank you