Haveing trouble changing addressable led color with state change in a lambda

This is my Lambda I have so far and need some help with.

    on_value:
      then:
        lambda: |-
            auto call = id(LED0).turn_on();
            if ((id(ads1115_A0).state >= 1.771) && (id(ads1115_A0).state < 4.00)) {
              call.set_brightness(.6);
              call.set_rgb(1.00,0.00,0.00);
              } else if ((id(ads1115_A0).state >= 1.311) && (id(ads1115_A0).state < 1.771)) {
                    call.set_brightness(.6);
                    call.set_rgb(0.5,0.5,0.0);
                 } else   
                     ((id(ads1115_A0).state >= 0.400) && (id(ads1115_A0).state < 1.310)); {
                       call.set_brightness(.6);
                       call.set_rgb(0.0,1.0,0.0);
                }
              call.perform(); 

It’s only turns the led to one color. Green. No matter the value of the sensor. I was hoping for traffic light colors depending on the sensor state value.
Log snips:

[23:55:22][D][ads1115:163]: 'ADS1115 49 Channel A3-GND': Got Voltage=3.148313V
[23:55:24][D][api.connection:630]: Client 'Home Assistant 2021.6.6 (192.168.0.89)' connected successfully!
[23:55:27][D][ads1115:163]: 'ADS1115 48 Channel A1-GND': Got Voltage=1.732687V
[23:55:28][D][ads1115:163]: 'ADS1115 49 Channel A1-GND': Got Voltage=0.609563V
[23:55:30][D][ads1115:163]: 'ADS1115 48 Channel A0-GND': Got Voltage=2.968125V
[23:55:30][D][light:275]: '48-A0 indicator led' Setting:
[23:55:30][D][light:288]:   Brightness: 60%
[23:55:30][D][light:297]:   Red=0%, Green=100%, Blue=0%
[23:55:30][D][light:314]:   Transition Length: 1.0s
[23:55:30][D][ads1115:163]: 'ADS1115 49 Channel A0-GND': Got Voltage=1.515375V
[23:55:30][D][ads1115:163]: 'ADS1115 48 Channel A2-GND': Got Voltage=0.639375V
[23:55:33][D][ads1115:163]: 'ADS1115 48 Channel A3-GND': Got Voltage=0.616687V

[23:50:05][D][ads1115:163]: 'ADS1115 49 Channel A0-GND': Got Voltage=1.483125V
[23:50:06][D][ads1115:163]: 'ADS1115 48 Channel A0-GND': Got Voltage=1.479938V
[23:50:06][D][light:275]: '48-A0 indicator led' Setting:
[23:50:06][D][light:288]:   Brightness: 60%
[23:50:06][D][light:297]:   Red=0%, Green=100%, Blue=0%
[23:50:06][D][light:314]:   Transition Length: 1.0s
[23:50:08][D][sensor:117]: '49-A0 Soil Saturation': Sending state 62.32372 % with 0 decimals of accuracy
[23:50:08][D][sensor:117]: '48-A1 Soil Saturation': Sending state 8.21313 % with 0 decimals of accuracy
[23:50:19][D][sensor:117]: '48-A2 Soil Saturation': Sending state 100.00000 % with 0 decimals of accuracy

This is for a soil sensor project I’m working on that will have 16 soil sensors with one WS2812 led for each sensor

Full working code as of now. Just not fully fleshed out:

esphome:
  name: soil-water-level
  platform: ESP8266
  board: d1_mini_lite


# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "ggg"

wifi:
  ssid: "ggg"
  password: "ggg"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Soil-Water-Level"
    password: "ggg"

captive_portal:


i2c:
  id: bus_a
  sda: D2
  scl: D1
  scan: True

ads1115:
#  i2c_id: bus_a
- address: 0x48
  id: ads1115_48
- address: 0x49
  id: ads1115_49
#for esp32 only
#ads1115:
#  i2c_id: bus_b
#  address: 0x49
#  id: ads1115_49
  
sensor:
# ads1115-48 A0
  - platform: ads1115
    multiplexer: 'A0_GND'
    gain: 6.144
    name: "ADS1115 48 Channel A0-GND"
    id: ads1115_A0
    ads1115_id: ads1115_48
    update_interval: 30s
    internal: true 
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    #on_raw_value:
    on_value:
      then:
        lambda: |-
            auto call = id(LED0).turn_on();
            if ((id(ads1115_A0).state >= 1.771) && (id(ads1115_A0).state < 4.00)) {
              call.set_brightness(.6);
              call.set_rgb(1.00,0.00,0.00);
              } else if ((id(ads1115_A0).state >= 1.311) && (id(ads1115_A0).state < 1.771)) {
                    call.set_brightness(.6);
                    call.set_rgb(0.5,0.5,0.0);
                 } else   
                     ((id(ads1115_A0).state >= 0.400) && (id(ads1115_A0).state < 1.310)); {
                       call.set_brightness(.6);
                       call.set_rgb(0.0,1.0,0.0);
                }
              call.perform(); 
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) {
            return 0;
            } else if (x < 1.310) {
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 
                }
     
                
  - platform: template
    name: "48-A0 Soil Saturation"
    id: ads1115_48A0_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_A0).state;                  
# ads1115-48 A1
  - platform: ads1115
    multiplexer: 'A1_GND'
    gain: 6.144
    name: "ADS1115 48 Channel A1-GND"
    id: ads1115_A1
    ads1115_id: ads1115_48
    update_interval: 30s
    internal: true # Don't expose sensor to hass, the template sensors does that
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) { 				// if over 1.771 volts,  dry enough to water
            return 0;
            } else if (x < 1.310) { 			// if under 1.310 volts, we are sokeing wet
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 	// linear for any values in between
                }
                
  - platform: template
    name: "48-A1 Soil Saturation"
    id: ads1115_48A1_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_A1).state; 
# ads1115-48 A2
  - platform: ads1115
    multiplexer: 'A2_GND'
    gain: 6.144
    name: "ADS1115 48 Channel A2-GND"
    id: ads1115_A2
    ads1115_id: ads1115_48
    update_interval: 30s
    internal: true # Don't expose sensor to hass, the template sensors does that
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) { 				// if over 1.771 volts,  dry enough to water
            return 0;
            } else if (x < 1.310) { 			// if under 1.310 volts, we are sokeing wet
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 	// linear for any values in between
                }
                
  - platform: template
    name: "48-A2 Soil Saturation"
    id: ads1115_48A2_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_A2).state;       
# ads1115-48 A3
  - platform: ads1115
    multiplexer: 'A3_GND'
    gain: 6.144
    name: "ADS1115 48 Channel A3-GND"
    id: ads1115_A3
    ads1115_id: ads1115_48
    update_interval: 30s
    internal: true # Don't expose sensor to hass, the template sensors does that
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) { 				// if over 1.771 volts,  dry enough to water
            return 0;
            } else if (x < 1.310) { 			// if under 1.310 volts, we are sokeing wet
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 	// linear for any values in between
                }
                
  - platform: template
    name: "48-A3 Soil Saturation"
    id: ads1115_48A3_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_A3).state;     
# ads1115-49 A0
  - platform: ads1115
    multiplexer: 'A0_GND'
    gain: 6.144
    name: "ADS1115 49 Channel A0-GND"
    id: ads1115_B0
    ads1115_id: ads1115_49
    update_interval: 30s
    internal: true # Don't expose sensor to hass, the template sensors does that
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) { 				// if over 1.771 volts,  dry enough to water
            return 0;
            } else if (x < 1.310) { 			// if under 1.310 volts, we are sokeing wet
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 	// linear for any values in between
                }
                
  - platform: template
    name: "49-A0 Soil Saturation"
    id: ads1115_49A0_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_B0).state;                        
# ads1115-49 A1
  - platform: ads1115
    multiplexer: 'A1_GND'
    gain: 6.144
    name: "ADS1115 49 Channel A1-GND"
    id: ads1115_B1
    ads1115_id: ads1115_49
    update_interval: 30s
    internal: true # Don't expose sensor to hass, the template sensors does that
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) { 				// if over 1.771 volts,  dry enough to water
            return 0;
            } else if (x < 1.310) { 			// if under 1.310 volts, we are sokeing wet
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 	// linear for any values in between
                }
                
  - platform: template
    name: "49-A1 Soil Saturation"
    id: ads1115_49A1_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_B1).state;      
 # ads1115-49 A2
  - platform: ads1115
    multiplexer: 'A2_GND'
    gain: 6.144
    name: "ADS1115 49 Channel A2-GND"
    id: ads1115_B2
    ads1115_id: ads1115_49
    update_interval: 30s
    internal: true # Don't expose sensor to hass, the template sensors does that
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) { 				// if over 1.771 volts,  dry enough to water
            return 0;
            } else if (x < 1.310) { 			// if under 1.310 volts, we are sokeing wet
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 	// linear for any values in between
                }
                
  - platform: template
    name: "49-A2 Soil Saturation"
    id: ads1115_49A2_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_B2).state;     
# ads1115-49 A3
  - platform: ads1115
    multiplexer: 'A3_GND'
    gain: 6.144
    name: "ADS1115 49 Channel A3-GND"
    id: ads1115_B3
    ads1115_id: ads1115_49
    update_interval: 30s
    internal: true # Don't expose sensor to hass, the template sensors does that
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) { 				// if over 1.771 volts,  dry enough to water
            return 0;
            } else if (x < 1.310) { 			// if under 1.310 volts, we are sokeing wet
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 	// linear for any values in between
                }
                
  - platform: template
    name: "49-A3 Soil Saturation"
    id: ads1115_49A3_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_B3).state;    
      
light:
  - platform: neopixelbus
    type: GRB
    pin: GPIO3
    num_leds: 9
    name: "indicator-lights"
    internal: true
    id: indicatorlt
          
  - platform: partition
    name: "48-A0 indicator led"
    id: LED0
    internal: true
    segments:
      - id: indicatorlt
        from: 0
        to: 0
  - platform: partition
    name: "48-A1 indicator led"
    id: LED1
    internal: true
    segments:
      - id: indicatorlt
        from: 1
        to: 1
  - platform: partition
    name: "48-A2 indicator led"
    id: LED2
    internal: true
    segments:
      - id: indicatorlt
        from: 2
        to: 2
  - platform: partition
    name: "48-A3 indicator led"
    id: LED3
    internal: true
    segments:
      - id: indicatorlt
        from: 3
        to: 3
  - platform: partition
    name: "49-A0 indicator led"
    id: LED4
    internal: true
    segments:
      - id: indicatorlt
        from: 4
        to: 4
  - platform: partition
    name: "49-A1 indicator led"
    id: LED5
    internal: true
    segments:
      - id: indicatorlt
        from: 5
        to: 5
  - platform: partition
    name: "49-A2 indicator led"
    id: LED6
    internal: true
    segments:
      - id: indicatorlt
        from: 6
        to: 6
  - platform: partition
    name: "49-A3 indicator led"
    id: LED7
    internal: true
    segments:
      - id: indicatorlt
        from: 7
        to: 7
  - platform: partition
    name: "LED8"
    id: LED8
    #internal: true  , Left avbl to HA to test leds
    segments:
      - id: indicatorlt
        from: 8
        to: 8

I tried reworking that piece of code and came up with something that in my mind should work, but does not :frowning:

    on_value: 
          then:
            - if:
                condition:
                   lambda: |-
                         return ((id(ads1115_A0).state >= 1.771) && (id(ads1115_A0).state < 4.00)) == true;
                then:
                  - light.turn_on:
                     id: LED0
                     brightness: 50%
                     red: 100% 
                     green: 0%
                     blue: 0%  
            - if:
                condition:
                   lambda: |-
                          return ((id(ads1115_A0).state >= 1.311) && (id(ads1115_A0).state < 1.770)) == true;                     
                then:
                  - light.turn_on:
                       id: LED0
                       brightness: 50%
                       red: 25%   #orange I think
                       green: 25%
                       blue: 0%            
            - if:
                condition:
                   lambda: |-
                          return ((id(ads1115_A0).state >= 0.400) & (id(ads1115_A0).state < 1.310)) == true;
                then:
                  - light.turn_on:
                       id: LED0
                       brightness: 50%
                       red: 0% 
                       green: 100%
                       blue: 0%  

