Energy consumption calculation

I would like to see the actual energy my houshold displayed in Watt. To achieve I followed these steps:

  1. I’ve made a template sensor to convert the kWh to Watt of my power_consumption
sensor:
  - platform: template
    sensors:
      power_consumption_watts:
        friendly_name: "Power Consumption (Watts)"
        unit_of_measurement: "W"
        value_template: "{{ states('sensor.power_consumption') | float * 1000 }}"
  1. I’ve made a template sensor which will calculate the difference between the productie (power_production) and the verbruik (power_consumption_watts)
template:
  - sensor:
      - name: "Energie Verschil"
        unit_of_measurement: "W"
        state: >
          {% set productie = states('sensor.power_production') | float %}
          {% set verbruik = states('sensor.power_consumption_watts') | float %}
          {{ productie - verbruik }}
  1. Display the created sensors on a entities card:
    afbeelding

For some reason, the calculation displays the amount of used power (huidig energieverbruik) as 138.0 W instead of -98.9 W, which should be returned to the grid (naar net).

I have the impression that the power_consumption sensor cannot handle negative numbers.

Who can help me to get a current overview of the power I am feeding back?

Maybe I’m not following, but you can’t just multiply a kWh value by 1000 to get W.

Well, that’s exactly what the template does, or am I missing something?

The template also seems to work well, but when more solar energy is generated, the ‘huidig energieverbruik’ drops to 0 because this sensor cannot display negative numbers. ‘huidig Energieverbruik’ should actually show the sum:
‘Zonne Energie Productie’ - ‘Huidig Energieverbruik’ = 'Energie Verschil (naar net)
in order to display the correct calculation.

kWh (or Wh) is energy and W (or kW) is power, you can’t simply convert one to the other, they’re different things.

Ok,

What I want to achieve is to subtract the current solar energy production from the power consumption, which will display the surplus on solar power. Then I can optimize the usage of my self-generated energy, for example by turning on the washing machine.

How can I achieve this?

In general terms, the following works very nicely for me

template:
  - sensor:
      name: excess_solar_power
      unit_of_measurement: W
      state: "{{ states('sensor.mb_solar_power')|int(0) - states('sensor.mb_load_power')|int(0) }}"

Note that

  • template > sensor is the new approach and preferred over sensor > template
  • you can do all the work, including multiplication, directly in one line
  • I like to work with intergers as there seems little point to 0.1 Watts
  • the int(0) provides a default value of 0 rather than unavailable / unknown

As long as your two entities, Solar and Load, are already power in Watts [or kW and you can use (states(‘sensor.load_power’)|float(0) * 1000) ] this should generate a value that is either positive or negative.

If you have no solar power, then the result will be negative, and equal to the load.

It is also worth noting that templates are recalculated and update when any one of the referenced entities changes state (or attribute), or this is limited to once per minute for entities that change often. If your two entities are updated by different integrations, and at different times, then the result will always be an approximation since you are not comparing like with like time-wise.

Thank you,

HA is now providing the correct sum (values). However, it seems that updating the data isn’t working properly.

It often happens that the value ‘Huidig Energie Verbruik’ shows as 0. When this occurs, the value of ‘Zonne Energie Productie’ is displayed as the result of the sum. This outcome is technically correct, but it’s impossible that I would then be feeding 100% back into the grid.

It likely has to do with the answer you gave about the difference in updating the states of the different integrations.

Is it possible to adjust this so that the timing of the statuses aligns to provide an accurate real-time calculation?

This is close to impossible.

You have two entity sensors here: solar (power) and load (power).

For an accurate calculation, both entity state values must be from the exact same time event, otherwise they will be out of step - the solar power from say 10:12:34 and the load power from 10:13:15 where clearly they do not relate to the same instant in time.

You do not say where your sensor values come from, so I have to guess.

All entity state values come into Home Assistant from the outside world, via integrations that talk with devices. It is the responsibility of the integration to sample the device state, and therefore the entity state value seen in Home Assistant will change when the integration updates.

In the ‘worst’ case you will have two, independent integrations. The only way that the state values of both integrations will match is if you can orchestrate both integrations to update their entity state values at the same time. This may be possible if both integrations have a service call (now action) that permits a forced update (ie force the integration to ‘take a reading’ for you).

In my case, I have a solar inverter that provides me with register values for both solar power and load power, so in theory I have one integration (my Node-RED flow) that reads the inverter Modbus registers for solar power and load power, at the ‘same’ time.

The Modbus reads take time (3 seconds over the registers I want) and the inverter does not update all registers together. The major issue is that the load value is calculated. The inverter knows the grid import/export value (from a power meter CT clamp) which it samples every second. The inverter knows the solar power, and in my case the battery charge/discharge power from internal current sensors.

By the laws of physics
solar + import + discharge = load + export + charge

hence load = solar + import - export + discharge - charge

and in simple terms, the inverter itself cannot measure all of these at exactly the same time, so the load figure is a guestimate.

I calculate the unavoidable ‘error’ (much as the Architect does in The Matrix) by

imbalance = solar + import + discharge - load - export - charge

At quiet times, this is roughly the amount of energy the inverter is consuming itself. When turning large loads on or off, or for cloud cover, this value can be very high.

In your case, with a value regularly going to zero when you don’t expect it to, it may be the result of ‘unavailable’ rather than a precise reading, so you may want to explore the route by which your load sensor value arrives to see if you need to trap ‘unavailable’ and perhaps remove the zeros altogether. Availability templates and the like spring to mind.

An alternative would be to time-average both values over the same period, say 5 minutes. This is long enough to make the readings relate to the almost equal time period, yet short enough to provide a realistic view on what is going on. There is a great statistics integration in Home Assistant that I use for rolling-average values to try and smooth out fluctuations in solar production for much the same reason. Very much still a work in progress, since ‘excess solar’ will go from 3kW to 0 in the moment it takes to turn the vacuum cleaner on.

Hi Geoff,

Thank you for the detailed explanation. I now understand the complexity involved!

Both integrations indeed come from different entities. I’ll investigate further to see if it’s possible to pull both values from the ‘smart meter’ instead.

I’m very interested in the statistical integration in Home Assistant. Could you point me to any documentation or resources on how I could use this (perhaps via Node-RED) in my setup?

Thanks again for your help!

The Statistics Integration in Home Assistant is documented at

https://www.home-assistant.io/integrations/statistics/

It is a powerful tool to provide a statistical summary on an entity state over a recent time period or sample. Create a new stats-entity sensor using a source sensor and YAML configuration.

Statistical analysis in Node-RED is possible, you just need to find an appropriate node that does what you want (or write your own code to do it yourself).