Template sensor with calibrate_linear or similiar?

Hi!

I have an ESPHome ultrasonic sensor that use the calibrate_linear to convert distance to percent:

- platform: ultrasonic
  trigger_pin: GPIO18
  echo_pin: GPIO19
  name: "Silo"
  unit_of_measurement: "%"
  accuracy_decimals: 0
  filters:
    - median:
  update_interval: 10s
    - calibrate_linear:
       method: least_squares
       datapoints:
        - 0.0 -> 100
       - 0.65 -> 0.0

When the tank is full (distance from the sensor is 0 cm.) it is 100%. When it is empty (distance is 65 cm.) it is 0%. This works fine for my needs.

However, I would like to create a template sensor for Home Assistant (not using ESPHome) that do the same conversion for another sensor also measuring distance.

I can’t find any documentation if the calibrate_linear feature is available for a template sensor.

What would be the easiest way of creating a template sensor doing this kind of conversion?

Use this integration instead of a template sensor:

It works exactly the same as ESPHome’s calibrate filters.

Even better actually as you can define limits if you need to.

1 Like

Ah, great! I didn’t know about this integration! It seems to be excactly what I was looking for.

Would this give the same result as my previous ESPHome configuration?

compensation:
  ultrasonic_sensor_percent:
    source: sensor.ultrasonic_sensor
    unit_of_measurement: "%"
    data_points:
      - [0.0, 100]
      - [0.65, 0.0]

Yes

1 Like