Ultrasonic sensor with LED and esp32

Hi everyone. I’m new to esphome. I would like to create a tank level sensor with ultrasonic sensor. I have already configured the sensor for the liquid level in percentage by setting minimum and maximum values. now I would like for each decade to turn on an LED based on the level of the liquid (10% - 20% …) but I would also like it to turn off if the level decreases. Unfortunately I didn’t succeed. advice?

light:
 - platform: binary
   name: LED10
   output: led10
   id: led10p
 - platform: binary
   name: LED20
   output: led20
   id: led20p
 - platform: binary
   name: LED30
   output: led30
   id: led30p
 - platform: binary
   name: LED40
   output: led40
   id: led40p
 - platform: binary
   name: LED50
   output: led50
   id: led50p
 - platform: binary
   name: LED60
   output: led60
   id: led60p
 - platform: binary
   name: LED70
   output: led70
   id: led70p
 - platform: binary
   name: LED80
   output: led80
   id: led80p
 - platform: binary
   name: LED90
   output: led90
   id: led90p
 - platform: binary
   name: LED100
   output: led100
   id: led100p

output:
 - platform: gpio
   pin: GPIO12
   id: led10
 - platform: gpio
   pin: GPIO14
   id: led20  
 - platform: gpio
   pin: GPIO27
   id: led30
 - platform: gpio
   pin: GPIO26
   id: led40
 - platform: gpio
   pin: GPIO25
   id: led50
 - platform: gpio
   pin: GPIO33
   id: led60
 - platform: gpio
   pin: GPIO32
   id: led70
 - platform: gpio
   pin: GPIO2
   id: led80
 - platform: gpio
   pin: GPIO0
   id: led90
 - platform: gpio
   pin: GPIO4
   id: led100
#Configurazione sensore ultrasuoni
 - platform: ultrasonic
   trigger_pin: GPIO23
   echo_pin: GPIO22
   name: sensore_ultrasuoni_percentuale_acqua
   icon: 'mdi:water-percent'
   unit_of_measurement: '%'
   update_interval: 2s
   pulse_time: 50us
   filters:
    - filter_out: nan
    - median:
       window_size: 7
       send_every: 4
       send_first_at: 3
    - calibrate_linear:
       - 0.03 -> 0
       - 0.20 -> 100.0
    - lambda: return 100 - x ;
   on_value_range:
    - above: 0
      below: 11
      then:
        - light.turn_on:
           id: led10p
    - above: 12
      below: 21
      then:
        - light.turn_on:
           id: led20p
    - above: 22
      below: 31
      then:
        - light.turn_on:
           id: led30p
    - above: 32
      below: 41
      then:
        - light.turn_on:
           id: led40p
    - above: 42
      below: 51
      then:
        - light.turn_on:
           id: led50p
    - above: 52
      below: 61
      then:
        - light.turn_on:
           id: led60p
    - above: 62
      below: 71
      then:
        - light.turn_on:
           id: led70p
    - above: 72
      below: 81
      then:
        - light.turn_on:
           id: led80p
    - above: 82
      below: 91
      then:
        - light.turn_on:
           id: led90p
    - above: 92
      below: 100
      then:
        - light.turn_on:
           id: led100p

Hi Giovanni and welcome to HA. Can you edit your post as per the link below, to enclose your yaml in formatted text tags? On the posting toolbar they look like </>. Because indentation is so important in yaml you need to post it so the formatting is readable.

Assuming that the intention is to only turn the lights on and off in successive order, a relatively simple solution would be to add an action to every on_value_range action to turn off the adjacent lights first and then turn on the intended light.
Something like this:

    on_value_range:
      - above: 0
        below: 11
        then:
          - light.turn_off:
              id: led20p
          - light.turn_on:
              id: led10p
      - above: 12
        below: 21
        then:
          - light.turn_off:
              id: led10p
          - light.turn_off:
              id: led30p
          - light.turn_on:
              id: led20p
      - above: 22
        below: 31
        then:
          - light.turn_off:
              id: led20p
          - light.turn_off:
              id: led40p
          - light.turn_on:
              id: led30p

Etcetera.

Please note that I corrected some indentations in your code: each indentation in Yaml should consist of 2 spaces.