How to sync multiple HomeWizard meters in a template?

I own multiple Homewizard devices to monitor power and energy. This question involves the P1 dongle in my smart meter and an kWh meter to measure solar PV generation.

The P1 dongle reports current electrical throughput (among many other things) which can be either a positive value if I’m importing from the grid, or negative when I’m exporting to the grid

The kWh meter reports the current value of power being generated by solar PV. This is always a positive number, or it is zero when it’s dark.

I’ve defined a template sensor in YAML that sums these two values, and this (by definition) returns the power currently being consumed in my house. For example: If I’m exporting 3400 watt to the grid, and my solar is generating 4000 watt, then the sum of the two values is -3400 + 4000 = 600 watt. That means that I’m currently consuming 600 watt in my house.

The thing is that the two meters do not update at the very same moment. Each meter gets an updated value reported into Home Assistant with intervals that are either not the same, or out of sync.

The result is that sometimes when a cloud moves across my panels and generation drops, or when I switch on a high load like a kettle in the house, as just 2 examples, the values of the 2 meters and the sum of them can turn either very high or even below zero.

For example, let’s say the P1 meter’s last reported value is still -3400 watt, but the kWH meter for solar suddenly drops from 4000 to reporting only 2000 watt. Then the template sums them as -3400 + 2000 = -1400 watt, and then on the dashboard that displays the template sensor shows the house consuming negative 1400 watt. A few seconds later this will be “fixed” when the P1 meter’s reading is also updated and reporting -1400 watt. The new template sensor summed value becomes -1400 + 2000 = 600 watt again.

To some degree, this is purely a cosmetic issue, the actual values of grid throughput, solar generation and thus house consumption change in real time in the electrical installation in my house. And if I look at the graph of the house consumption for the past couple of hours, it does make for a very weird look.

I’m curious if anyone in the community has noticed this kind of behavior as well and possibly found a way to work around it. Meters from other brands or meters measuring very different readings may have the same kind of behavior, so perhaps there is a best practice that someone could refer to?

This is quite a hard problem to solve, but you could consider averaging the measurements over a short period (e.g. a 5 minute rolling average). This will probably hide the problem of out of sync measurements.

I have no idea how, and if this is possible in templates but maybe this can get you in the right direction.

Another way is creating an automation that refreshes all your devices at once, while disabling auto-updating. Problem with this could be that the P1 meter only receives a telegram every 1 (or 10 second) so you can be a second late every time.

Looking into this a little bit more, and considering that the HomeWizard integration is “local polling”, it seems that there are probably two aspects that influence the availability of fresh power readings:

  • the integration itself, which decides how frequently and when exactly to poll the meters for new readings;
  • the meters internally, which may or may not have a fresh value available at the time the integration decides to poll them

Theoretically, the speed of the network (including the wifi) could be a factor in this, but I don’t think it is in this case.

Obviously, an integration can’t poll all of the devices it has at the very same millisecond, especially since I have a few more devices than just the two that I mentioned in my original post.

Using a moving average might be a good way to smooth things over, but a 5 minute rolling average would probably be a bit too much of a good thing. I see that values are being updated every few seconds, so perhaps a 30 second rolling average could help.

If anyone else in the community has any thoughts on this or a best practice to share, I’d very much appreciate it.