Utility meter not counting corectly

Hi!
I’m having trouble getting my energy meter to work properly. It’s a PZEM-004 energy meter via Modbus. All the sensor values read from device show correct values. The problem is the utility meter which, instead of incrementing its value by 1kWh on each change of counter (it has no decimal part), increments by an amount equal to the meter’s actual value (7449kWh). Did I miss something?
234e1a1277ae94ef25d4e73ac3d99a6fa23b8d8e_2_294x500

# modbus device
- name: PZEM004
  type: tcp
  host: 192.168.1.111
  port: 502
  delay: 0
  message_wait_milliseconds: 300
  retries: 3
  timeout: 5
  sensors:
    - name: Energy counter
      unique_id: EnergyCounter
      address: 120
      input_type: holding
      scan_interval: 5
      slave: 1
      scale: 1
      precision: 0
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
utility_meter:
  hourly_energy:
    source: sensor.energy_counter
    cycle: hourly
    delta_values: false
  daily_energy:
    source: sensor.energy_counter
    cycle: daily
    delta_values: false
  monthly_energy:
    source: sensor.energy_counter
    cycle: monthly
    delta_values: false

image

Does the value of the total actually increase when new values are sent? I get the feeling that because the new value isn’t higher and the state class is total_increasing, it registers every double as a counter reset. I think utility meter expects only actual increases. But i.m.h.o. it should not behave as it does.

If the total value never actually resets, setting the state class to total might fix it I guess.

