Help! Counting power consumption with current_consumption - I'm stuck

Hi sorry in advance for typos, translation by deep.

Ok. I need to ask for some help.
I’ve searched through a really good amount of forms, threads, topics in the last 2 weeks, tested various settings and…poof.

I have a GoSund socket which I have connected to Tuya.
I have Tuya version 2 hooked up to Home Assistant.

I have 3 entities in my devices:
1 - mA
2 - Watts
3 - Volt
4 - On/Off

I am trying to use the 2nd entity to count the power consumption on this outlet.
In the application it somehow works and counts fine but in HA I fail.

And so I tried based on the forums to add for example:

sensor:

  • platform: statistics
    name: ‘Zm_stat_40’
    entity_id: sensor.test_zm_wat40
    sampling_size: 12
    precision: 5

sensors:
test_zm_wat40:
friendly_name: ‘Zm_Actual current consumption_1’
value_template: ‘{{ ((states(‘sensor.socket_dishwasher_2’) | float / 10000 ) | round(6) ) }}’"
unit_of_measurement: ‘kWh’
device_class: energy

That is, I have an entity that gives information about power consumption in watts something along the lines of (if I skip tuya and take by localtuya):

value_template: '{{ ((state_attr(‘switch.lt_g_dishwasher’, ‘current_consumption’)

I divide by 10000 because there is an offset of one 0 so instead of 1k I have 10k.

This gives the value of the current consumption in kWh, for example: 0.020
Then this goes to the statistics.

There I have:
sampling_size: 12
because (if I understand correctly) the value 60 is given if the reading is one per second. In my case it is every 5s, hence the value 12 (correct me if I am wrong).

And now if I turn on the dryer which consumes 1840 w then in 1 minute the power consumption should be
0.0306 kWh

And now if with:

  • platform: statistics
    name: ‘Zm_stat_40’

I make an entity that extracts the mean value, and in the other entity total:

test_zm_zuz40:
friendly_name: ‘Zm_zuz_total_4’
value_template: “{{ (state_attr(‘sensor.Zm_stat_40’, ‘mean’) | round(5) ) }}”
unit_of_measurement: ‘kWh’
device_class: energy

test_zm_zuz_t40:
friendly_name: ‘Zm_zuz_total_t_4’
value_template: ‘{{ (state_attr(‘sensor.Zm_stat_40’, ‘total’) | round(5) ) }}’
unit_of_measurement: ‘kWh’
device_class: energy

It doesn’t matter how I would divide it afterwards e.g.: value from entity: Zm_zuz_total_t_4 / (1000*60 (or 12))
or from an entity that takes data from mean, then in no way I get the value of current consumption as in tuya application.

I also tried to do in customize.yaml

sensor.test_kwh40:
friendly_name: Zm_energy_40
state_class: total_increasing
unit_of_measurement: ‘kWh’
device_class: energy
last_reset: ‘1970-01-01T00:00:00+00:00’

while in configuration.yaml it is as follows:

test_kwh40:
friendly_name: ‘Zm_kwh_5’
value_template: “{{ (state_attr(‘sensor.Zm_stat_40’, ‘total’) | round(5) ) }}”
unit_of_measurement: ‘kWh’
device_class: energy

I have the same.

In addition, I wanted to plug in the Energy tab, but no matter which entity, sensor, counter I plug in the values no matter how I multiply, divide do not give the actual consumption.

What am I doing wrong? Help…

Teaching aids:

To get it into the energy dashboard (and also just as a simple way to achieve what you are after):

  - platform: integration
    source: sensor.socket_dishwasher_2
    name: Dishwasher Conspumption
    unit_prefix: k
    round: 2

The source is whatever sensor is providing the instantaneous consumption in watts.
Docs: Integration - Riemann sum integral - Home Assistant

3 Likes

I am able to add to Energy but the problem is that it counts wrongly for example:
If the dryer consumes 1840 watts it should give the consumption after 1 minute:
0.0306 kWh, and through these entities, statistics gives really different values. Sometimes it counts well after 1 minute but after a whole day it is wrong.

You should be feeding the watts value directly from the device in to the Reimann Sum integration. The problem with doing any sort of calculation on the value, is that almost no device out there reports at a frequent rate, so you generally have no idea of how many reporting periods are in an hour. You only have to miss a few, or get a sudden batch of faster reports, and your calculations are thrown off. The only true way to DIY it, is to calculate how many seconds have passed between each report. I believe this is what the Reimann sum integration is doing. I use it to take my House instantaneous electricity consumption figure and provide me with a daily running total, which fairly accurately matches what I am reporting to the electricity company.

2 Likes

Oh my!
I don’t know how to thank you.
My wife is almost hanging me because I’m sitting on it for another day :slight_smile:

I just did a test and it came out: 0.048 kWh, unfortunately this is discrepant with the real value of 0.0306 kWh.

Is it possible to do something else with it?
There is already progress in this

As mentioned in the docs, if the device is “spiky” in it’s consumption, you might want to move to a different method of tracking it’s consumption. The default is trapezoidal, but if you change it to left, it might provide more accurate results.

So add:

    method: left

in to the sensor.

3 Likes

Yes, it did help.
Thank you very much for your help.
That’s what I appreciate independent internet forum and help from people in the world for.

Once again, a big thank you.

3 Likes

The Reimann sum keeps adding the total consumption everyday, is there à way to calculate only today’s consumation ?