Filter ADC value

Hi all,
I would like to filter an adc value in this way:
If the value of the ADC is above 2 volt and below 5 volt then give me back the value
else return zero
so I tried to find a solution in different ways but non of my ideas was right.
The last try was this


#################################################
#           ADC ACQUISITION MANAGEMENT          #                 
#################################################
#Acquisition of battery voltage - needed to check the residual charge
  - platform: adc
    pin: $battery_acquisition
    name: "Voltage supply battery"
    id: V_bat
    attenuation: 11db
    update_interval: 1s
    filters:
      - calibrate_linear:
          - 0.0 -> 0.0
          - 2.49 -> 2.74
          - 2.99 -> 2.68
          - 3.04 -> 2.71
          - 3.08 -> 2.74
          - 3.85 -> 3.14
      - median:
          window_size: 3
          send_every: 4
          send_first_at: 3
    icon: "mdi:battery-medium"
  - platform: template
    name: "Battery voltage value"
    id: battery_voltage_value
    icon: "mdi:battery-medium"
    on_value_range:
      above: 2
      below: 5
      then:
      - lambda: |
          battery_voltage_value=v_bat;

When I compile I get the following error:

Compiling .pioenvs/irrigatore/src/main.cpp.o
/config/irrigatore.yaml: In lambda function:
/config/irrigatore.yaml:385:10: error: cannot convert 'float' to 'esphome::globals::GlobalsComponent<float>*' in assignment
               red= id(v_bat).state;
          ^
*** [.pioenvs/irrigatore/src/main.cpp.o] Error 1

But it does not work.
Could you help me?

“It does not work” is not helpful.

Your data does not look linear.

Also I think id should be lowercase, so v_bat not V_bat.

Yes, You totally right.
I added some information in the post and changed the upper case of V_bat in a lower case, but i get the same error when I compile:
When I compile I get the following error:

Compiling .pioenvs/irrigatore/src/main.cpp.o
/config/irrigatore.yaml: In lambda function:
/config/irrigatore.yaml:385:10: error: cannot convert 'float' to 'esphome::globals::GlobalsComponent<float>*' in assignment
               red= id(v_bat).state;
          ^
*** [.pioenvs/irrigatore/src/main.cpp.o] Error 1

Where red is a global variable defined as a float.
I do not need to pass the value to the variable red I just need to have the following behaviour:
When the mosfet is closed the conversion from ADC should start and pass the value to HA, when is open stop to pass the value or give me a value filterable by HA

Is a lambda filter your after.
Here is an example of what I use to filter my soil moisture sensor. So if the value is over 0.8 then return 0 and below 0.36 it’s 100%. Otherwise I calculate the percentage from the raw voltage.

- exponential_moving_average:
          alpha: 0.1
          send_every: 10
      - lambda: |-
          if (x > 0.8) {
            return 0;
          } else if (x < 0.36) {
            return 100;
          } else {
            return (0.8-x) / (0.8-0.36) * 100.0;
          }

So in your case.

- exponential_moving_average:
          alpha: 0.1
          send_every: 10
      - lambda: |-
          if (x > 5) {
            return 0;
          } else if (x < 2) {
            return 0;
          } else {
            return x;
          }

Is this what your looking for.
/Mattias

2 Likes

Thanks it works! :slight_smile: