Help to convert battery voltage to percentage

You could use the compensation integration:

compensation:
  battery_pct:
    source: sensor.your_battery_sensor_here
    unit_of_measurement: "%"
    data_points:
      - [1.62, 100]
      - [1.3, 0]

However this will project values above and below your voltage range if they are input to the integration.

Instead you should use a template sensor:

template:
  - sensor:
      - name: "Battery %"
        unit_of_measurement: "°%"
        device_class: battery
        availability: "{{ states('sensor.your_battery_sensor_here')|is_number }}"
        state: >
          {% set batt_volts = states('sensor.your_battery_sensor_here')|float(0) %}
          {% set batt_pct = 312.5 * batt_volts - 406.25 %} {# calculate percent from the voltage #}
          {% set batt_pct_clamped = ([0, batt_pct, 100]|sort)[1] %} {# clamp the output to a 0-100% range #}
          {{ batt_pct_clamped }}

I’ve expanded my working for clarity. You can simplify it if you wish.

The device class will give a nice changing icon depending on the state of charge.

The availability template will make the output unavailable if your sensor is unavailable (rather than 0 from the float(0) default).

7 Likes