Temperature & Humidity compensated HC-SR04 possible with ESPHome?

I have some Arduino code the compensates the HC-SR04 distance reading based on changing speed of sound due to temp and humidity changes.

I’m struggling to implement this into ESPHome, can anyone give me some direction? I know how to use id’s to get the temp and humidity readings where they need to be but that’s as far as I can get.

void loop()
{
  delay(500);  //
   
    hum = sht31.readHumidity();  // Get Humidity value
    temp= sht31.readTemperature();  // Get Temperature value
    
    // Calculate the Speed of Sound in M/S
    soundsp = 331.4 + (0.606 * temp) + (0.0124 * hum);
    
    // Convert to cm/ms
    
    soundcm = soundsp / 10000;
    
  digitalWrite(trigPin, LOW);//setting trigger pin in LOW state
  delayMicroseconds(5);      //for 5 microseconds
 
  digitalWrite(trigPin, HIGH);  //setting trigger pin in HIGH state
  delayMicroseconds(10);       //for 10 microsecond and emits 8pulses from the transmitter and bounces back onces hit by an object
  digitalWrite(trigPin, LOW);  //after recieving the signal,setting the trigger pin to LOW state

  duration = pulseIn(echoPin, HIGH); //duration is a variable that starts timing once echo pin is high by using  arduino  function pulseIn()
  
  // Calculate the distance
  distance = (duration / 2) * soundcm;

…this is what I have done and seems to be working. It assumes the distance calc being done by ESPHome uses the default velocity for sound of 343 m/s. Also, I have assumed that the distance being returned raw by the sensor is the “over-and-back” distance, and so I divide by two, which produces distances close to what the my actual distance being measured is. However my sensor jumps around a lot (!) and I am trying to figure out why…

  - platform: ultrasonic
    trigger_pin: GPIO2
    echo_pin: GPIO12
    name: "Ultrasonic Sensor"
    id: car_presence
    filters:
      - lambda: |-
          return (x/2.0)*((331.4+(0.606*id(temp).state)+(0.0124*id(hum).state))/343.0);
      - median:
          window_size: 11
          send_every: 11
          send_first_at: 11
    update_interval: 1min
    pulse_time: 20us
    timeout: 2.0m

I have just read some other posts on the SR04 that state ESPHome does divide by two, however I am completely puzzled, as I have carefully measured the distance from the sensor to the top of my car when parked underneath, and it is (51cm) just about half of what the returned measurements are (all just over 1.1m) – perhaps the “divide by two” is missing somewhere ? However the temp and humidity correction seems to be working OK (they are measured with a DHT22).

Below are some measurements from a few minutes ago, the “resulting value” from the averaging window, AND my divide-by-two in my config is almost dead on to my tape-measured value, and shows that what the SR04 is returning is not being divided by two ?.. maybe I got a REALLY bad SR04 sensor ?

[23:09:39][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 1.139 m
[23:10:39][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.962 m
[23:11:39][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 0.962 m
[23:12:39][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 1.121 m
[23:13:39][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 1.020 m
[23:14:39][D][ultrasonic.sensor:040]: 'Ultrasonic Sensor' - Got distance: 1.020 m

[23:14:39][D][sensor:093]: 'Ultrasonic Sensor': Sending state 0.52534 m with 2 decimals of accuracy

as well, you can see the actual measurements jump around a bit, up to 17cm or so…

*** EDIT ***-- well, after changing out the SR04 for a new one, all is working more than great. Oscillations are gone, and the calculations of distance are dead on, no need to divide by two, as this is allready being done by ESPHome already.