Recently I have connected my solar inverter to HASS through MQTT. Since the inverter does not directly track the energy usage in kWh, I need to add a helper that will integrate it for the different categories.
The inverter provides me with the following data (relevant to the issue at hand):
Instantaneous solar power generation in W
Instantaneous output power of the inverter in W
In order to track total solar generation and total house power consumption in kWh I have created left integrating helpers that give me “Solar Total Generation” and “Household Total Consumption”. These work perfectly and integrate their values over time correctly.
However, I also wanted a way of measuring the amount of energy consumed from the grid. For this I have created another helper called “Grid Energy Usage”. The intention was to subtract the solar generation from the inverter output energy and thus obtain how much energy was consumed from the grid. However, I have observed that when solar generates more than or an equal amount of energy as the inverter outputs than the “Grid Energy Usage” value would actually decrease. In order to address this I attempted to add clamping to just leave it at zero if the result would have been negative. But the issue of the “Grid Energy Usage” values going down during periods of high solar generation still persists and I am unsure why.
“Grid Energy Usage” helper:
{% set grid_energy = states('sensor.household_total_consumption')|float - states('sensor.solar_total_generation')|float %}
{{ grid_energy if grid_energy >= 0 else 0 }}
“Solar Total Generation” over the course of a day:
“Household Total Consumption” over the course of a day:
“Grid Energy Usage” over the course of a day, we can see how it goes down between ~13:00 - 16:00:
Energy dashboard showing negative consumption:
I would appreciate if someone could help me debug why my clamping to a zero value isn’t helping to prevent this negative consumption behavior. Or alternative ways of achieving this that are less error prone. Thank you.