Battery voltage to percentage

Hi All,

I copied this formula from a user on this forum and it’s brilliant, works well.

I want to add an extra addition on to this and wondering if anyone can help. I want to show when the battery reaches a certain voltage to replace 100% with “Charging” and when it reaches the charged (float) voltage to say “Charged”

- sensor:
   - name: "BatteryPercent"
      unique_id: battery_percentage
      unit_of_measurement: "%"
      device_class: battery
      availability: "{{ states('sensor.victron_solarcharger_battery_voltage_239')|is_number }}"
      state: >
         {% set batt_volts = states('sensor.victron_solarcharger_battery_voltage_239')|float(0) %}
         {% set charging = 28.40 %}
         {% set charged = 27.20 %}
         {% set max_battery_voltage = 25.77 %}
         {% set min_battery_voltage = 23.25 %}
         {% set batt_pct = (batt_volts - charged) / (batt_volts - charging) - charging %}` 
         {% set batt_pct = (batt_volts - min_battery_voltage) / (max_battery_voltage - min_battery_voltage) *100 %}
         {% set batt_pct_clamped = ([0, batt_pct, 100]|sort)[1]|round(0) %} 
         {{ batt_pct_clamped }}

Thanks
Daniel

I think if you want to do this with one sensor you will need to get rid of the unit of measurement and device class so you can just use text there. Either that or create a second sensor that provides the text reading. Alternatively you could use attributes on your template sensor to provide that as well and use an extension and/or a conditional card to show the alternate text of “Charging” or “Charged”.