mkaiser / Sungrow-SHx-Inverter-Modbus-Home-Assistant

Sungrow SH Integration for Home Assistant for SH3K6, SH4K6, SH5K-20, SH5K-V13, SH3K6-30, SH4K6-30, SH5K-30, SH3.RS, SH3.6RS, SH4.0RS, SH5.0RS, SH6.0RS, SH5.0RT, SH6.0RT, SH8.0RT, SH10RT, SH5.0RT-20, SH6.0RT-20, SH8.0RT-20, SH10RT-20, SH5.0RT-V112, SH6.0RT-V112, SH8.0RT-V112, SH10RT-V112, SH5.0RT-V122, SH6.0RT-V122, SH8.0RT-V122, SH10RT-V122, SH4.6R
344 stars 92 forks source link

Wrong rounding in Battery Charge Sensor #172

Closed drschlaumeier closed 11 months ago

drschlaumeier commented 1 year ago

Hi, I discovered that you have a small error in rounding the Battery Charge. It currently rounds the (max_soc - min_soc)/10000 instead of (battery_capacity * (max_soc - min_soc)/10000). so, if you have 100%-5% it rounds to 0.9 which gives wrong capacity...

BR


      - name: "Battery charge"
        unique_id: sg_battery_charge
        unit_of_measurement: kWh
        device_class: energy
        availability: >-
          {{
              states('sensor.battery_capacity') | is_number and
              states('sensor.battery_level') | is_number and
              states('sensor.min_soc') | is_number and
              states('sensor.max_soc') | is_number
          }}
        state: >-
          {{  
              states('sensor.battery_capacity') | float *
                ((states('sensor.max_soc') | float - states('sensor.min_soc') | float) * 
                states('sensor.battery_level') | float / 10000) | round(1)
          }}
mkaiser commented 1 year ago

you are right, thanks for the hint!

just to be sure, because I am a bit unconcentrated at the moment... So it should look like this?

(see last section, I used the first lines for the developer tools --> template window)

sometimes the syntax is quiet challenging and hard to read :/

"{{ states('sensor.battery_capacity') }}" "{{ states('sensor.max_soc') }}" "{{ states('sensor.min_soc') }}" "{{ states('sensor.battery_level') }}"

{{ "(max-min) " }} "{{ states('sensor.max_soc') |float - states('sensor.min_soc') |float }}"

{{ "(max-min) capacity" }} "{{ states('sensor.battery_capacity') |float ( states('sensor.max_soc') |float - states('sensor.min_soc') |float ) /100 }}"

{{ "(max-min) capacity batt level" }} "{{ states('sensor.battery_capacity') |float ( states('sensor.max_soc') |float - states('sensor.min_soc') |float ) /100 states('sensor.battery_level') |float /100 }}"

{{ "((max-min) capacity batt level ) | round" }} "{{ ( states('sensor.battery_capacity') |float ( states('sensor.max_soc') |float - states('sensor.min_soc') |float ) /100 states('sensor.battery_level') |float /100 ) | round(2) }}"

drschlaumeier commented 1 year ago

Sorry for late reply. Best is you set brackets in each step calculation since the HASS template seems be stupid :-)

e.g. to be sure that the final result will be rounded instead the max-min ...

{{ "((max-min) capacity batt level ) | round" }}

"{{ ( (states('sensor.battery_capacity') |float) ( ((states('sensor.max_soc') |float) - (states('sensor.min_soc') |float ) /100) ((states('sensor.battery_level') |float) /100) ) ) | round(2) }}"