Help with filters

I have a HX711 weight sensor and I’m trying to limit what data it returns to HA. Right now what I have below works almost all the time, however a few times a day HA will receive a crazy huge negative number like -400 for example. I am fine with, and would actually like to see actual negative numbers if they ever happen, but prevent extremes.

I tried to use the lambda - but I am confused on the order of operations of the different filter components. Does any one know what part of what’s below could be allowing (-400) through the filter? Is there something special about 0 for polynomials than needs calibration data points for negative numbers?

For reference, sensor value -140000 is about -10lbs.

sensor:
  - platform: hx711
    name: "Bed Weight"
    dout_pin: D4
    clk_pin: D3
    gain: 128
    update_interval: 1s
    accuracy_decimals: 1
    filters:
      - lambda: |-
          if (x >= -140000) return x;
          else return {};
      - calibrate_polynomial:
         degree: 3
         datapoints:
          - -135500 -> 0
          - -132500 -> 5
          - -128700 -> 10
          - -125500 -> 15
          - -119500 -> 20
          - -113600 -> 30
          - -106000 -> 60
          - -104000 -> 65
          - -102000 -> 70
          - -100000 -> 75
          - -98000 -> 80
          - -80800 -> 127
      - or:
          - throttle: 600s
          - delta: 0.5

My guess would that the polynomial filter produces the value.
For comparison I chucked your datapoints into Exel, plotted them on a graph and added a trendline (polynomial, order: 3). This shows that your filter could produce an output of -400 if the sensor input is around 0. ESPHome probably derives a slightly different polynom so the actual input value may be different.

Thanks! That was the exact issue! I changed the polynomial to be an order 2 and also changed the lambda function to avoid exactly 0, because it must fall back to that if something glitches or resets.

- calibrate_polynomial:
     degree: 2
if (x >= -140000 && x != 0) return x;
     else return {};
1 Like