MQTT value_template with ignore of 0 or null

Hi guys,

atm i’m extracting my state of my sensor with:

- platform: mqtt
  name: "Zaehlerstand Strom"
  state_topic: "tele/hwrzaehler/SENSOR"
  unit_of_measurement: "kWh"
  value_template: "{{ value_json.Strom.Eges }}"

This is a power sensor and shows my actual Total Power Consumption in kWh.

But somehow, when the sensor reports a value of 0 or null, my whole energy monitoring messes up.
Is it possible to ignore those values? For exampe if my sensor has a value of “12345” and in the next step “0” … is it possible to ignore the 0 until it has again a “real state” with 12345 or higher?

Would this help? I never tried it really, but the example uses 90% of current value and 10% of previous value - you could easily adjust it to use previous state if current state equals to 0, for example.

- platform: mqtt
  name: "Zaehlerstand Strom"
  state_topic: "tele/hwrzaehler/SENSOR"
  unit_of_measurement: "kWh"
  value_template: "{{ value_json.Strom.Eges if value_json.Strom.Eges > 0 else states('sensor.zaehlerstand_strom') }}"

tbh i saw this but didn’t know how to use it…

I will try that out and give feedback, thank you!