Take a look at batt_voltage_filtered and the sensor it copies. You should be able to use filters for the various processing steps, especially calibrate linear, multiply and lambda. You should be able to do it all in yaml (maybe with a few lambda’s) rather than a custom component.
If it’s a strictly linear mapping and you don’t expect any weirdness (aberrant values that need filtering out) then I think the filter stuff is overkill and prone to skewing results (it’s still not clear to me how much state even the linear conversion holds/why it needs to). You can use esphome::remap in a lambda (an example from a different kind of sensor) as an alternative that isn’t that popular
e.g. something like esphome::remap(x, 0.0f, 3.3f, 0.0f, 30.0f) but the code above it in my example that rejects bad values is important too. Actually looking at your code you’re converting from the 12bit adc (0-4095) to voltages and then to a real-world value. So skip the voltages and use esphome::remap(x, 0.0f, 4095.0f, 0.0f, 30.0f)?
digging, i found that the best will be use a ina3221, which will give me the reading from 0 to 5 without the need of voltage dividers and so on, simplifying the circuit., and so i can read the wind direction too.
In this case, can i esphome::remap(x, 0.0, 4095, 0.0, 30.0)?
and how to debug it, like printing the results on screen, like arduino ide does :
Nice work, just make sure you type 4095.0 (to make it a float) or you’ll get an obscure error message as all the values have to be the same type (float in this case). This is a low-level function without niceties that filters have.
# Logging level
logger:
level: VERBOSE
in the yaml should help with the debugging, if you connect to the logs remotely via HA/esphome plugin or locally on the usb port.
The digital inputs are quite tolerant, but the built-in adc units definitely aren’t; even that linked article says so (and the spec sheet for the esp’s is quite emphatic).
lambda: |-
ESP_LOGI("","Lambda incoming value=%f - data array size is %d",x,data.size());
if (isnan(x)) { // add other values of x that are not valid you want to exclude
return NAN;
}
return esphome::remap(x, 0.0f, 4095f, 0.0f, 30.0f);