jms3000
(Martin)
August 8, 2021, 4:50pm
1
The riemann integral is used to calculate for example the energy comsumption from the current consumption. Or if a sensor delivers “liters per second”, the riemann
integral calculates “total liters”.
The riemann integral integration uses the trapezoidal method as the default calculation method for the area under a curve.
But in in many cases this default is wrong.
Assume the temperature curve of a heating radiator which is polled every 10 minutes. If it is 20° at time t and 30° at time t+5, you can
assume that the temperature went up slowly from 20 to 30°. So the area under the curve is (20+30)/2 * 5 min. (20+30)/2 is the average temperature.
But now let us look at a cooking plate. It is off for 10 hours and it is switched to 2kW now. HA did not get any sensor readings for 10 hours,
because the value did not change. So the energy is calculated as (0kW + 2kW)/2 * 10 hours = 10 kWh. So for the first second you switch your cooking plate on,
HA calculates 10 kWh energy comsumption. This happens with all devices where the comsumption remains at a constant level for a longer time and then goes up.
For these cases, you can use the method:left which calculates the area under the curve only as old-value*time.
So for values which are continuously differentiable, the trapzoid method is ok, for values which change in steps (i.e. for the most electrical appliances), you should
use method: left.
The problem is that most people do not think about that problem and they will get totally wrong readings for their energy consumption.
Solutions for this problem:
make method: left the default
enhance the documentation and warn
introduce a new “method: optimal” which uses method:left if the elapsed_time is long or if dX/dY is big, i.e. the gradient is large. in all other cases use “method:trapezoidal”
tom_l
August 8, 2021, 5:06pm
2
+1
If I get time I’ll submit a PR to change this to a blue “Note” box tomorrow:
Or you could do it. https://github.com/home-assistant/home-assistant.io/blob/cdc0ec82cca77443d7d20e34f36d900416912edb/source/_integrations/integration.markdown
Don’t forget to vote for your own feature request.
1 Like
jms3000
(Martin)
August 8, 2021, 5:13pm
3
My oven and hot plate do not produce spikes. They go on and stay on for 10 minutes.
The problem is that they jump from off to on. They produce stairs. Also, all lights do that. For the trapezoidal method to be correct, you would need changing values which are not sent to HA. This is the case for devices which are polled every x minutes. But for all other devices which send their values as soon as they change, you have to use the “left” method.
tom_l
August 8, 2021, 5:59pm
4
That’s exactly what the author meant by "spikey consumption ", on/off rather than continuously variable. It is poorly worded. As I said, feel free to edit it.
jms3000
(Martin)
August 9, 2021, 7:23am
5
Maybe we can make a list of devices where the trapezoidal method is fine and those devices where the “left” method should be used.
Also if the device has been off for hours, the integration must not calculate the first value, because this value will be badly wrong.
I agree we should really make method “left” the default.
This issue keeps popping up, unfortunately taking our time, and producing poor integration results.
opened 12:19PM - 10 Sep 21 UTC
closed 12:22PM - 10 Sep 21 UTC
integration: integration
### The problem
I am using https://www.home-assistant.io/integrations/integra… tion/ to calculate kWh of my devices.
It works fine when there is a bigger load than 0, even if it stays constant for a long period of time.
However, I have noticed it doesn't properly calculate kWh when it jumps between "0" and anything more than 0.
See following example.
Here is my water heater, it either consumes 0 or maximum to heat water.
![image](https://user-images.githubusercontent.com/5785999/132851652-7a794580-b6c1-4e51-b99c-e570f0a0c7b5.png)
You can see it was turning on and off to keep the temperature of water. In the below screenshot from integration, focus on the last 4 loads only, I added sensor recently so its missing 24h data).
I am using this sensor to calculate kWh using the Integration:
```
sensor:
- platform: integration
source: sensor.garage_motor_current_consumption
name: Water Heater
unit_prefix: k
round: 2
```
And this is the result (focus on the last 4 loads only, I added sensor recently so its missing 24h data):
![image](https://user-images.githubusercontent.com/5785999/132851778-0d2beb91-5e38-47af-89f8-8cbf7e3a3ead.png)
You can see that it jumped way too much, seemingly ignoring the time when there was a 0 power consumption and assuming constant full load.
Comparing that to a sensor of total consumptions for the same socket for water heater from tplink:
![image](https://user-images.githubusercontent.com/5785999/132851955-6734158f-8db2-4025-b2b5-0b8388105375.png)
You can see that the integration numbers are wrong - during the night, keeping the temperature, it only used around 1kWh.
### What is version of Home Assistant Core has the issue?
core-2021.9.5
### What was the last working version of Home Assistant Core?
_No response_
### What type of installation are you running?
Home Assistant OS
### Integration causing the issue
integration
### Link to integration documentation on our website
https://www.home-assistant.io/integrations/integration/
### Example YAML snippet
_No response_
### Anything in the logs that might be useful for us?
_No response_
### Additional information
_No response_
opened 02:49AM - 24 Aug 21 UTC
closed 01:10PM - 24 Aug 21 UTC
integration: integration
### The problem
I noticed that with my calculation for export/import when using… the right method, I always go widely inaccurate results; even though from a mathematical standpoint it should be the best method.
I use a iotawatt sensor provides readings of a defined output for both import and export:
The sensor is defined as a calculation:
`Export = (Phase1 + Phase2 + Phase3 + Tesla1 + Tesla2 + Tesla3 + Pool + Solar1 + Solar2 + AC1 + AC2 + AC3 + HotWater) min 0`
`Import = (Phase1 + Phase2 + Phase3 + Tesla1 + Tesla2 + Tesla3 + Pool + Solar1 + Solar2 + AC1 + AC2 + AC3 + HotWater) max 0`
values for Import would be negative.
As the energy screen requires data that is positive; I use a template for that:
```
- platform: template
sensors:
power_grid_import:
friendly_name: "Grid Power Import"
unit_of_measurement: W
device_class: power
value_template: "{{ states('sensor.iotawatt_output_import')|float|abs }}"
availability_template: "{{ states('sensor.iotawatt_output_import') not in ['unavailable', 'unknown', 'none' ] }}"
attribute_templates:
last_reset: "{{ state_attr('sensor.iotawatt_output_import', 'last_reset') }}"
state_class: "{{ state_attr('sensor.iotawatt_output_import', 'state_class') }}"
power_grid_export:
friendly_name: "Grid Power Export"
unit_of_measurement: W
device_class: power
value_template: "{{ states('sensor.iotawatt_output_export')|float|abs }}"
availability_template: "{{ states('sensor.iotawatt_output_export') not in ['unavailable', 'unknown', 'none' ] }}"
attribute_templates:
last_reset: "{{ state_attr('sensor.iotawatt_output_export', 'last_reset') }}"
state_class: "{{ state_attr('sensor.iotawatt_output_export', 'state_class') }}"
```
I then use the `integration` integration as followed:
```
- platform: integration
source: sensor.power_grid_export
name: energy_grid_export_wh_r
unit_prefix: k
method: right
- platform: integration
source: sensor.power_grid_import
name: energy_grid_import_wh_r
unit_prefix: k
method: right
```
This however provides extremely inaccurate result .
Consider the history plot for the export power reading:
![e561f1d21575b4379f390c52465d4b539d2b9796](https://user-images.githubusercontent.com/505748/130547199-e3976808-f1e4-45b7-b74f-61087d8df58a.png)
However, when you look at the graph out of the integration:
![46fd1615fd045d3a675b501625ad1c48da288fd6](https://user-images.githubusercontent.com/505748/130547392-fe208e0a-4b02-4f4b-a3d0-42a51eb18753.png)
What appears to happen is that the power sensor will read 0, and with a utility_meter reset at 0
The first read that is not 0 is at around 9:00AM and has a value of 188W
This is calculated as 188W*9h = 1.69kWh for the first entry; which is obviously wrong.
In that graph again, we can see that it then reads 0 again, and then 2000W 30 minutes later and that again is calculated as an extra 1kWh
So you end up shortly with an energy calculated significantly higher that what it actually is.
With Trapezoidal you also see a similar issue, though not as bad ; left works fine as it only measure from the first read (left side)
![61fd84cd0b3569b404e5c8d874fbfae3a976027c](https://user-images.githubusercontent.com/505748/130547882-0ab734b9-e8c8-4315-8af6-fbf20af49732.jpeg)
This are the value plotted using all 3 methods; the actual value reported by the energy meter is 3.9kWh export and 19.3kWh import.
As you can see, the Right value (on the left) is almost 50% over-valued.
### What is version of Home Assistant Core has the issue?
core-2021.8.8
### What was the last working version of Home Assistant Core?
_No response_
### What type of installation are you running?
Home Assistant OS
### Integration causing the issue
integration (Riemann Sum)
### Link to integration documentation on our website
https://www.home-assistant.io/integrations/integration/#energy
### Example YAML snippet
_No response_
### Anything in the logs that might be useful for us?
_No response_
### Additional information
_No response_
opened 02:41PM - 13 Apr 21 UTC
closed 02:31PM - 11 Jun 21 UTC
integration: integration
### The problem
I have parameter an energy sensor like in documentation:
```
… - platform: integration
source: sensor.power_b
name: WallBox_energy
unit_prefix: k
round: 2
```
My goal is to estimate energy use for charging my EV per mounth and the cost (in finaly)
On my first test the integration calculate arround 29kWh, but 64h later on a second charge it's make and step of 96kWh before run normaly see above on left the power sensor history, on right the energy by integration
![bf0tDk](https://user-images.githubusercontent.com/4771050/114570614-42757500-9c76-11eb-8bc8-fc8948050152.png)
### What is version of Home Assistant Core has the issue?
core-2021.4.1
### What was the last working version of Home Assistant Core?
_No response_
### What type of installation are you running?
Home Assistant Core
### Integration causing the issue
Integration
### Link to integration documentation on our website
https://www.home-assistant.io/integrations/integration/
### Example YAML snippet
_No response_
### Anything in the logs that might be useful for us?
_No response_
I think it seems to be working fine …
Before 13:00 the cost is approximately 22.5 kWh x 0.07 E/kWh = E1.58
After 13:00 the cost is approximately 22.5 kWh x 0.14 E/kWh = E3.15
The jump in price appears to be caused by the PVPC sensor jumping which sounds like it comes from an external source. At least that’s the way I see it.
[2021-08-18_145245] [2021-08-18_145318]
Here they are @del13r
The two big spikes are from short turn-on of kettle, when I was testing this.
Integration sensor is working great for my power meter, which provides updates every 10 seconds, and is always doing something.
It’s no good for my gas sensor though, which provides updates every 15 minutes, and goes for long periods at 0 kW. Let’s say my gas usage has been 0 from 00:00 to 06:00. At 06:00 it jumps to 10 kW. The integration sensor interprets that as a Trapezium/Triangle gradually increasing from 0kW@00:00 to 10kW@6:00, or 30 kWh. Actually though, I’ve used 0 kWh.
[Capture] .
C…
My device does not calculate Ws, only the instantaneous W usage, from what I know. It’s this device/component for reference: https://esphome.io/components/sensor/pzem004t.html
Well, if I understood right, what do I need to provide?
Well, then we could say that the trapezoidal isn’t that great for energy measure, from what I’ve experienced. A lot of people probably use it unknowingly and wonder why it isn’t that accurate.
It’s called “pzem_004t_boiler_v3_power”.
I now use:
- platform: …
opened 08:40AM - 16 Sep 21 UTC
closed 07:42PM - 16 Sep 21 UTC
integration: integration
### The problem
There was a spurious peak in the energy dashboard:
![image](… https://user-images.githubusercontent.com/1636266/133574533-aa7bdfc5-4b48-4a23-913b-46c7b30ed871.png)
In our setup, that "grid export" value is calculated with:
```yaml
- platform: integration
source: sensor.grid_feed_in_w
name: grid_export_kwh
unit_prefix: k
unit: kWh
round: 3
```
So there seems to be an issue with the platform "integration" (Riemann sum integral).
The step occurred right after a HASS restart.
![Screenshot_20210916_093104](https://user-images.githubusercontent.com/1636266/133574374-5bc40824-289e-4f84-babd-9e72b4d34d85.png)
### What is version of Home Assistant Core has the issue?
core-2021.9.6
### What was the last working version of Home Assistant Core?
_No response_
### What type of installation are you running?
Home Assistant OS
### Integration causing the issue
platform: integration - Riemann sum integral
### Link to integration documentation on our website
https://www.home-assistant.io/integrations/integration/
### Example YAML snippet
_No response_
### Anything in the logs that might be useful for us?
_No response_
### Additional information
Related threads:
https://github.com/home-assistant/core/issues/56056
https://github.com/home-assistant/core/issues/55130
https://github.com/home-assistant/core/issues/49173
https://community.home-assistant.io/t/template-utility-meter-not-working-properly/302780/2
https://community.home-assistant.io/t/calculate-energy-from-smart-socket-that-reports-watts-only/331056/3
https://community.home-assistant.io/t/different-modes-for-integration-sensor/98827
https://community.home-assistant.io/t/integration-sensor-wrong-inaccurate/150544/43
https://community.home-assistant.io/t/riemann-integral-calculates-wrong-values-with-electrical-devices/328174
3 Likes
Apologies for not being a math wizard, but help me to explain; I am seeing a lot of ‘energy leakage’ in my home. What do I call energy leakage is the true power consumption (DSMR reader) and substract all the known energy from it (I have about 12 appliance monitors). During the day the amount of energy leakage always increases to about 3 kWh (which is about 30% of my consumption.
I do measure my fridge, TV, boiler, oven, dishwasher, dryer, washing machine etc and am using the default trapezoid for that.
Are you now suggesting I move all that to method left?
Yes, “left” should be the best option for you.
Updated all of them with the exclusion of 1 of them (which always shows very gradual consumption).
Will report back whether or not my energy leakage problem is solved.
2 Likes
Old topic, but problem still exists. Setup is:
sensor:
platform: integration
source: sensor.chargert_power_active_import
name: charged_energy_to_car
method: left
round: 3
Resul is high jump in value at beginning of electricity consumption. Any ideas?
Seems proper restart was needed.