Calibration of a sensor

I am using an ESP01 with an integrated DHT11 sensor, which works through the ESPHome integration.
It appeared that the given temperature value is always more than 2 degrees off, so I created a calibrated copy of the sensor like this in configuration.yaml:

      cal_esp01dht11_temp:
        friendly_name: "ESP01-DHT11 Temperatuur"
        icon_template: hass:thermometer
        unit_of_measurement: "°C"
        value_template: '{{ states.sensor.esp01_dht11_temperature.state | float - 2.3 }}'

This normally gives a reasonably good value, but there is one problem: at every restart of Home Assistant the calibrated sensor apparently gets a value of zero from the original sensor, so shows a value of -2.3 degrees in that case. The original ESPHome sensor output however does not show those zero values.
This are some example graphs of the original and the calibrated sensor:

ESP01-DHT11
ESP01-DHT11_calibrated

You can clearly see the downward spikes at every restart of HA.
I could add a filter to ignore values of zero, but then there is a problem when the real value is exactly zero.
So is there a solution for this?

  1. read and heed the warning here:

https://www.home-assistant.io/docs/configuration/templating/#states

  1. Use an availability_template to reject the unknown states that the |float filter returns as 0:
cal_esp01dht11_temp:
        friendly_name: "ESP01-DHT11 Temperatuur"
        icon_template: hass:thermometer
        unit_of_measurement: "°C"
        value_template: "{{ states('sensor.esp01_dht11_temperature') | float - 2.3 }}"
        availability_template:  "{{ states('sensor.esp01_dht11_temperature') not in ['unknown', 'unavailable'] }}"

Thank you :grinning:
That is looking exactly as to what I need, so I will try it out.

However, in the mean time I probably found another solution as well?
It appears that the calibration can be added directly to the ESPHome node.
So this is what I did now in ESPHome:

sensor:
  - platform: dht
    pin: 2
    temperature:
      name: "ESP01-DHT11 Temperature"
      filters:
        - offset: -2.3
    humidity:
      name: "ESP01-DHT11 Humidity"
      filters:
        - offset: 1.0
    model: DHT11
    update_interval: 60s

I expect that this will work fine as well, because all values that are given by the ESP01 are now correct (so no zero values anymore)?

5 Likes

Indeed, that would be a better solution.

OK, I added both solutions to my configuration, and indeed both work as expected:

ESP01-DHT11_2
ESP01-DHT11_calibrated_2

The original sensor is now showing the values with an -2.3 degrees offset, and the calibrated sensor is not sparking down anymore at several restarts of HA.
Can we conclude that “my” solution is the preferred solution in this case, because it precludes getting any ‘unknown’ or ‘unavailable’ values?

1 Like

Yes, definitely.

The ‘unknown’ issue is only a problem for the template sensor. The |float filter returns 0 for any string that isn’t a number.

1 Like