Calculating Watts / Kw and KWh

Make a template sensor for the W. You already have the equation and it’s written here enough.

  - platform: template
    sensors:
      power_watts:
        friendly_name: "Power Watts"
        unit_of_measurement: 'W'
        value_template: >
          {% set PF = state_attr(input_number.power_factor.state | int ) %}
          {% set V = state_attr('sensor.pow_voltage', 'V') %}
          {% set A = state_attr('sensor.measured_current', 'A') %}
          {{ PP * A * V }}

Now, to properly calculate kWh, you want to use the integration integration.

This is where things get tricky. There’s a field called method. Choosing the correct method is all about math. I’ll make a quick synopsis of the styles:

trapezoidal

This method averages 2 points of data and gets the area under the curve. See image below.

image

Pros:

  • Accurate if your data is constantly changing.

Cons:

  • Not accurate if your data stays the same value for prolonged periods of time. If the next piece of data is wildly different, it will add a ton of error.

I recommend only using this method if your data is always changing and it has a ‘roll’ to it like a sine wave.

sensor:
  - platform: integration
    source: sensor.power_watts
    name: Energy Spent
    unit_prefix: k
    method: trapezoidal
    round: 2

left / right

This method chooses a point on the line and assumes it’s that value until the next point is received. For right, it’s reversed.

image

Pros:

  • Accurate if you have prolonged periods of data spikes (Think square waves).

Cons:

  • When rising at a slow rate, the area under the curve always contains error. Positive error (right), or negative error(left). This is opposite when falling at a slow rate.
sensor:
  - platform: integration
    source: sensor.power_watts
    name: Energy Spent
    unit_prefix: k
    method: left
    round: 2

  - platform: integration
    source: sensor.power_watts
    name: Energy Spent
    unit_prefix: k
    method: right
    round: 2

tldr:
Use left/right if you have momentary watt spikes like a square wave. Use trapezoidal when you have rounding peaks and valleys.

7 Likes