ESPHome Energy meter kWh

I’m trying to get a ESPHome energy meter working without any success.
I followed the guide here Non-Invasive Power Meter.
I receive the counts just fine every 60th second and the value shows up in Home Asssistant but when I try to use the Utility Meter Integration it’s not working correctly.

NodeMCU config looks like this:

sensor:
  - platform: pulse_counter
    pin: GPIO12 #D6
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    internal_filter: 29ms
    unit_of_measurement: 'kW'
    name: 'Värme'
    filters:
      - multiply: 0.06
    id: power1

As I understand it, this will produce a sensor that the Utility Meter Integration can use? This is the config:

  hourly_varme_usage:
    source: sensor.varme
    cycle: hourly
    tariffs:
      - peak
  daily_varme_usage:
    source: sensor.varme
    cycle: daily
    tariffs:
      - peak

When I look at the readings after a few minutes (or hours) the values are too high. I have a digital energy meter in the main fuse box that I take the readings from so I know they are incorrect in Home Assistant. My guess is that the conversion from pulses/min to kWh is where the problem lies. Any tips will be greatly appreciated.

how did it go? will soon start my bulid:D

I got it working using Node-red. The trick was to accumulate the value and send it to the Utility Meter.

My solution isn’t very elegant but it works good. Feel free to let me know if you need any help.

Can You show Your solutions? Im trying to do this but readnings looks bad.

I had to change the NodeMCU config a bit.

sensor:
  - platform: pulse_counter
    pin: GPIO12 #D6
    count_mode:
      rising_edge: DISABLE
      falling_edge: INCREMENT
    internal_filter: 29ms
    unit_of_measurement: 'Wh'
    name: 'Värme'
    accuracy_decimals: 0
    id: power1

If you import this into Node-red you should be able to see how I solved it. The sensor that the flow outputs is what I used in Home assistant.

[{"id":"7738c465.999994","type":"mqtt in","z":"2ac8cf2d.a326d","name":"Wh String","topic":"power1/sensor/heat/state","qos":"0","datatype":"auto","broker":"a9164020.d4d1d8","x":160,"y":160,"wires":[["1fe5ac5.76003d4"]]},{"id":"1fe5ac5.76003d4","type":"function","z":"2ac8cf2d.a326d","name":"Convert string to integer","func":"msg.payload = Number(msg.payload);\nreturn msg;","outputs":1,"noerr":0,"x":490,"y":160,"wires":[["b76c6317.edd5f"]]},{"id":"597e468c.f58","type":"mqtt out","z":"2ac8cf2d.a326d","name":"Wh Accumulated","topic":"sensor/heat/wh/ack","qos":"0","retain":"","broker":"a9164020.d4d1d8","x":1330,"y":160,"wires":[]},{"id":"b76c6317.edd5f","type":"totaliser","z":"2ac8cf2d.a326d","output":"total","interval":"1","intervalUnits":"minutes","name":"Accumulate","x":770,"y":160,"wires":[["76967d1.4acc404"]]},{"id":"76967d1.4acc404","type":"change","z":"2ac8cf2d.a326d","name":"Extract Total","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.total","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":990,"y":160,"wires":[["597e468c.f58","a3d86d6f.8124b8"]]},{"id":"a3d86d6f.8124b8","type":"debug","z":"2ac8cf2d.a326d","name":"Output Total","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1310,"y":240,"wires":[]},{"id":"a9164020.d4d1d8","type":"mqtt-broker","z":"","name":"Mosquitto","broker":"192.168.0.212","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

And in Home assistant I did this:

sensor:
  - platform: mqtt
    name: "Värme Wh"
    state_topic: "sensor/heat/wh/ack"

      varme_kwh:
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.varme_wh')|float / 1000  }}"

That produced two sensors, one for Wh and one for kWh. I then used the kWh sensor for the energy meter, like this:

utility_meter:
  yearly_varme_usage:
    source: sensor.varme_kwh
    cycle: yearly
    tariffs:
      - peak

Like I said, not the most elegant solution but it works. Hope it helps!

Im also struggling to get this right, but I think the Integration component (https://www.home-assistant.io/integrations/integration/) could be used to accumulate over a time period…

I don’t know if you are still interested in this, but why aren’t you guys just using the ESPHome built in integration? https://esphome.io/components/sensor/total_daily_energy.html

You can use this for daily accumulation.

Home assistant Integration component:

"An integration sensor is quite useful in energy billing scenarios since energy is generally billed in kWh and many sensors provide power in W (Watts).

ESPHome Total Daily Energy Sensor:

So this component allows you to convert readings in W or kW to readings of the total daily energy usage in Wh or kWh .

The problem is that my sensor provides Wh, not W. 1 blink = 1 Wh.
If you know the difference between power and energy you understand it.
Of course there must be a better solution than what I’m using right now, but I haven’t got the imagination or knowledge to figure it out :frowning_face:

that’s not how the sensor works. you have a multiplier in the config which converts your pulses, which unit corresponds [kWh] like you said, to [kW]. ESPhome says

The output from the pulse counter sensor is in pulses/min.

So your pulses [kWh] are divided by the time [min] (that’s why it is calculating the pulses per min) . Hence the unit of measurement is [kW]. ESPHome says:

Thus, rearranging the expression yields a proportional factor of 0.06 from pulses/min to kW

With this information the Total Daily Energy Sensor of ESPHome can accumulate the total energy again in [kWh].
And yes, your sensor is just counting pulses (energy in [Wh]) because you didn’t define the multiplier:

filters:
      - multiply: 0.06 (This number is calculated by dividing 60 with the imp/kWh of your power meter)

Btw: It looks like this way of calculation is a circle, but this circle is important. Counting just pulses would show you just the sum of the energy. But with this circle you know how big the energy consumption was in every minute/hour/etc… :slight_smile:

And yes, as an engineer I guess I know a bit about power, energy and co. :stuck_out_tongue:

3 Likes