Integration Sensor Component Needs Reset Service

The recently introduced (0.87.0) Integration Sensor is a great idea but needs a reset service to reset the integration in say an hour to calculate kWh from a kW sensor over an hour period.
The reset would be done in an automation service call.

Please see the Counter component for an implementation example.

The integration sensor already calculates kWh from kW…

Yes, but how to reset the sensor at midnight to follow the consumption (kWh) day by day?

You can use the utility_meter component for that

1 Like

Apparently, sensors cannot have services. The Counter component has its own domain so it can have a reset service.
For a brute force approach one could try calling a Python Script as a service, containing this one line:
hass.states.set(sensor.your_sensor_name, 0)

I second this request (or appropriate workaround).
Although the Utility meter component does a great job of taking care of the time of segmentation mentioned above, my problem is somewhat different. I created the integration sensor with no prefix (like “k”) this gave me calculations in wh, which I later changed… this resulted in the figure remaining, but now calculated in kwh. would basically like to reset it once of… I guess I could just delete and recreate them, but there are some dependencies already, and I’d rather check if there are no alternatives.

My workaround was an automation that calls a python script. It is the ultimate way to get exactly what you want. You can reset the integration and change the unit of measurement. I combined the Integration Sensor and the Utility Meter in this example.

but with this I can only track one energy value.
If I would like to know solar energy produced, EV charged energy, total house consumption Utility meter is not usable, right?

ok, got it, the utilites can be name however I want, I also can create several of them:

  - platform: integration
    source: sensor.consumption_power
    name: consumption_energy
    unit_prefix: k
    round: 3
  - platform: integration
    source: sensor.import_power
    name: import_energy
    unit_prefix: k
    round: 3
  - platform: integration
    source: sensor.export_power
    name: export_energy
    unit_prefix: k
    round: 3
  - platform: integration
    source: sensor.evchargingpower
    name: evcharging_energy
    unit_prefix: k
    round: 3
  - platform: integration
    source: sensor.devolo_home_control_metering_plug_mt02646_power_2
    name: pool_energy
    unit_prefix: k
    round: 3

utility_meter:
  daily_consumption_energy:
    source: sensor.consumption_energy
    cycle: daily
  daily_import_energy:
    source: sensor.import_energy
    cycle: daily
  daily_export_energy:
    source: sensor.export_energy
    cycle: daily
  daily_evcharging_energy:
    source: sensor.evcharging_energy
    cycle: daily
  daily_pool_energy:
    source: sensor.pool_energy
    cycle: daily

I also vote for this request to have an option to reset the Integration sensor. I use the Integration component to total the rainfall per day. I use the yr_precipitation sensor, which measures rainfall in mm/h. (and often registers rainfall as 0.1 or 0.2 mm/h). The integration sensor works very well, and it would be very elegant if it could be reset to 0 by an automation at the end of a 24 hour period. I have now also tried the Utility Meter component, but it seems to me it is not set-up to total these small numbers (does it only work with integers and not with float?) and I get different totals compared with the Integration sensor.

1 Like

Have you tried my workaround using a Python script?
Use an input number entity as an accumulator the reset it with something like:
hass.states.set(input_number_energy_acum, 0, {"unit_of_measurement": energy_unit})
It works well but requires a little Python knowledge and an good example.

Utility meter should work flawlessly in your scenario, open a issue in GitHub and I will follow up

Also it would be great to get the runtime of the sensor in order to know over which period the integration was made. I do not mean the cycle.

Since my Blitzwolf SHP13 do not show power consumption (kWh) I used this integration

  - platform: integration
    source: sensor.blitzwolf5_tv_power
    name: blitzwolf5_energy
    unit_prefix: k
    round: 2

but it show me the following error:

2021-02-13 11:43:33 ERROR (MainThread) [homeassistant.components.sensor] Error adding entities for domain sensor with platform integration
Traceback (most recent call last):
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 315, in async_add_entities
await asyncio.gather(*tasks)
File “/usr/src/homeassistant/homeassistant/helpers/entity_platform.py”, line 506, in _async_add_entity
await entity.add_to_platform_finish()
File “/usr/src/homeassistant/homeassistant/helpers/entity.py”, line 530, in add_to_platform_finish
await self.async_added_to_hass()
File “/usr/src/homeassistant/homeassistant/components/integration/sensor.py”, line 125, in async_added_to_hass
self._state = Decimal(state.state)
decimal.InvalidOperation: [<class ‘decimal.ConversionSyntax’>]

can you trace the value of sensor.blitzwolf5_tv_power at the moment of the crash ?

I also seem to have the issue described in this thread
image
the integration of this is 46.08 kWh which seems off
image

It seems as if the integration is over the last 24 hours, not since the beginning of the day?

I fully agree with you, however I recently tried to solve this issue (still happening in 2022) for a water meter that gives one pulse per litre, now I was fiddling around with integration sensor and utility meter, tried statistics too, nothing really accomplishing what could have been. Finally I got the idea to treat the input as a binary sensor, in that case we can count the amount of times it turned ‘on’ which in my case corresponds to one litre. The history stats integration actually does a great job of counting the amount of on times and can easily be reset every hour, day, month. I also use the history stats to show the total usage over the past 24 hours.
esp32 code:

binary_sensor:
  - platform: gpio
    pin: 22
    name: binary_water

home assistant configuration code:

sensor:
  - platform: history_stats
    name: water_daily_usage
    entity_id: binary_sensor.binary_water
    state: "on"
    type: count
    start: "{{ now().replace(hour=0, minute=0, second=0) }}"
    end: "{{ now() }}"
  - platform: history_stats
    name: water_24_usage
    entity_id: binary_sensor.binary_water
    state: "on"
    type: count
    end: "{{ now() }}"
    duration:
        hours: 24

maybe this’ll help someone, I also know this won’t be useful for sensors that don’t use a pulse per unit used but the utility sensor really should be able to have handled this and integration too but here we are using binary sensor and history stats

Would also be great to have an option to integrate over a sliding window of time. I’m trying to integrate from a rain-intensity sensor to determine if I should close the windows or not. I don’t wan to close if I get a single spike at “3”, but I do if I get a bunch of densely packed "2"s…

Screen Shot 2022-06-07 at 07.39.04

For seeing the rainfall in the past minutes (or whatever time interval), you could also check out the statistics module and use something like the average value for a time window.

This is what I’m doing to (sort of) have an integration sensor reset to zero at midnight:

[Psuedo Code]
I have a sensor say sensor.Power
I use an integration helper (reimann integral left) to create sensor.EnergyTotal from sensor.Power
Then at midnight I set sensor.Midnight to sensor.EnergyTotal
Then I create sensor.EnergyToday to (sensor.EnergyTotal - sensor.Midnight)

Does that make sense?
You let the integration grow continually but capture its value at midnight and then calculate how much it has increased since midnight.

Hope it helps (clearly an ability to reset an integration sensor to zero at midnight would be much simpler)