Question: Energy Meter - Integration - Riemann Sum Integral

I’m using the “Integration - Riemann Sum Integral” to calculate energy used.

The power meters I am using are reporting power in watts. The documentation says that based on my configuration the energy sensor (config below) should be reporting kWh, but my sensors are reporting Wh (watt hours). Anyone have a suggestion? I realize I could make another sensor to divide this number by 1000 to get kWh, was just trying to avoid that if I could.

Documentation Example

  - platform: integration
    source: sensor.current_power
    name: energy_spent
    unit_prefix: k
    round: 2

My Configuration

  - platform: integration
    source: sensor.network_power_meter
    name: Network Energy Meter
    unit_prefix: k
    round: 2

does sensor.network_power_meter have a unit_of_measurement?

I think so, at least it displays with one.

Yes, it does…

Did you by chance create this at first without unit_prefix? If so, you just need to wait a bit for the database to purge. If not, you can try setting the unit_of_measurement by hand.

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

It doesn’t take a unit_of_measurement value.

Invalid config for [sensor.integration]: [unit_of_measurement] is an invalid option for [sensor.integration]. Check: sensor.integration->unit_of_measurement. (See ?, line ?).

It takes…

In the case of setting the “unit”, would I set that to W or kW? I think I tried kW before and it didn’t change anything…

Neither worked… Tried W and kW…

unit_of_measurement should work, it’s still in the schema.

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Optional(CONF_NAME): cv.string,
        vol.Required(CONF_SOURCE_SENSOR): cv.entity_id,
        vol.Optional(CONF_ROUND_DIGITS, default=DEFAULT_ROUND): vol.Coerce(int),
        vol.Optional(CONF_UNIT_PREFIX, default=None): vol.In(UNIT_PREFIXES),
        vol.Optional(CONF_UNIT_TIME, default=TIME_HOURS): vol.In(UNIT_TIME),
        vol.Optional(CONF_UNIT_OF_MEASUREMENT): cv.string, <---------------------------------
        vol.Optional(CONF_METHOD, default=TRAPEZOIDAL_METHOD): vol.In(
            INTEGRATION_METHOD
        ),
    }
)

Either way, can you answer this question:

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