Pellet stove hopper level, ultrasonic sensor and thermostat relay. ESPHOME

I went down a rabbit hole trying to do this sort of thing, but found it much easier (and still accurate enough) to just track stove run time using history_stats.

configuration.yaml:

sensor:
    # Track Pellet Fill
  - platform: history_stats
    name: Pellet Stove Runtime Since Fill
    entity_id: switch.pellet_stove
    state: "on"
    type: time
    start: "{{ state_attr('input_datetime.pellet_stove_last_filled', 'timestamp') }}"
    end: "{{ now() }}"

climate:
  - platform: generic_thermostat
    name: Pellet Stove
    heater: switch.pellet_stove
    target_sensor: sensor.pellet_stove_zone_mix
    min_cycle_duration:
      minutes: 15

Then just create an input_datetime for the time that you last filled the stove. I do the same for the time I last cleaned it. Once you establish known values you can further abstract it into a percentage.

EDIT: a few more details:
switch.pellet_stove is a zigbee relay ($17.90) that connects/disconnects the external room temperature probe
sensor.pellet_stove_zone_mix is a sensor group that mixes the values of a few zigbee air temp sensors ($19 each)

Hello!
I have a question! I’ve had the sensor mounted on my stove for a while. The main problem is that most of the time, it says it’s 98%. Once it starts working, it starts to take real values. Is this a vibration problem?

Vibration or slope of pellets
Might have to adjust bracket, location or angle.
I have some false reading when pellets are feeding but minimal and my automation adjust for that.
I’m very happy with mine.

changed the update interval to 5 min for less false readings.

  - platform: ultrasonic
    trigger_pin: D4
    echo_pin: D3
  
    name: "pellet level in percent"
    update_interval: 30s