Filter Offset based on slider or value input

I’m trying to figure out how I can set the Filter Offset value with a slider or value input using a card. This set value should also survive a device or HA reset, so I guess I need some static or stored variable.
The sensor in question is a SCD30 and the CO2 calibration can be set in the Config in Device info.
I now would like to be able to adjust the offset for Humidity and Temperature as well.
This does work with fixed values, but it would be way more convenient if I just can change the offset using a slider, so I dont have to recompile and upload the code upon adjustments.

I tried dupicating the CO2 calibration and adjusted it for RH and Temp, but found out that this isnt going to work.
This is probably something easy for an experienced HA user, but me being a starter…
Anybody?

sensor:
  - platform: scd30
    address: 0x61
    update_interval: 5s
    automatic_self_calibration: False
    co2:
      name: "Sensor 3 CO2"
      accuracy_decimals: 0
    temperature:
      name: "Sensor 3 Temperature"
      accuracy_decimals: 1
      filters:
        - offset: -0.9
    humidity:
      name: "Sensor 3 Humidity"
      accuracy_decimals: 1
      filters:
        - offset: -0.3
  # temperature_offset: 2.5 °C

button:
  - platform: restart
    name: "Restart"
  - platform: template
    name: "SCD30 Force manual calibration"
    entity_category: "config"
    on_press:
      then:
        - scd30.force_recalibration_with_reference:
            value: !lambda 'return id(co2_cal).state;'

number:
  - platform: template
    name: "CO2 calibration value"
    optimistic: true
    min_value: 0
    max_value: 4500
    step: 1
    id: co2_cal
    icon: "mdi:molecule-co2"
    entity_category: "config"
  - platform: template
    name: "RH correction"
    optimistic: true
    min_value: -10
    max_value: 10
    step: 0.1
    id: lv_cal
    icon: "mdi:water-percent"
    entity_category: "config"
  - platform: template
    name: "Temp correction"
    optimistic: true
    min_value: -10
    max_value: 10
    step: 0.1
    id: lv_temp
    icon: "mdi:thermometer"
    entity_category: "config"

I’ve asked ChatGPT how to achieve this and I got the following example:

Define the Input Number Entity (Slider): Make sure you have defined the input_number.example_slider entity in your Home Assistant configuration. You've likely already done this:

yaml

input_number:
  example_slider:
    name: Example Slider
    initial: 0
    min: -10
    max: 10
    step: 0.1

Adjust the min, max, and step values as needed for your use case.

Configure the ESPHome Sensor with Offset: In your ESPHome configuration, add the filter platform to your sensor configuration. This will apply the offset from the slider to the sensor's value:

yaml

sensor:
  - platform: dht
    pin: D2
    temperature:
      name: "Temperature"
    humidity:
      name: "Humidity"
    update_interval: 60s
    filters:
      - offset: "{{ states('input_number.example_slider') | float }}"

In this configuration:

    The offset filter is used to adjust the sensor's reading by the offset value from the slider.
    The float filter converts the slider value to a floating-point number for accurate calculations.

If I try the 2nd yaml in esphome and compile, I get this error:

Failed config

sensor.sht3xd: [source /config/esphome/sht31-test.yaml:48]
  platform: sht3xd
  address: 68
  update_interval: 5s
  temperature: 
    name: Living Room Temperature
  humidity: 
    name: Living Room Humidity
    filters: 
      - 
        expected float.
        offset: {{ states('input_number.example_slider') }} |float

I dont quite understand the error, because the offset is piped through float. Then how come I get this error?

ChatGPT continues to uphold its reputation for providing incorrect suggestions.

I don’t see any evidence, in ESPHome’s documentation for offset, that it supports a template. It only supports a constant and that’s why you received an error message.

ChatGPT continues to uphold its reputation for providing incorrect suggestions.

Ok, didnt expect it to be completely true. It helped me with the input_number. The slider does work and if I paste the sensor config in Developer Tools it actually returns the correct value

It suggested you do something that isn’t supported. I wouldn’t call that helpful.

The fact that the suggested template works in the Template Editor is unremarkable. The Template Editor’s sole purpose is for evaluating templates. It doesn’t evaluate YAML configurations.

Yeah, I get it, this isnt going to work.
Is there another way of doing this?

Given that offset doesn’t support a template, it means it can only accept a constant value, not a dynamic value. I don’t see how you can work around that fundamental limitation.

I was afraid of that.
Thanks anyway

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.

If you are not actually using the sensor values on the device, then move the calculations over to HA instead.

Thanks for the suggestion Wally. Not sure how to do that though.
Do I need to make a custom sensor? I do need the sensor data to be available and consistent for whatever automation I’m going to come up with in the future :wink:

You get the device data in raw format to HA and then you make a template sensor there with the adjustments you want.
The template sensor can then be used in your automations.
HA can then also store the value over restarts and restarts of the device will not affect this either.

Can you help me with example code how to do this? I usually copy/paste snippets of example code to make things work (kinda :wink: ). Im not yet sufficiently knowledgable to write code myself. This will greatly help me learning how HA works :slight_smile:

Home assistant have a pretty good page for templates with lots of examples.