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!