Question: Energy Meter - Integration - Riemann Sum Integral

Yes I created it first without a unit_prefix. When I use this config…

  - platform: integration
    source: sensor.network_power_meter
    name: Network Energy Meter
    method: trapezoidal
    round: 2
    unit_of_measurement: kW

I get the same error as above.

If that’s the case, then you have to wait until the database is purged. It will update to kW when this happens.

Ok, thanks. I created a new sensor to test it out and it worked find. How long until a DB purge?

if you’re at the default, it should be nightly. Or 3 days, I can’t remember

Thank you for the help.

@petro - Is there a way to manually delete these sensors as they have not reset to kWh yet. I’d rather not change the names because I have a naming schema I like to follow. I’m currently using MariaDB. I even tried the following steps with no luck. Seems like HA is keeping the data somewhere else?

Seems my energy meters have reset, but the older utility meters are still reporting in Wh (as a unit) but the number is kWh.

1 - Changed my config to not use MariaDB and to use the built in database
2 - Commented out the configuration of both the utility meters and the energy meter sensor.
3 - Restarted HA
4 - Uncommented (enabling) both the utility meters and energy meters.
5 - Restarted HA

When I do this, the old utility meter is still Wh. If I create a utility meter against the same energy meter with a different name, it is kWh.

They are stored in your database, the only way is to wait for the purge, perform the purge yourself, or delete the database. The numbers are correct, the visual units display is incorrect. If you can get over that, just wait it out.

I did delete the database, and I just did it again.

Here is what I did…

  1. Delete the home-assistant_v2.db file as it existed from my previous experiment this morning.
  2. Changed my config to not use MariaDB.
  3. Rebooted
  4. Uninstalled MariaDB

The sensors still have old values and the utility_meter is showing the wrong unit.


I even configured TimescaleDB as I have never used that DB before and confirmed it is being used.

Here is the record from TimeScaleDB

{"source": "sensor.network_energy", "status": "paused", "last_period": "7.73", "last_reset": "2020-08-11T00:00:00.008825-04:00", "meter_period": "daily", "unit_of_measurement": "Wh", "friendly_name": "network_daily_utility_meter", "icon": "mdi:counter"}

I also did a purge with 0 days to keep. Confirmed the database emptied. Still reporting as Wh and doesn’t seem to be deleting it as it still has the previous period information in there.

I removed the sensors, rebooted HA, purged the DB, rebooted, added the sensors back, rebooted, and now they are there again with the Wh unit and old values.

that’s not the integration sensor…

Something else is creating that other sensor. I’m guessing utility_meter. The integration sensor does not have extra attributes like ‘meter_period’, and ‘last_reset’, etc.

Sorry, I didn’t mean to be confusing. The sensor.network_energy is the integration sensor. I had to create it as a new sensor that to get it to report the right numbers as I don’t think you can reset an energy meter to zero and deleting the database doesn’t help. The utility_meter sensor which is above it, is reporting in Wh because it had a source of sensor.network_energy_meter (which is still reporting in Wh, not kWh which is why I created sensor.network_energy).

Yes, the utility_meter is not the Riemann Sum sensor. It is a utility meter. My issue now is like the energy meter it won’t reset to kWh. I’ve tried everything I know of short of starting over from scratch which I can’t do because I have too many devices.

I’ve…

Run a purge
Delete the database
Switch to a new database entirely
utility_meter.calibrate

but somehow HA still remembers the old values for the utility_meter sensor.network_daily_utility_meter and the old energy meter sensor.network_energy_meter

Here is the original sensor.network_energy_meter after I added unit of measurement to it in the config. It reports kWh now, but the number is too high, yes it is just going up little by little now, so it is working, but how does HA remember the previous state of the sensor when I have done all the purging, deleting, switching…

I appreciate you taking the time to help with this.

Ok, I figured it out. It’s a feature, not a bug I. I was able to reuse an existing utility_meter name by first deleting that device out of the ../homeassistant/.storage/core.restore_state file and then restarting HA. Once restarted, recreate the sensor with the same name and it was reset. I had to wait a minute or two for the energy meter to increase in value so the sensor would initialize.

