Sensor: "value_template" - how to filter "unavailable" values

There is a “fan” integration providing a “fan.xiaomi_mijia_300_1” object.
The object has a state and a few attributes including “pm25”.
Now I need a sensor for this particular attribute.

I have been using the following method (very popular here btw):

sensor:
  - platform: template
      mijia_300_1_pm25:
        friendly_name: 'Xiaomi Mijia-300 PM2.5'
        ...
        value_template: "{% set fan_name = 'mijia_300_1' -%}
                         {%- if state_attr('fan.xiaomi_' + fan_name, 'pm25') -%}
                         {{ state_attr('fan.xiaomi_' + fan_name,'pm25') }}
                         {%- else -%}
                         {{ states('sensor.' + fan_name +'_pm25') }}
                         {%- endif %}"

Here was the logic:
a) If the “fan” object is not ready then use the previous value of the sensor.
b) Otherwise - use a value of the object’s attribute.

OK, it does work - now I do not have “strange” values of “pm25” in the History since these “strange” values (like “99999” or “sudden 0”) in 99% take place in case of unavailability of the “fan” object.

BUT - what if “0” value is NORMAL for this particular attribute?
How can we differ these cases - “0 because the object is not available” and “0 because it is actually 0” ?

Conclusion here: