Trying to set global based on HA switch state

I’m building an indicator that sets a red, yellow, or green light to illuminate depending on the state of a distance sensor. I have that part working well, but what I’d like to do is to only enable the leds when the light to that room is on in Home Assistant so the leds aren’t on all day or night long when no one is there. Here’s my yaml where I’m having trouble getting the switch value from Home assistant, can anyone point me in the right direction to get this working? Thanks

esphome:
  name: dust-collector-level

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "111111111111111="

ota:
  password: "11111111111"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Dust-Collector-Level"
    password: "11111111"

web_server:
  port: 80
  
time:
  - platform: homeassistant
    id: homeassistant_time

captive_portal:
    
i2c:

globals:
  - id: led_enabled
    type: bool
    restore_value: no
    initial_value: "false"
  
switch:
  - platform: restart
    name: "dust-collector-level Restart"
    
light:
  - platform: binary
    name: dust-light-green
    output: dust_light_output_g
    id: light_green
  - platform: binary
    name: dust-light-yellow
    output: dust_light_output_y
    id: light_yellow
  - platform: binary
    name: dust-light-red
    output: dust_light_output_r
    id: light_red

output:
  - id: dust_light_output_g
    platform: gpio
    pin: D5
  - id: dust_light_output_y
    platform: gpio
    pin: D6
  - id: dust_light_output_r
    platform: gpio
    pin: D7

sensor:
  - platform: uptime
    name: dust-collector-level Uptime
  
  - platform: wifi_signal
    name: dust-collector-level WiFi Signal
    update_interval: 60s
    
  - platform: homeassistant
    id: led_switch
    entity_id: switch.garage_light
    internal: false #switch this to true once it's working
    on_value:
      then:
        - globals.set:
            id: led_enabled
            value: !lambda return id(led_switch).state; 
            #I'm getting an error grabbing the value here
    
  - platform: vl53l0x
    name: dust-collector-level-fill
    id: dc_measure_level
    update_interval: 1s
    filters:
      - sliding_window_moving_average:
          send_every: 5
          window_size: 10
    on_value:
      then:
        - if:
            condition:
              lambda: |-
                return id(led_enabled).state == false;
            then:
              - light.turn_off: light_red
              - light.turn_off: light_yellow
              - light.turn_off: light_green
            else:
              - if:
                  condition: 
                    lambda: |-
                      return id(dc_measure_level).state > 0.35;
                  then:
                     - light.turn_off: light_red
                     - light.turn_off: light_yellow
                     - light.turn_on: light_green
                  else:
                    - if:
                        condition:
                          lambda: |-
                            return id(dc_measure_level).state < 0.18;
                        then:
                          - light.turn_off: light_green
                          - light.turn_off: light_yellow
                          - light.turn_on: light_red
                        else:
                          - light.turn_off: light_green
                          - light.turn_off: light_red
                          - light.turn_on: light_yellow

text_sensor:
  - platform: version
    name: dust-collector-level ESPHome Version
  - platform: wifi_info
    ip_address:
      name: dust-collector-level IP
    ssid:
      name: dust-collector-level SSID
    bssid:
      name: dust-collector-level BSSID

So what are you seeing in the logs?

And why do you need a global? The state of the led_switch is known.

Thank you. I’m getting a compilation error on this line, but good point about not needing a global.
value: !lambda return id(led_switch).state;

In my first approach, I was trying to create a ‘virtual’ switch in the sketch and then having Home Assistant update that in the light switch automation.

That worked, Thank you @nickrout, I was making it needlessly complicated. Here’s the final yaml that worked for me.

esphome:
  name: dust-collector-level

esp8266:
  board: d1_mini

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "111111="

ota:
  password: "111111"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Dust-Collector-Level"
    password: "111111"

web_server:
  port: 80
  
time:
  - platform: homeassistant
    id: homeassistant_time

captive_portal:
    
i2c:
  
switch:
  - platform: restart
    name: "dust-collector-level Restart"
    
light:
  - platform: binary
    name: dust-light-green
    output: dust_light_output_g
    id: light_green
  - platform: binary
    name: dust-light-yellow
    output: dust_light_output_y
    id: light_yellow
  - platform: binary
    name: dust-light-red
    output: dust_light_output_r
    id: light_red

output:
  - id: dust_light_output_g
    platform: gpio
    pin: D5
  - id: dust_light_output_y
    platform: gpio
    pin: D6
  - id: dust_light_output_r
    platform: gpio
    pin: D7

binary_sensor:
  - platform: homeassistant
    id: led_switch
    entity_id: switch.garage_light
    internal: true

sensor:
  - platform: uptime
    name: dust-collector-level Uptime
  
  - platform: wifi_signal
    name: dust-collector-level WiFi Signal
    update_interval: 60s

  - platform: vl53l0x
    name: dust-collector-level-fill
    id: dc_measure_level
    update_interval: 1s
    filters:
      - sliding_window_moving_average:
          send_every: 5
          window_size: 10
    on_value:
      then:
        - if:
            condition: 
              lambda: return (id(led_switch).state);
            then:
              - light.turn_off: light_red
              - light.turn_off: light_yellow
              - light.turn_off: light_green
            else:
              - if:
                  condition: 
                    lambda: return id(dc_measure_level).state > 0.35;
                  then:
                    - light.turn_off: light_red
                    - light.turn_off: light_yellow
                    - light.turn_on: light_green
                  else:
                    - if:
                        condition:
                          lambda: return id(dc_measure_level).state < 0.18;
                        then:
                          - light.turn_off: light_green
                          - light.turn_off: light_yellow
                          - light.turn_on: light_red
                        else:
                          - light.turn_off: light_green
                          - light.turn_off: light_red
                          - light.turn_on: light_yellow
                          
text_sensor:
  - platform: version
    name: dust-collector-level ESPHome Version
  - platform: wifi_info
    ip_address:
      name: dust-collector-level IP
    ssid:
      name: dust-collector-level SSID
    bssid:
      name: dust-collector-level BSSID

Glad it worked, I am a believer in KISS and Occam’s Razor. :slight_smile: