Filter invalid spikes from sensor

I have an ESPHome sensor reading the levels in my water tank. The ESPHome device wakes up every 4-hours, takes 3 readings and goes back to sleep. For some reason I occasionally get these massive spikes that are incorrect. I’d just like it to ignore anything outside the 0-100 range, it seems like the Filter outlier sensor is taking some kind of mean.

I tried using a filter sensor but that didn’t work. Maybe because the sample size is so small? I don’t quite understand it. Below is my filter sensor (not working), some screenshots, and my ESPHome config. I’m open to solving this on the ESPHome side as well.

Spikes I’m trying to filter out:

Filter Sensor(not working)

- platform: filter
  name: "Filtered Water Tank Sensor Percent"
  unique_id: "filtered_water_tank_sensor_percent"
  entity_id: sensor.water_tank_sensor_percent
  filters:
    - filter: outlier
      window_size: 4

ESPHome yaml

esphome:
  name: water-tank-sensor-v3
  on_boot:
    then:
      - output.turn_on: water_tank_sensor_relay
      - delay: 3s
  on_shutdown:
    then:
      - output.turn_off: water_tank_sensor_relay
      - logger.log: "Goodnight sweet prince"

esp32:
  board: esp32-s3-devkitc-1
  variant: ESP32S3
  framework:
    type: arduino
    version: 2.0.3
    platform_version: 5.1.1

# Deep sleep
deep_sleep:
  run_duration: 15s
  sleep_duration: 4h


# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: [redacted]

ota:
  password: [redacted]

# wifi settings
wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  use_address: 192.168.20.128
  fast_connect: true
  power_save_mode: none
  manual_ip:
    static_ip: 192.168.20.128
    gateway: 192.168.20.1
    subnet: 255.255.255.0

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Water-Tank-Sensor-V3"
    password: [redacted]

captive_portal:

mqtt:
  broker: !secret mqtt_broker
  username: !secret mqtt_un
  password: !secret mqtt_pw
  birth_message:
  will_message:


output:
  - platform: gpio
    pin: GPIO5
    inverted: True
    id: water_tank_sensor_relay

sensor:
  - platform: adc
    pin: GPIO10
    update_interval: 2s
    id: "water_tank_sensor_voltage_v3"
    name: "Water Tank Sensor Voltage v3"
    accuracy_decimals: 3
    icon: "mdi:water-percent"
    filters:
    - multiply: 3.3
    - median:
        window_size: 3
        send_every: 3
        send_first_at: 3

Hi Hummingbear,

I’m no expert but I experienced random spikes when I set up my electricity meter monitor - I tried all sorts of things to try and ‘cure’ it. In the end, I added the internal pull-up resistor to the sensor input pin, in your case GPIO10 by adding some yaml code.

Before pulling all your hair out and substituting sensors etc (as I did) try adding the internal pull-up / pull-down to your code first to see if it resolves the problem. More details can be found here in the Docs.

This is the yaml I added…

pin: 
	number: GPIO27 #change this to the GPIO pin you are using on the ESP32
	mode: 
	  input: true
	  pullup: true

It certainly cured my random spikes.

I hope that helps. :+1:t2:

Cheers,

Mike.