Energy Dashboard reporting incorrect kWh with WemosEM (MQTT) after core upgrade

Hi,

Energy prices are rising, and many people are shocked and bewildered by high energy bills. This setup is for friends that want to better understand their energy usage and modify their behavior to not waste electricity and receive lower bills.
Since the WemosEM hardware is not on the same subnet as my server, I was unable to use ESPHOME, so I am using hardware/firmware/MQTT as described - GitHub - alcar21/WemosEM: Cheap power monitor (STC013) with Home Assistant Discover and Wemos D1 Mini / PRO (esp8266) - to bring KwH into the Energy dashboard.

I have set up Home Assistant with public IP on VM on my server.
The problem is that after a core upgrade the energy dashboard is showing x 3 the actual KwH.




I would be very grateful to anyone that can help me modify the configuration so that Home Assistant Energy Dashboard provides accurate kWh values

The configuration.yaml:

mqtt:
  sensor:
    - state_topic: "wemos/wemosEM-51187C/power"
      name: WemosEM-KWh
      icon: mdi:counter
      unit_of_measurement: "kWh"
      value_template: "{{ value_json.kwh | float  | round (2) }}"
      device_class: energy
      state_class: total_increasing

Try this

      value_template: "{{ value_json.kwh | float(0)  | round (2) }}"

Thanks, I really appreciate your help and suggestions. I have tried float(0) in place of float as suggested. I tried rebooting Home Assistant, and the “jump” in values did not happen. I am not really sure how else to test the change. Any suggestions?

This template will not solve your issue.

The problem occurs when value_json.kwh is not a number or is undefined. Without a default value the template fails.

Supplying a default value helps but will still cause the issue if the default is 0.

If the energy goes from some total → 0 → some total then the energy dashboard registers that jump from zero to the total as an amount of energy used.

If the energy goes some total → unknown/unavailable → some total then this jump does not occur.

In template sensors the unavailable state can be set using an availability template. Unfortunately this is not a valid option for an mqtt sensor. So you have to build this into your value template.

Some possible solutions:

  1. keep the last value if the mqtt message is not a number:
value_template: "{{ if value_json.kwh|is_number value_json.kwh | float | round (2) else this.state }}"
  1. Or set the value to unavailable if the mqtt message is not a number:
value_template: "{{ value_json.kwh | float(none) | round (2, default = none) }}"

Both untested but either should work.

Hi,
A quick update.
Frank’s solution worked well for a few restarts and then broke.
Tom’s solution 2 has been working for a week.
Thank you both for your help!

1 Like