ESPHome ESP32 WiFi connected LED

I need to have a ‘WiFi connected’ LED for my ESP32.

I can figure out how to use the wifi.component in a condition but how do I use to turn on an LED when connected and off when disconnected.

This is probably really simple but I’m stuck.

Any ideas?

1 Like

I’m going to guess you’re using ESPHome … check this: https://esphome.io/components/wifi.html?highlight=wifi%20connected#wifi-connected-condition

Thanks. I’ve amended the title accordingly.

I’d already seen the wifi.connected element. I just can’t figure how to use that to drive an output.

It needs a trigger, like on_press, for example.

I need something like:

on-wifi.connected
then:
switch.turn_on: wifi_led

Simple as that, but how?

1 Like

This is how I do it:

interval:
  - interval: 1s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - switch.turn_on: blue_led
        else:
          - switch.turn_off: blue_led

I haven’t found a better way other than polling the sensor.

3 Likes

Thank you!

Hate to bring this up again, but is it a switch or a light? I am trying this but am getting errors.

ID 'blue_led' of type esp8266_pwm::ESP8266PWM doesn't inherit from switch_::Switch. 
Please double check your ID is pointing to the correct value.
id: blue_led [source /config/esphome/switch_back_4ch.yaml:95]`

This is what I have in the yaml…

output:
  # Register the blue LED as a dimmable output ....
  - platform: esp8266_pwm
    id: blue_led
    pin: GPIO13
    inverted: True

light:
  # ... and then make a light out of it.
  - platform: monochromatic
    name: "Sonoff 4CH Blue LED"
    output: blue_led
    
interval:
  - interval: 1s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - switch.turn_on: blue_led
        else:
          - switch.turn_on: blue_led

I am also getting errors with light.turn_on as well. What should I be using? Thanks in advance.

blue_led is an output, not a switch. Therefore you cant use the switch.turn_on/off actions. Use the output.turn_on/off actions instead.

interval:
  - interval: 1s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - output.turn_on: blue_led
        else:
          - output.turn_off: blue_led
2 Likes

Tom, thanks for this! I “took the liberty” to use your solution in my setup, too. However, i modified it a bit, so i get a blinking led. I guess that as a side effect i’ll get a visual indication if by any chance module freezes (led will stop blinking).

interval:
  - interval: 1s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - switch.turn_on: test_wifi_connected
          - delay: 500ms
          - switch.turn_off: test_wifi_connected
        else:
          - switch.turn_off: test_wifi_connected

I went a bit further: i programmed my LED so that i know if it’s only connected to wifi or if it’s also connected to HA. “Wifi only” shows by steady 0,5s flashing, while api connected i get pulsed flashing. Ok, i admit, usability is questionable at this point… maybe it will prove usefull.

interval:
  - interval: 1s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - if:
              condition:
                api.connected:
              then:
                - switch.turn_on: test_wifi_connected
                - delay: 200ms
                - switch.turn_off: test_wifi_connected
                - delay: 200ms
              else:
                - switch.turn_on: test_wifi_connected
                - delay: 500ms
                - switch.turn_off: test_wifi_connected
                - delay: 500ms
        else:
          - switch.turn_off: test_wifi_connected  
6 Likes

I know this is old topic, but maybe it will help someone looking for a solution.

I was also looking for a way to control an LED on wifi connect/disconnect state and you can directly configure this in the wifi component like so:

wifi:
  #your wifi settins
  on_connect:
    - switch.turn_on: switch1
  on_disconnect:
    - switch.turn_off: switch1
3 Likes

True, but in this case you don’t know if esp module by any chance locks up, or in any way “go south”. Although rare, it may happen, so it’s better to make led flash in regular intervals.

What about this?

output:
  - platform: gpio
    pin: 12
    id: led_wifi

light:
  - platform: binary
    id: wifi_led
    output: led_wifi
    effects:
      - strobe:
          name: "blink_slow"
          colors:
            - state: true
              duration: 500ms
            - state: false
              duration: 500ms
      - strobe:
          name: "blink_fast"
          colors:
            - state: true
              duration: 200ms
            - state: false
              duration: 200ms

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password
  enable_on_boot: True
  id: wlan
  on_connect: 
    then: 
      - light.turn_on:
          id: wifi_led
          effect: blink_slow

  on_disconnect: 
    then: 
      light.turn_off: wifi_led

api:
  #YOUR CODE

  on_client_connected:
    - if:
        condition:
          lambda: 'return (0 == client_info.find("Home Assistant "));' 
        then:
          - light.turn_on: 
            id: wifi_led
            effect: blink_fast
          
  on_client_disconnected:
    - if:
        condition:
          lambda: 'return (0 == client_info.find("Home Assistant "));' 
        then:
          - light.turn_off: wifi_led

I think this solution is better because when I was using the condition api.connected, it was triggering also if any other api connected, not just home assistant.
Only downside I have encountered with this new approach is that sometimes it takes about 1-2min till the esp realizes that it connected/disconnected to home assistant.

Hm…
first “error” i see in your version is this situation:

  • wifi connects → blink slow = ok
  • api connects → blink fast = ok
  • (later) if api disconnects → led off = NOT ok → you can’t tell if esp is still connected to wifi or not, since this command turns off led, while wifi component doesnt turn on slow blink again. So, here more sophisticated “if” would be required, like if wifi is connected → slow blink, otherwise → led off.

However, i can’t really see why there would be 1-2 minutes delay here…?

My version with interval is now a bit different - it uses flash lentgh:
(substitution “connected_pin” must be defined)

interval:
  - interval: 2s
    then:
      if:
        condition:
          wifi.connected:
        then:
          - if:
              condition:
                api.connected:
              then:
                - light.turn_on:
                    id: ${device_name}_led
                    flash_length: 100ms
              else:
                - light.turn_on:
                    id: ${device_name}_led
                    flash_length: 1000ms
        else:
          - light.turn_on:
              id: ${device_name}_led
              flash_length: 1900ms

light: # ----------------------------------------------------------------------- LIGHT
  - platform: binary
    id: ${device_name}_led
    internal: true
    output: status_ledica_out

output:
  - id: status_ledica_out
    platform: gpio
    pin: ${connected_pin}

But, i think that even in my config there is an option to include “client_info” or “client_ip” into condition. It’s just a matter of knowledge (or trial/error)