UI Frontend Calibration - calibrate_linear filtering using variables

I wanted to create a way to calibrate a temperature sensor using a number sensor that can be changed in Home Assistant frontend, much like this sensor (as an example of a number sensor):

Screenshot 2023-08-22 235731

The intent is to enter the measured temp of the sensor into the Frontend input number template sensor to change the filters: calibration_linear: datapoints: of a Dallas temp sensor.

  # Temperature Probes 1
  - platform: dallas
    id: temp_1
    icon: mdi:thermometer-lines
    index: 0
    name: "Temperature"
    filters:
      - calibrate_linear:
          method: least_squares
          datapoints: 
            - 0.0 -> ${cal_0}
            - 100.0 -> ${cal_100}
            # - 0.0 -> 0.0
            # - 100.0 -> 100.0

Using number sensors like these:

  # Calibration Temp. at 0C - Ice Bath
  - platform: template
    name: "Calibration Temp. at 0C - Ice Bath"
    id: cal_0
    icon: mdi:snowflake-alert
    optimistic: true
    min_value: -10.0
    max_value: 10.0
    step: .1
    restore_value: True
    initial_value: 0.0
    entity_category: "Config"


  # Calibration Temp. at 100C - Boiling
  - platform: template
    name: "Calibration Temp. at 100C - Boiling"
    id: cal_100
    icon: mdi:snowflake-alert
    optimistic: true
    min_value: 90.0
    max_value: 110.0
    step: .1
    restore_value: True
    initial_value: 100.0
    entity_category: "Config"

but the only thing I can substitute into datapoints are substitutions: like this

substitutions:
  cal_0: "0.0"
  cal_100: "100.0"

But I want to be able to change them, and I don’t think substitutions can be variables, or is there a way? I was thinking of globals: and setting the value based on the number sensor but I am not familiar with that and do not know if that’s even an option, I get an error expected float, as expected if I can’t pass a float into datapoints…

globals:
  - id: cal_0
    type: float
    restore_value: yes
    initial_value: '0.0'

I want a UI frontend method of calibration. Is this even possible with ESPHome and Home Assistant? Is there someway to pass variables into datapoints of a filter? Or perhaps a way to alter a substitution from the frontend? Please and thank you.

This post was asking the same question:
Calibrate Linear Filter using a Global Variable?

I suspect to use a variable you might have to DIY your own calibrate linear filter in your own lambda filter?

Is this more a feature you want for the initial calibration or do you see yourself needing the flexibility on an ongoing basis?

I found I got decent calibration using just a fixed offset and a multiplier.

Using Grafana I could plot a lot of fine grained data of the sensor against another one with known accuracy and tinker with the values in real-time until my monkey-eyes told me “that looks pretty good”.

Mostly just initial, but would just like a way to do it from the Frontend as needed. I want to make a nice, user friendly product for some clients, and a way for them to calibrate it easily would be very helpful.

1 Like