Can i get raw data from HC-SR04 via ESPhome @ HA?

Hello all.

For the water level measurement at my cistern, I have so far used a HC-RS04 on a RPi3.
Now I have switched to HA and an ESP32. The HC-RS04 also runs here without any problems, but via HA I only get the already preprocessed data reported back in the form of the distance in meters. On the Pi I could read the raw data in the form of the signal runtime in ms and then process it as desired. For example, convert the propagation time dependence of the environmental temperature into the distance.

In HA I can use templates to convert the data, but for this I only have the preprocessed data available.

Is there a way to get the raw data?

Thanks for your help.

Hannah

There is no direct way to get the raw data, but you could create a template sensor and in its lambda just reverse the calculation done by ultrasonic component. The following method calculates the distance based on the roundtrip time:

float UltrasonicSensorComponent::us_to_m(uint32_t us) {
  const float speed_sound_m_per_s = 343.0f;
  const float time_s = us / 1e6f;
  const float total_dist = time_s * speed_sound_m_per_s;
  return total_dist / 2.0f;
}
1 Like

Ok… Thanks…
That confirms my guess that you can only calculate the values backwards.
Is there any information about which values were used. That for the speed of sound the value at 20° C was used, is surely only a guess or?

Well, I guess due to the lack of a built-in temperature sensor the developer had to decide on a value for the speed of sound.

BTW: I just realised that instead of going through a template sensor, if you only want to compensate for temperature differences you could do that with a lambda filter on the ultrasonic sensor directly.