If the total is decreasing (because energy is decreasing (due to energy production) then this is expected behaviour and the state class should then be changed tot total.

I just noticed some zero value spikes on the energy counter, possibly due to communication problems. Maybe that would be the cause of the error. How can this situation be handled? My counter keeps going up, I have no energy production. It is expected that it will reset itself after 9999 kWh.

This is definitely hte cause of the error. Every time the value rises from 0 to the total then that total is added to the utility meter.

We can filter this with a template sensor:

template:
  - sensor:
      - name: Energy Counter Filtered
        device_class: energy
        state_class: total_increasing
        unit_of_measurement: kWh
        state: "{{ states('sensor.energy_counter') }}"
        availability: "{{ states('sensor.energy_counter')|float(0) > 0 }}"

The state of this filtered sensor will be unavailable whenever sensor.energy_counter is 0.

Feed this filtered sensor to your utility meter.

Changing from unavailablesome value does not add anything to the utility meter.

EDIT: Actually looking at the modbus sensor options, you could probably use this:

So try this before trying the template sensor:

# modbus device
- name: PZEM004
  type: tcp
  host: 192.168.1.111
  port: 502
  delay: 0
  message_wait_milliseconds: 300
  retries: 3
  timeout: 5
  sensors:
    - name: Energy counter
      unique_id: EnergyCounter
      address: 120
      input_type: holding
      scan_interval: 5
      slave: 1
      scale: 1
      precision: 0
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      nan_value: 0

Thank you.
I have modified according to your recommendations, but it seems to behave the same. Here are the results. Also increased the precision of the energy_counter register to observe smaller changes.

Values after zeroing all entities at the start of the period:


The filtered value remains zero, maybe I’m missing something because I’m new to HA.

    - name: Energy counter
      unique_id: EnergyCounter
      address: 122
      input_type: holding
      data_type: uint32
      swap: word
      scan_interval: 5
      slave: 1
      scale: 0.01
      precision: 2
      unit_of_measurement: kWh
      device_class: energy
      state_class: total_increasing
      nan_value: 0
# template.yaml
- sensor:
    - name: Energy Counter Filtered
      data_type: float
      device_class: energy
      state_class: total_increasing
      precision: 2
      unit_of_measurement: kWh
      state: "{{ states('sensor.energy_counter') }}"
      availability: "{{ states('sensor.energy_counter')|float(0) > 0 }}"
# utility_meter.yaml
hourly_energy:
  source: sensor.energy_counter
  cycle: hourly
daily_energy:
  source: sensor.energy_counter
  cycle: daily
weekly_energy:
  source: sensor.energy_counter
  cycle: weekly
monthly_energy:
  source: sensor.energy_counter
  cycle: monthly
yearly_energy:
  source: sensor.energy_counter
  cycle: yearly

hourly_energy_filtered:
  source: sensor.energy_counter_filtered
  cycle: hourly

Yeah ok, so the nan_value did not help. The template sensor should have. What does the hourly filtered utility meter history look like?

contains only zero
image

image

# File "template.yaml"
- sensor:
    - name: Energy Counter Filtered
      data_type: float
      device_class: energy
      state_class: total_increasing
      precision: 2
      unit_of_measurement: kWh
      state: "{{ states('sensor.energy_counter') }}"
      availability: "{{ states('sensor.energy_counter')|float(0) > 0 }}"

I’ve just noticed that my filtered sensor is not showing up in the DeveloperTools/States section.

Did you restart after creating the sensor?

Are there any errors related to it in the log?

Checking the logs, I found that the following lines prevented the creation of the filtered sensor:

data_type: float
...
precision: 2

After changing to uint32 it works now. Thanks for your help!

I’m having a similar problem.
I set it up to monitor a hot water recirc pump.
That pump is drawing ~25W when on.

So at the end of 4 hours I expect the utility meter to read 100Wh
It’s reporting 37W

I also tried to monitor my coffee maker. it’s drawing 1000W when heating
I was expecting 1000Wh
At the end of 1 hour the utility meter reported 87,000W

I’ll scan through the posts above but this seems like a simple issue of integration over time.

The utility meter does not integrate power to energy, it just counts up the total of an energy sensor for a defined cycle (e.g. energy last hour). If you don’t have an energy sensor then you need to integrate your power to energy by using the Riemann sum helper:

Pay particular attention to the method, nine times out of ten left will reduce approximation errors the best but it is not the default setting.

Once you have that always increasing energy sensor (it never resets) you can create energy per hour/day/week/etc… sensors with the utility meter.

Thanks for the explanation - now I question the “utility” of the Utility Meter :slight_smile:

Watts is an instantaneous measurement - the power draw of a particular load at the point in time.
Watt-hours is the power (energy) consumption.

What is the utility of “counting” multiple instantaneous load measurements?
It’s not averaging over time. It’s not determining the energy consumption. It doesn’t appear to report peak load over the defined period.

It’s definitely summing, although now I’m stumped as to what the information is supposed to illustrate.

Also, how many samples is it summing?
I had it set to “daily”, how many cycles is that? Does it take a reading every minute? 5 minutes? 5 seconds?

I’ll look at the linked helper.

The integration helper converts e.g. W to Wh or kW to kWh. You need that if you only have W or kW, because there’s no point in adding those. Compare it to speed versus distance traveled. Adding speeds tells you nothing. Integrating speed to get distance traveled is useful. Integrating is not mere adding like the utility meter does. Integrating is calculating a surface area in a graph.

The utility meter helps creating an ever increasing total for entities that reset when you don’t want them to, or helps you get a daily, weekly, monthly value for totals that are ever increasing. So if the integral helper gives you a total kWh sensor (ever increasing), the utility meter can give you a daily or monthly kWh sensor.

Another example, my wall charger gives a sensor for kWh added per charge. It resets for each charge cycle. In order to get an ever increasing total, you can also use an utility meter without a cycle. It will add all charge cycles for you. So utility meter is doing a summation of increases over a period of time.

That was what I expected.
It’s not doing that.

“What” isn’t doing that?

And what is “that”?

The Riemann sum integration is not integrating power to energy?

Or the utility meter is not resetting your energy total counting?

Please describe your issue in detail. We are not very good mind readers.

Read what I wrote again. That is not what the utility meter does.

It takes an ever increasing energy total sensor and enables you to extract totals for any period you want.

An ever increasing energy total is not much use for calculating your monthly bill. The utility meter will give you monthly energy totals from the ever increasing total. Or daily totals if you want to know how much energy you used today compared to yesterday.

It has nothing to do with instantaneous power.

I think i identified the problem and i think the Utility Meter docs need to be more specific.

The plug im using has several power related entities
Current in A
Voltage in V
Power in W
Power Delivered in kWh

I was originally using the Power entity in W thinking the Utility Meter would use the time stamp of each sample and intergrate the instantaneous power measurement at the sample time and the time elapsed to calculate a running Wh total.

I was thinking the Power Delivered in kWh was already in kWh and appeared to be a running total with an infinite time horizon, it just keeps increasing. So using that as the source integrated over time would be double integrating the time component. Essentially returning kWhh. And there is no way to reset that entity value so the starting number would include power consumed in the past.

Now, if i understand correctly after playing with the utility meter integrating the kWh entity, it appears my assumptions were in correct.

It appears that the Utility Meter is taking the samples as differential consumption reported since the last sample. That was not readily apparrent.

The docs should be very specific and direct the user to use a kWh entity not the instantaneous W entity.

"Input sensor

The sensor entity providing utility readings (energy, water, gas, heating)."

There should be a caveat immediately under that.
Something to the effect:
“Note: The Utility Meter tracks total consumption over a period and requires an entity value with an integral time component. For electricity use kWh, Wh, etc. Do not use an enity reporting instantaneous power in W.”

Similar break outs for other utilities should be specified.

The Utility Meter is not converting W to Wh as i originally expected.

There are feedback links at the bottom of every documentation page.