How to filter sensor to get monotone increasing values

I have a sensor which reports the ODO of my car to HA. Sometimes I get invalid values (or no value) so ODO drops to 0.
How can I achieve a monotonously increasing sensor with HA?
That is: keep the last value, if ODO reports as 0 or lower than the last value.
The catch : I don’t want to filter against upper limit, as it is possible to drive without the sender enabled, so the next time it gets enabled, there may a jump from lets say 10,000 to 10,050

I think a template sensor would be best that ignores certain values.

How would I compare the incoming value to the last value from HA?
Is it possible to compare a (template)sensor value with itself?
like pseudo coded:

odo_templated:
   value_template: 
       if  sensor.real_odo < sensor.odo_templated 
          sensor.odo_templated 
       else  
          sensor.real_odo
       endif

Oh, I’m sorry I overread that part, I thought you only want to ignore 0 values. Not sure if it is possible to reference the entity itself in the template that defines the entity.

yeah, it actually is OK to do that. I have a few sensors that work like that.

That is pretty much exactly how you do it:

odo_templated:
  value_template: >
    {% if  states('sensor.real_odo') | int < states('sensor.odo_templated') | int %}
      {{ states('sensor.odo_templated') }}
    {% else %}
      {{ states('sensor.real_odo') }}
    {% endif %}
2 Likes

Have it like that running for 2days now, works great.

2 Likes