Help on Templating in ESPHome convert volts to pressure

Hi, I have added a pressure sensor to my ESP Home D1 Mini, Range is 0.5V to 5V, 2.5V = 400kpa

  - platform: adc
    pin: A0
    name: "Mains Water Pressure"
    update_interval: 10s
    filters:
      - multiply: 3.3

This give me the voltage from the sensor, so I want to Subtract 0.5 (v) from the A/D and multiply by 400 and retrun the result to HA.

Would be grateful on some help. or a good YouTube Video on how you do calculations in ESP Home.

Kind regards Dave

Use the calibrate_linear filter.

  - platform: adc
    pin: A0
    name: "Mains Water Pressure"
    update_interval: 10s
    filters:
      - multiply: 3.3
      - calibrate_linear:
          - 0.0 -> 0.0
          - 2.5 -> 400

I would normally apply the filter to a Copy sensor, so you can monitor the raw values for a while and establish more datapoints.

I would use the lambda filter

- platform: adc
    pin: A0
    name: "Mains Water Pressure"
    update_interval: 10s
    filters:
      - lambda: return ((x * 3.3) - 0.5) * 400
    unit_of_measurement: "kPa"  

Thank you both for the replies, although I write software in assembler & C I dont understand calibrate_linear, will need to read up on it.

But happy with the lambda

Regards, Dave

Hi just tried your code and got this error, did it as a screen dump so you can see the spacing

Silly me, there is supposed to be ; at the end of the lambda line. Might also need .0 on the 400

- lambda: return ((x * 3.3) - 0.5) * 400.0;

Sorry about that, and see Sensor Component — ESPHome

Thanks, now working, also thanks for the link

re Dave

1 Like

Hi @DaveTiff
What’s does multiply 3.3 do?
I have a similar pressure sensor measuring pressure at the bottom of my water tank, It’s all so a 0.5 to 5V signal
looking to convert to 0-100%
Thanks

Post #2 above has yaml. Calibrate_linear

You measure the voltage when empty and then when full and then use it in the filter

To cope with the scaling, see https://esphome.io/components/sensor/adc.html?highlight=esp32+d

This component prints the voltage as seen by the chip pin. On the ESP8266, this is always 0.0V to 1.0V Some development boards like the Wemos D1 mini include external voltage divider circuitry to scale down a 3.3V input signal to the chip-internal 1.0V. If your board has this circuitry, add a multiply filter to get correct values:

Ok Thanks I see now, I’m using ESP32 thats way i was a little confused.