Calculate energy from Smart Socket that reports Watts only

Hello All!

I am using Tuya V2 integration and that works all fine. However, sockets report only power draw in Watts, and do not report energy consumed in Wh or kWh. I have found this guide:

and (almost) successfully applied it to calculate kWh. However, I encounter one bug.
It is working fine when devices are powered and consuming energy for long periods of time. But when devices are turned off and powered back on in quick successions, it is adding huge amount of energy consumed like in screenshot:
2021-08-18_131629

2 devices are connected to this socket: speakers that draw a few Watts and kettle, that draws ~2 kW. When speakers are on, it is summing energy well from power, but when I turn on kettle, it adds random ~50/ 30/ 60 Wh instantly.

My configuration is:

template:
  - sensor:
        name: Grid Import Power
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ (states('sensor.smart_socket_1')|float + states('sensor.smart_socket_2')|float)|round(3) }}
  - sensor:
        name: Lampka pobór mocy
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ (states('sensor.smart_socket_1')|float)|round(3) }}
  - sensor:
        name: Czajnik pobór mocy
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ (states('sensor.smart_socket_2')|float)|round(3) }}

and:

  - platform: integration
    name: Grid Import Energy
    source: sensor.grid_import_power
    unit_prefix: k
    unit_time: h
  - platform: integration
    name: Czajnik pobrana energia
    source: sensor.czajnik_pobor_mocy
    unit_prefix: k
    unit_time: h
  - platform: integration
    name: Lampka pobrana energia
    source: sensor.lampka_pobor_mocy
    unit_prefix: k
    unit_time: h

Any idea how to implement kWh calculation from W properly? Thanks

You have shown your ‘Czajnik pobrana energia’ graph.
Please also show your graphs for
‘sensor.czajnik_pobor_mocy’
as well as
‘sensor.smart_socket_2’

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.

use method: left

method string (Optional, default: trapezoidal)

Riemann sum method to be used. Available methods are trapezoidal, left and right.

In case you have an appliance which produces spikey consumption (like an on/off electrical boiler) you should opt for the left method to get accurate readings.

2 Likes

Hey @cichy45

The first thing I will say is that you might have created ‘Lampka pobór mocy’ and ‘Czajnik pobór mocy’ without needing to.

Please go to dev tools / states and show me what is on the right column for sensor.smart_socket_1 and sensor.smart_socket_2

Next thing is most czajnik will consume 2000w when it is on as it is converting electical energy into heat energy so this figure is expected.

Your graph isnt saying you used 2 kWh, it just says you used a lot of power for a short amount of time which is true when you turn on high wattage device.

I’m not sure what the problem is.

Also, try @francisp suggestion of method: left for all integration sensors

@del13r

Lampka is just another socket, configured in the same way.
Dev tools:



this is smart_socket_2 and 1 (I just removed this ID in my main post for code simplicity)

I think that @francisp can be right. Maybe left integration would work better for this spike temporary load. I will have to test this and report back to you.

yes, I know this, but i think it summs W into Wh wrong with short burst of high-power devices.

What makes you say that though?

I thought this is what your issue was

If you hold down the button on the kettle at 2300W for a whole hour, you would consume 2.3kwh.
Divide that by 60 and you would consume 38Wh per minute.
Your graph shows it went to 196Wh so yes it is a little inaccurate.

It was adding like 60 W in the moment that kettle was turn on, even if for like 5 seconds (it could not consume this amount of energy).

Now I used

method: left

as @francisp mentioned, and now it seems to be calculating correctly. Kettle was running for 93 seconds with 2243 W of power. 93 s / 3600 s = 0.0258333333 hour * 2243 Watts = 58 Wh of energy. HA is showing 58.01 Wh of energy consumed, so it is working correctly with method: left.

The method: trapezoidal was probably assuming 2243 W of power form last state change (the last change before turning on kettle) and thus was adding many Wh right in moment the kettle was enabled.

1 Like

Cool, glad that fixed it.

This has prompted me to set left on my own integration sensors.

Now my other point was, why did you create these?

  - sensor:
        name: Lampka pobór mocy
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ (states('sensor.smart_socket_1')|float)|round(3) }}
  - sensor:
        name: Czajnik pobór mocy
        state_class: measurement
        icon: mdi:transmission-tower
        unit_of_measurement: W
        device_class: power
        state: >
            {{ (states('sensor.smart_socket_2')|float)|round(3) }}

when you already have

sensor.smart_socket_1
sensor.smart_socket_2

Just seems like you are recording 2 lots of the same information.
You can use customize.yaml or even go to configuration / customizations in the gui to add the missing

state_class: measurement 

to these sensors

1 Like

You are totally right, these are not needed. That are mistakes that you make following other guides late in the night. Thank you for pointing this out!

1 Like

Hi,

I had same problem this solution : method:left helped for some devices.

Unfortunately I made using weUI Integration for one of my sockets with standard value : trapezoid. It don’t work correctly. I need to switch to left.

How I can change it for “left”?