This is not effecting the LEDs at all .
At least the first one changes the LED to a color. be it wrong however :slight_smile:

Any tips please. I’m at my knowledge limit.

A variant of the first lambda, it’s doing the same as the first. Only green.

    on_raw_value:
    #on_value:
      then:
        lambda: |-
            auto call = id(LED0).turn_on();
            if ((x >= 1.771) && (x < 4.00)) {
              call.set_brightness(.6);
              call.set_rgb(1.00,0.00,0.00);
              } 
              else if ((x >= 1.311) && (x < 1.771)) {
                    call.set_brightness(.6);
                    call.set_rgb(0.5,0.5,0.0);
                 } 
                 else if   
                     ((x >= 0.400) && (x < 1.310)); {
                       call.set_brightness(.6);
                       call.set_rgb(0.0,1.0,0.0);
                }
              call.perform(); 

One more same result :confused:

    on_raw_value:
    #on_value:
      then:
      - lambda: |-
            
            if ((x >= 1.771) && (x < 4.00)) {
              auto call = id(LED0).turn_on();
              call.set_brightness(.6);
              call.set_rgb(1.00,0.00,0.00);
              call.perform();
              } 
              else if ((x >= 1.311) && (x < 1.771)) {
                    auto call = id(LED0).turn_on();
                    call.set_brightness(.6);
                    call.set_rgb(0.5,0.5,0.0);
                    call.perform();
                 } 
                 else if   
                     ((x >= 0.400) && (x < 1.310)); {
                       auto call = id(LED0).turn_on();
                       call.set_brightness(.6);
                       call.set_rgb(0.0,1.0,0.0);
                       call.perform();
                }
 

Just jotting down things i have tried.

Ok figured it out!!
This is the working code block:

sensor:
# ads1115-48 A0
  - platform: ads1115
    multiplexer: 'A0_GND'
    gain: 6.144
    name: "ADS1115 48 Channel A0-GND"
    id: ads1115_A0
    ads1115_id: ads1115_48
    update_interval: 30s
    internal: true 
    #  1.771 soil from holding bin placed in a cup (Dry)
    #  1.310 soil in a cup with lotof water (Wet)

    
    filters:
      - sliding_window_moving_average: # averages the last 5 results,
          window_size: 5
          send_every: 5
      - lambda: |-
            if (x > 1.771) {
            return 0;
            } else if (x < 1.310) {
                return 100;
                } else {
                  return (1.771-x) / (1.771-1.310) * 100.0; 
                }
    on_raw_value:
    #on_value: 
          then:
            - if:
                condition:
                   lambda: |-
                         return (x >= 1.771 && x < 4.00);
                then:
                  - light.turn_on:
                     id: LED0
                     brightness: 50%
                     red: 100% 
                     green: 0%
                     blue: 0%  
            - if:
                condition:
                   lambda: |-
                          return (x >= 1.311 && x < 1.770);                     
                then:
                  - light.turn_on:
                       id: LED0
                       brightness: 50%
                       red: 25%   #orange I think
                       green: 25%
                       blue: 0%            
            - if:
                condition:
                   lambda: |-
                          return (x >= 0.400 && x < 1.310);
                then:
                  - light.turn_on:
                       id: LED0
                       brightness: 50%
                       red: 0% 
                       green: 100%
                       blue: 0%  

      
                
  - platform: template
    name: "48-A0 Soil Saturation"
    id: ads1115_48A0_saturation
    unit_of_measurement: "%"
    icon: "mdi:water-percent"
    accuracy_decimals: 0
    lambda: |-
      return id(ads1115_A0).state;