Filter 0 values from Total Enery

I have a tasmota SML sensor for the optical port of my power meter. This sensor gives me the total power consumed of the power meter over MQTT. When the sensor restarts, it sends 0 kWh for a couple of times, until it gets a new value from the power meter. Now I have spikes in my power usage every now and then, when it jumps from 0kWh to the actual total (appros 1500kWh). I tried to filter the value with:

sensor:  
  - platform: filter
    name: "Apator Total In Filtered"
    unique_id: apator_total_in_filtered
    entity_id: sensor.tasmota_sml_apator_total_in
    filters:
      - filter: outlier
        window_size: 3
        radius: 10
        precision: 2

but this doesn’t work, because when the sensor gets no new value from the power meter for a longer time (a few seconds) it will outrun the window_size - and I can’t just increase the window_size, because I can imagine that the sensor could be without power for a couple of minutes (e.g. when doing some work on the electrical installation).


As you can see in the picture, with the filter: outlier method, that is proposed on similar threads, the filtered sensor just does the same like the original sensor, just a bit later.

Is there a way, to just ignore 0kWh values from a sensor?

ok after a lot of try and error, I found a solution that does exactly what I want, and I wonder why I could not find this quiet simple solution in the internet. All the solutions I could find were workarounds to filter 0 values, which sometimes worked, sometimes not.
I created a template that simply checks if the power meter gives a value greater than 0, and if so, it takes the value.

template:
  - sensor:
      - name: "Tasmota SMI Filtered"
        unit_of_measurement: kWh
        state: >
          {% set state1 = states('sensor.tasmota_sml_apator_total_in')|float(none) %}
          {% if state1 > 0 %}
            {{ state1 }}
          {% endif %}
        device_class: energy
        state_class: total_increasing
        unique_id: tasmota_smi_filtered
        availability: >
          {{ state1 != none }}

In the history you can see the line of the new template sensor. Everytime the line is interrupted, the tasmota sensor was down or rebooting. Whenever tasmota would send 0 kWh or home assistant would interprete the lack of information as 0 kWh, my new sensor simply has no value for that time, just as I wanted it to be.

I also found something that should have solved this problem at the root, so in tasmota, but it didn’t work out in home assistant. The idea is to stop tasmota from sending values for the first x seconds after boot or reboot, so that the power meter has time to send data to the tasmota sensor. After x seconds, sending of values to home assistant is enabled. This first part works for me, tasmota will wait 35 seconds in my case (because my power meter sends its value every 30 seconds, plus some 5 seconds extra) and by this, will never send 0 kWh, but always an actual reading from the power meter. But unfortunately, when the tasmota sensor sends no values, the home assistant sensor will automatically show 0 kWh, which then again causes the problem. If you are interessted in that, search for

smlj=0

in the Tastmota Smart Meter Interface documentation

Maybe this is useful for someone :slight_smile:

Did you look at

  filters:
    - filter_out: 0

?

This is ESPhome - only!

1 Like

Good point. I misread the OP as being ESHome YAML. Looking again it’s clearly not, nor is the post category… :confounded: