Help with the template sensor

But, when the battery is fully charged the current (y) will drop very low or to 0 and the sensor will return “Battery Discharging” instead of “Battery Full”. In order to cover the negative current issue and avoid the divide-by-zero error you can redefine “y” after the first else.

- platform: template
  sensors:
    battery_recharge_time:
      friendly_name: "Battery Recharge Time"
      value_template: >-
        {% set x = states('sensor.battery_remaining_capacity') | float(0) %}
        {% set y = states('sensor.battery_current') | float(0) %}
        {% if y < 0 %}
          Battery Discharging
        {% else %}
          {% set y = ([0.1, y, 200]|sort)[1] %}
          {% set z = (2*(x/y)) | round(0, default=0) %}
          {% if z > 591 %}
            Battery Full
          {% elif 591 > z > 550 %}
            Battery almost full
          {% else %}
            {{ z }} Hr
          {% endif %}
        {% endif %}
1 Like

That looks better.
Yeah without really knowing what the different states are of the sensors, I wasn’t entirely sure what the current sensor would return and when. If the current is 0 then the battery is full? But what if the battery is completely empty, isn’t the current 0 then too?

x will approach 0 as the battery approaches empty. So no matter what the current is, z will be low.

I’ve put it in and will observe results over the next day or two and advise

{% set y = states('sensor.battery_current') | float(0)) %}

had to make it like this:

{% set y = states('sensor.battery_current') | float(0) %}

Thanks a mill, I’ll definitely provide feedback :pray:

After testing all and observing, I came to a conclusion that the below code is the most optimal for the need :slight_smile:
Thank you all for helping, I am very very grateful

- platform: template
  sensors:
    battery_recharge_time:
      friendly_name: "Battery Recharge Time"
      device_class: 'battery'
      value_template: >-
        {% set x = states('sensor.battery_remaining_capacity') | float(0) %}
        {% set y = states('sensor.battery_current') | float(0) %}
        {% if y < 0 %}
          Battery Discharging
        {% else %}
          {% set y = ([0.1, y, 200]|sort)[1] %}
          {% set z = (2*(x/y)) | round(0, default=0) %}
          {% if z > 591 %}
            Battery Full/Not Charging
          {% elif 591 > z > 550 %}
            Battery almost full
          {% else %}
            {{ z }} Hr
          {% endif %}
        {% endif %}

P.S.Battery Full/Not Charging - caters for the sensor.battery_current being a value of 0…so battery is either not charging due to the low “sun” conditions or its full.

1 Like

I tried to use your code too but it always shows me “Battery Discharging” I want to mention that I am a beginner in ha.
what does 591> z> 550 mean ?. I have a 48v photovoltaic installation. a 100ah li-ion battery. I apologize for the stupid questions. but I’
m a beginner

No such thing as a stupid question at all :slight_smile:

So my battery capacity is 296ah (4 batteries of 74ah).
So then when you put that in a formula to get time needed for recharge is it: 2 x (remaining ah of the battery x current at which the battery is being charged at. 592 is the result when battery is full because its not charging…when battery is busy discharging, that means that “current at which is currently charging” is a negative value. You saw up in the posts that charging value of 0 was a problem and that is what was resolved in the latest code thanks to the templating gurus that helped :slight_smile:

Also there is one piece here that I never showed - “remaining ah of the battery”

That sensor is used here and the code for that sensor is:

- platform: template
  sensors:
    battery_remaining_capacity:
       friendly_name: "Battery Remaining Capacity"
       device_class: 'battery'
       unit_of_measurement: "Ah"
       value_template: >-
         {% set t = states('sensor.battery_soc_victron') | float %}
         {% set u = states('sensor.battery_current') | float %}
         {{(296*(t/100)) | round(0, default=0 )}}

Here instead of 296, you would put 100 in order to get the results you need.
All this requires you to have either modbus or MQTT integration into Victron in order to get “battery_soc_victron” and “battery_current” sensors in HA.

Hope it makes sense :smiley:

Thank you very much for your help . It works perfectly. could you also post the code for battery time remaining? Thanks

Is the code correct for my battery? (100ah)

  • platform: template
    sensors:
    battery_recharge_time:
    friendly_name: “Battery Recharge Time”
    device_class: ‘battery’
    value_template: >-
    {% set x = states(‘sensor.soc_senzor’) | float(0) %}
    {% set y = states(‘sensor.ant_bms_curent_baterie’) | float(0) %}
    {% if y < 0 %}
    Battery Discharging
    {% else %}
    {% set y = ([0.1, y, 200]|sort)[1] %}
    {% set z = (2*(x/y)) | round(0, default=0) %}
    {% if z > 199 %}
    Battery Full/Not Charging
    {% elif 199 > z > 150 %}
    Battery almost full
    {% else %}
    {{ z }} Hr
    {% endif %}
    {% endif %}