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?
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.
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.
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.
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
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 ). Im not yet sufficiently knowledgable to write code myself. This will greatly help me learning how HA works
You can use lamda to achieve this. Below is a code example where I changed the temperature being returned to a sensor dependent on whether a screen was turned on that was affecting the measured temperature: