ESPHome, Sonoff, show up as light, NOT switch

I have been using this example code from @frenck
The code works perfect, but i would like that the sonoff showed up in HA as a light, not switch (better for google assistant)
I still want all the “bells and whistles” (led status that is correct, the use of the physical button on the sonoff should work as in this example)

I know i can transform the switch in homassistant to a light, but i was thinking that this must be possible in the ESPHome yaml code itself.

I can figure out how to have the sonoff show up as a light, but then i can’t implement the “bells and whistles” :frowning:

esphome:
  name: mcsonoff03
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: 'XXXXXXX'
  password: 'XXXXXXXX'
  fast_connect: true
  manual_ip:
    static_ip: 192.168.111.32
    gateway: 192.168.111.1
    subnet: 255.255.255.0

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin:
      number: GPIO0
      mode: INPUT_PULLUP
      inverted: True
    name: "McSonoff03 Button"
    on_press:
      - switch.toggle: fakebutton

switch:
  - platform: template
    name: "McSonoff03 Relay"
    optimistic: true
    id: fakebutton
    turn_on_action:
    - switch.turn_on: relay
    - light.turn_on: led
    turn_off_action:
    - switch.turn_off: relay
    - light.turn_off: led
  - platform: gpio
    id: relay
    pin: GPIO12

output:
  - platform: esp8266_pwm
    id: basic_green_led
    pin:
      number: GPIO13
      inverted: True

light:
  - platform: monochromatic
    name: "McSonoff03 Green LED"
    output: basic_green_led
    id: led

sensor:
  - platform: wifi_signal
    name: "McSonoff03 WiFi Signal"
    update_interval: 30s
  - platform: uptime
    name: "McSonoff03 Uptime"

text_sensor:
  - platform: version
    name: "McSonoff03 ESPHome Version"

Basically you need to use a GPIO output component with a binary light component. Here’s how I’ve set up mine: https://github.com/DarkFox/ha-config/blob/master/esphome/kitchen_sink.yaml

3 Likes

Thanx!
Exactly what i was looking for.

1 Like

Hi @DarkFox I followed your example and it worked great. My Martin Jerry switch is showing as a light instead of a switch. The only thing I can’t figure out is how to tie the blue LED to turn on when the relay is active. Any tip? Thanks.

Hmm, I’m not sure. The switch component has triggers for turn_on/off, but light doesn’t. Perhaps @OttoWinter has a suggestion?

you can use something like this:

  interval:
  - interval: 1s
    then:
      - if:
          condition:
            light.is_on: light id
          then:
            light.turn_on: led_id
          else:
            light.turn_off: led_id

Thanks @DarkFox and Thanks @glmnet for the suggestion.

I did some digging and found I can use the power supply component and tying it to the blue led to turn on when the relay is on. Here’s a snippet of the code:

binary_sensor:
# Define button to toggle relay and blue LED
  - platform: gpio
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: True
    name: ${friendly_name} Button
    on_press:
      - light.toggle: lightsw
    internal: True
  - platform: status
    name: ${friendly_name} Status

# Define output for relay and toggle blue LED
output:
  - platform: gpio
    pin: GPIO12
    id: relay
    power_supply: blue_led

# Expose to HA as light    
light:
  - platform: binary
    name: ${friendly_name}    
    id: lightsw
    output: relay

# Define output for blue LED
power_supply:
  - id: blue_led
    enable_time: 0s
    keep_on_time: 0s
    pin:
      number: GPIO5
      inverted: True

1 Like

A bit hacky, good job!