Value Template Offset Incorrect when ESPHome Unavailable

I have a number of DHT22 sensors on some NodeMCU’s around my house. I calibrated them and then fixed the offset in Home Assistant using a value_template. It works well however whenever the NodeMCU goes unavailable for some reason the value_template offset gets calculated wrong. Normally, it just adds the offset to the sensor value and everything is great, but when it is unavailable the value_template just reports the offset itself.

Has anyone experienced this, is this how it is supposed to function or am I doing something wrong?

As an alternative you could move the offset to the ESPHome device itself and remove the template sensor. Sounds as if you are looking for the offset filter.

1 Like

Yeah, that is what I ended up doing but I just thought it was weird behaviour with Home Assistant, doesn’t seem like it should function like that

I can only assume that your value template was written in a way that the temperature sensor’s value is interpreted as zero when the device becomes unavailable, so effectively the template’s value becomes 0 + offset = offset.
To circumvent this behaviour you could change your value template and surround the offset calculation with a check if the sensor is currently available; if not, output something else.

However, I think calculating the offset on the device is the cleanest solution.

I’m running into a similar situation in that the output from the DHT sensor isn’t accurate. I think the ultimate fix is is to go with a more accurate sensor but lacking that I like the idea of using the filter in ESPHome.

One question on that tho.

I use substitutions to create an individualized frmware file for each device based on a common file for the device type.

Do you know if ESPHome can accept substitutions of the filter values?

I like the “calibrate_linear” filter for a better calibration of the output and I’m pretty sure I wouldn’t end up with the filter values being identical for each device. I’m not really sure how I could logically enter those values into a substitution.

@finity how do you calibrate your DHT sensors? I don’t know how accurate mine are because I don’t really know how to get a golden source and make a good test setup.

What I ended up doing is getting all of the sensors I have (6) and put them right next to each other and then take the average value and calibrate each sensor to that. I don’t know if this is the “right” value or not, but it is the best I can think of.

I don’t really “calibrate” them per se.

I just use a good digital thermometer next to the sensor and compare the two.

@exxamalte You are probably right. I think I just copied an example of this I found on the forums. What would you suggest? If the value was made null or something if the sensor is unavailable, would that fix the problem?

I’m going to keep the offset on the device like you say, but I just want to try to understand this a bit better if possible.

One of my offsets in HA is below:

living_room_temperature_offset:
value_template: ‘{{ ( states.sensor.living_room_temperature_2.state | float + 0.1 ) | round(1) }}’
friendly_name: ‘Living Room Temperature Offset’
unit_of_measurement: “°F”

You could paste this into the Templates editor. If the sensor state is unavailable, the result will be 0.0:

{{ states.sensor.living_room_temperature_2.state | float }}

If you really have to calculate the offset inside HA, you could try the following, but I don’t see a lot of benefit in this approach:

{% if not is_state("sensor.living_room_temperature_2", "unavailable") %}
  {{ ( states.sensor.living_room_temperature_2.state | float + 0.1 ) | round(1) }}
{%- else -%}
  unavailable
{%- endif %}

As I said, I’d recommend to calculate the offset on the ESP.

BTW: Last year I dumped all my DHT22 sensors, mainly because humidity was unreliable, and replaced them all with BME280 sensors. These are slightly more expensive, but much more reliable and give you temperature, humidity and air pressure.

Sorry,
I’m having the same problem.
My temperature sensor is reporting 2 degrees higher then it is supposed to.

Where/How should I change my code to adjust it´s output ?

esphome:
  name: multisensor_casal
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "My_SSID"
  password: "My_SSID_PSW"

logger:

# Enable Home Assistant API
api:

ota:

sensor:
  - platform: dht
    pin: D7
    model: dht22    
    temperature:
      name: "RoomTemperature"
    humidity:
      name: "Room Humidity"
    update_interval: 30s
    
binary_sensor:
  - platform: gpio
    pin: D6
    name: "RoomPIR"
    device_class: motion

See below for how I ultimately corrected mine in the ESPHome config. The correction occurs in the “lambda” filter which changes the measurement from Celsius to Fahrenheit and then adds 0.1 degree offset.

sensor:
  - platform: dht
    pin: D2
    model: AM2302
    temperature:
      name: "Living Room Temperature"
      filters:
      - lambda: return x * (9.0/5.0) + 32.0 + 0.1;
      unit_of_measurement: "°F"
    humidity:
      name: "Living Room Humidity"
    update_interval: 60s
3 Likes

PERFECT!!!
Thanks :slight_smile:

You can also use calibrate_linear. See here: https://esphome.io/components/sensor/index.html

1 Like

Probably the “offset” filter is the correct one to use. I didn’t know if the offset would be added before or after the lambda calculation, is the only reason I didn’t use it.

Can i calibration my AM2302 which is connected with sonoff basic.

Anyone now how to pu a minimun value f the sensor? I have a current sensor and when is 0 it appears as -0.01A and I would like to put 0.00A as min value (min_value: 0.00 doesn´t seem to work). Thank you

Maybe someaone could help aswell since it is the callibration topic. So I have the PH sensor which is calibrated initially with the calibrate_linear in ESPHOME. How can I calibrate the sensor afterwards using some HA commands to change the calibrate linear values and update the ESPHOME node?