Issue with MQTT autodiscoverd sensor and value "0"

Hello everyone,
I’m a bit on the fence. I’ve googled a lot and read a lot in the forum, but haven’t got it right.
I have sensors that were found via automatic discovery and some that I had to create via Config.yaml.
An automatically recognised sensor “sensor.volkzaehler_mt681_total_in” based on Tasmota and MQTT sometimes sends the value “0”. This messes up the meter reading in the energy portal. I have already tried using Utility Helper via the GUI, but that was even worse. I found a few snippets if I had created the sensor via configuration.yaml. I already found out that the UI entries are saved differently, so I can’t copy and customise the yaml part.
The beginner documents didn’t help me either and neither did the examples for templates, because there was always something missing or my HA didn’t like me…
Does anyone have a tip for me please, I don’t always want to clean up the statistics by hand.

You could make a template “copy” of the troublesome sensor, and add an availability template:

template:
  - sensor:
    - name: "MT681 filtered"
      state: "{{ states('sensor.volkzaehler_mt681_total_in') }}"
      unit_of_measurement: XXX
      device_class: XXX
      state_class: XXX
      availability: "{{ states('sensor.volkzaehler_mt681_total_in')|int(0) > 0 }}"

then use that in the energy dashboard instead of the MQTT sensor. Replace the XXXs to match the existing sensor’s details.

You will need to add this to configuration.yaml, but if there is already a template: header like:

template: !include templates.yaml

then put it in templates.yaml instead, but leave out the first line, so it starts with - sensor:.

That will then be unavailable when the returned value is 0, which should not affect the statistics so badly.

Hi Troon,
It looks like it works, but time will tell. Thanks