Hi,

I have a similar issue. I am trying - based on 4 objects (2 plugs and 2 bulbs) to convert the W to kWh and track the consumption. So this is what I constructed.

#W to KWh test:
- platform: template
  sensors:
    livingroom_plug_consumption:
      friendly_name: "Livingroom Plug Consumption"
      unit_of_measurement: "W"
      value_template: "{{ state_attr('switch.livingroom_plug_switch', 'current_power_w')}}"
    bedroom_plug_consumption:
      friendly_name: "Bedroom Plug Consumption"
      unit_of_measurement: "W"
      value_template: "{{ state_attr('switch.bedroom_plug_switch', 'current_power_w')}}"
    office_bulb1_consumption:
      friendly_name: "Office Lamp 1 Consumption"
      unit_of_measurement: "W"
      value_template: "{{ state_attr('light.office_lamp_1', 'current_power_w')}}"
    livingroom_bulb_consumption:
      friendly_name: "Livingroom Lamp Consumption"
      unit_of_measurement: "W"
      value_template: "{{ state_attr('light.livingroom_lamp', 'current_power_w')}}"


- platform: integration
  source: sensor.livingroom_plug_consumption
  name: livingroom_plug_energy_spent
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.bedroom_plug_consumption
  name: bedroom_plug_energy_spent
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.office_bulb1_consumption
  name: office_lamp1_energy_spent
  unit_prefix: k
  unit_time: h

- platform: integration
  source: sensor.livingroom_bulb_consumption
  name: livingroom_lamp_energy_spent
  unit_prefix: k
  unit_time: h

I first created template sensors to extract the W output for each object, then used the integration to convert the W s to kWh for each.
The output for the first template sensors is:

sensor.livingroom_plug_consumption
Livingroom Plug Consumption
	0.48 	unit_of_measurement: W
friendly_name: Livingroom Plug Consumption

sensor.office_bulb1_consumption
Office Lamp 1 Consumption
	5.8 	unit_of_measurement: W
friendly_name: Office Lamp 1 Consumption

but the output for the second set of sensors is:

sensor.livingroom_plug_energy_spent
livingroom_plug_energy_spent
	0.000 	state_class: measurement
last_reset: 2021-08-11T10:58:27.986192+00:00
source: sensor.livingroom_plug_consumption
unit_of_measurement: kWh
friendly_name: livingroom_plug_energy_spent
icon: mdi:chart-histogram

sensor.office_lamp1_energy_spent
office_lamp1_energy_spent
	0.000 	state_class: measurement
last_reset: 2021-08-11T10:58:27.986453+00:00
source: sensor.office_bulb1_consumption
unit_of_measurement: kWh
friendly_name: office_lamp1_energy_spent
icon: mdi:chart-histogram

Should I wait for it to gather more data, or is it something wrong with the template/integration sensors?
Any ideas?

1 Like

Can you double check for both the power meters and energy meters that the current state of those devices has the correct unit of measurement? For examples…

Power Meter
image

Energy Meter
image

Hi,
Thank you for the reply. I managed to fix it. I changed the device class from HA-> Customization - and now - after almost 24h - it started to show the consumption in kWh .
Thank you

1 Like

@lostprophet please could you give me steps to change device class from HA-> Customization?

Hi @dkovacic , I don’t think you can as there is no more the Customization menu - you need to do it from yaml - either in config, or where you defined the sensor.

I finally solved it. After I defined device class, initially there was no success. After that I added new sensor defintions, new names, same configuration, and those new sensors had correct unit of measurement kWh!

@dkovacic , what kind of integration method are you using?

image

The correct answer depends on your load.

If it’s an on / off load - like a light, dehumidifier, etc. Use Left otherwise you’ll get very inaccurate results.

If it’s a whole house energy meter that is constantly fluctuating, then trapezoidal may give you better accuracy.

1 Like

thank @PeteRage , in fact your answer covered 100% of my needs :smiley: