Sensor Rounding Issue

Hi Everyone, hoping someone out there can assist with a rounding issue I’ve encountered. I’ve done as much as I can with my limited coding skills.

Essentially, it should be a simple equation to take the solar inverter power generated and minus the sum of house power consumption, and display a result if there’s any excess generated. This is working but I’m ending up with a result that is sometimes 14 dp instead of 2. Please see code below:

Code to sum all house energy monitor devices:

  - platform: template
    sensors:
      total_house_consumption:
        unit_of_measurement: "W"
        value_template: >-
                {{(
                (states('sensor.pc191ha_4_power')|float(0))+
                (states('sensor.pc191ha_7_power')|float(0))+
                (states('sensor.pc191ha_2_power')|float(0))+
                (states('sensor.pc191ha_power')|float(0))+
                (states('sensor.pc191ha_3_power')|float(0))+
                (states('sensor.pc191ha_5_power')|float(0))+
                (states('sensor.pc191ha_6_power')|float(0))+
                (states('sensor.pc191ha_8_power')|float(0))+
                (states('sensor.pc191ha_2_power')|float(0))+
                (states('sensor.pc191ha_9_power')|float(0))+
                (states('sensor.pc191ha_10_power')|float(0))+
                (states('sensor.pc191ha_11_power')|float(0))+
                (states('sensor.pc191ha_12_power')|float(0))
                )}}
        device_class: energy

Code to do equation for excess energy consumption

  - platform: template
    sensors:
      solar_gridreturn:
        unit_of_measurement: "W"
        value_template: >-
          {% if ((states('sensor.solax_inverter_dc_power_pv1')|float(0)) - (states('sensor.total_house_consumption')|float(0)) > 0 ) %}
          {{(states('sensor.solax_inverter_dc_power_pv1')|float(0)) - (states('sensor.total_house_consumption')|float(0)|round(2))}}
          {% else %} 0.00 {% endif %}
        device_class: energy

Sometimes it will output correctly with 12.34 W (for example), other times it shows 1.13999999999999 W (for example). I’ve got the round(2) function at the end of the equation but not sure if this is working/in the correct place.

Any help appreciated!

Wondering if I have to put round(2) outside of the brackets…

{{(states('sensor.solax_inverter_dc_power_pv1')|float(0)) - states('sensor.total_house_consumption')|float(0))|round(2)}}

This looks right.

The firstm8ne will round the total house consumption before sum up the inverter, so if the inverter have more decimals, the sum is not rounded.

Have confirmed that inverter is displaying 2dp (eg 294.11 W currently), total house consumption is display ing 1dp (394.0 W). Will this affect the rounding I’m trying to achieve if I update to this line:

{{(states('sensor.solax_inverter_dc_power_pv1')|float(0)) - states('sensor.total_house_consumption')|float(0))|round(2)}}

This will sum the two values using all the available decimals and round the result to only 2 decimals.

294.11 + 394.0 = 688.11, will be rounded to 688.11
294.111111 + 394.0 = 688.111111, will be rounded to 688.11
294.111111 + 394.005111 = 688.116222, will be rounded to 688.12