ESPHome HCSR04 not updating correctly

Hi All,
I implemented a parking distance sensor using an HCSR04 and noticed that although the “got Distance” message was updating in real time, the ‘sensor.state’ seemed to randomly pick when it would update. So I deleted the lights portion and simplified it to this:

`esphome:
  name: garage-distance-sensor

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

web_server:
  port: 80

# Enable Home Assistant API
api:
  encryption:
    key: "kAdOhajh9bdfftyjftyoerrt/2HPAvQXRsm+FV6PI="

ota:
  password: "201a220e35802a469ef3957c76e5044"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Garage-Distance-Sensor"
    password: "garagelocal"

captive_portal:

globals:
  - id: direction
    type: int
    restore_value: no
    initial_value: "-1"
  - id: distance_int
    type: int
    restore_value: no
    initial_value: "0"

sensor:
  - platform: ultrasonic
    id: distancesensor
    trigger_pin: GPIO22  # pulse width in microSeconds 
    echo_pin: GPIO25     # uS/148 = inches /58=cm 38msec = no obstacle
    name: "Ultrasonic Sensor"
    update_interval: 500ms
    pulse_time: 10us
    timeout: 5m
    unit_of_measurement: 'cm'
    filters:
     - filter_out: nan
#     - sliding_window_moving_average:
#         window_size: 5 
#         send_every: 1 
#         send_first_at: 1        
     - delta : 1

  - platform: template
    name: "Distance"
    id: distance
    # unit_of_measurement: "ft"
    update_interval: 500ms  # Adjust as needed
    lambda: |- 
        return ((id(distancesensor).state*100));
    `

You can see I tried filters but commented it out to make it as simple as possible.

Using the web interface, I watched the values of both the sensor and the ‘distance’ that converts it to cm and cannot explain why the state is different from the “Got distance” messages:

14:44:56 [D] [sensor:125] ‘Distance’: Sending state 126.46410 with 1 decimals of accuracy
14:44:56 [D] [ultrasonic.sensor:040] ‘Ultrasonic Sensor’ - Got distance: 0.83 m
14:44:56 [D] [sensor:125] ‘Distance’: Sending state 126.46410 with 1 decimals of accuracy
14:44:57 [D] [ultrasonic.sensor:040] ‘Ultrasonic Sensor’ - Got distance: 0.83 m
14:44:57 [D] [sensor:125] ‘Distance’: Sending state 126.46410 with 1 decimals of accuracy
14:44:57 [D] [ultrasonic.sensor:040] ‘Ultrasonic Sensor’ - Got distance: 0.83 m
14:44:57 [D] [sensor:125] ‘Distance’: Sending state 126.46410 with 1 decimals of accuracy
14:44:58 [D] [ultrasonic.sensor:040] ‘Ultrasonic Sensor’ - Got distance: 0.82 m
14:44:58 [D] [sensor:125] ‘Distance’: Sending state 126.46410 with 1 decimals of accuracy

Notice that the "Got distance is around 0.83 meters but the sensor “Sending State message” shows 126.46410. My ‘distance’ sensor matches the sending state. But all the ‘Got distance’ messages are pretty accurate and real time.

What can I do to capture the real time’Got Distance’ values?

Thanks for your help.

Remove that -delta:1

…or replace it with 0.01

I came uon the same conclusion after first removing all filters then adding them back one at a time. The delta of 1 should allow it to report back when it changes by 1 which I thought it was, however since the reporting was in METERS, not centimeters, it was updating only every 100 cm. I thought by specifying unit of measurement ‘cm’ it would report centimeters.My error Thanks @Karosm and I hope others benefit from this.