Usable Battery Sensor
Posted: Wed Oct 19, 2022 8:01 pm
The standard Fox battery SoC sensor returns a 0-99% value (it never gets to 100%), coupled with the fact that a portion of the battery is unavailable for use (normally set to 10%) it can be a bit confusing when trying to explain the display.
This sensor code provides a 'usable battery' sensor by deducting the unusable minimum SoC and provides a full range 0-100% sensor that represents only the portion of the battery that is usable.
For example if the minimum SoC is set to 10%, only 89% of the battery is actually usable - this 89% is then scaled up to provide a full 0 - 100% sensor.
The sensor below needs to be added to your configuration.yaml file, if you run a different minSoc than the default value of 10, enter the value you use and it will then provide a new sensor.usable_battery
Edit: - this second version is for the latest firmware and Cube batteries where the battery SoC is reported full range to 100%
This sensor code provides a 'usable battery' sensor by deducting the unusable minimum SoC and provides a full range 0-100% sensor that represents only the portion of the battery that is usable.
For example if the minimum SoC is set to 10%, only 89% of the battery is actually usable - this 89% is then scaled up to provide a full 0 - 100% sensor.
The sensor below needs to be added to your configuration.yaml file, if you run a different minSoc than the default value of 10, enter the value you use and it will then provide a new sensor.usable_battery
Code: Select all
- sensor:
- name: "Usable Battery"
unit_of_measurement: "%"
state: >
{% set minSoC = 10 | float %}
{% if states("sensor.battery_soc") in ['unknown', 'unavailable', 'none'] %}
{{ states("sensor.battery_soc") }}
{% elif states("sensor.battery_soc")|float >= minSoC%}
{{ ( (states("sensor.battery_soc")|float - minSoC ) * (100/(99-minSoC)) )|round(0, default=0) }}
{% else %}
{{ 0|float }}
{% endif %}
Code: Select all
- sensor:
- name: "Usable Battery"
unit_of_measurement: "%"
device_class: battery
state: >
{% set minSoC = 10 | float %}
{% if states("sensor.battery_soc") in ['unknown', 'unavailable', 'none'] %}
{{ states("sensor.battery_soc") }}
{% elif states("sensor.battery_soc")|float > minSoC%}
{{ ( ((states("sensor.battery_soc")|float - minSoC) * (100/(100-minSoC)) ))|round(0, default=0) }}
{% else %}
{{ 0|int }}
{% endif %}