Issue on Make a remote Switch with ESP8266

Hello Everyone,

I am building a simple switch with two button and two LED to turn on/off a SONOFF switch.
I reach a issue that i don’t know how to reflect the switch status to the LED with startup.

Could someone can help to finish the LED part?

LED1 connecting to GPIO 5
LED2 connecting to GPIO 6
Button 1 connecting to GPIO 2
Button 2 connecting to GPIO 3

Here is my existing code:

esphome:
  name: my_esp8266
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: "your_wifi_ssid"
  password: "your_wifi_password"

# Enable the button component
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: true
    name: "Button 1"
    on_press:
      then:
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.sonoff_1000XXXXXX0

  - platform: gpio
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
      inverted: true
    name: "Button 2"
    on_press:
      then:
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.sonoff_1000XXXXXX1

You can set up LEDs like this if that’s what you need?

Then after that you can add a toggle light action to your button.

But you might also want to import the state of the Sonoff to help keep everything syncronised - the leds can be interlocked with the true state of the Sonoff.

I would like the led reflect the switch status when the esp8266 startup, i don’t know how to make it…

You need to set up the leds up as light components in esphome. This basically means linking an output, see here Binary Light — ESPHome

Then set the buttons as binary sensors, as you have done, but link them not to a home assistant service, but to the light component, use light.toggle.

I modified the code like this, but it shown error

light:
  - platform: binary
    name: "LED 1"
    output: led1
    restore_mode: ALWAYS_OFF

  - platform: binary
    name: "LED 2"
    output: led2
    restore_mode: ALWAYS_OFF

output:
  - platform: gpio
    pin: GPIO4
    id: led1

  - platform: gpio
    pin: GPIO5
    id: led2


binary_sensor:
  - platform: gpio
    pin:
      number: GPIO2
      mode: INPUT_PULLUP
      inverted: true
    name: "Button 1"
    on_press:
      then:
        - light.toggle: led1
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.sonoff_1000XXXXX0

  - platform: gpio
    pin:
      number: GPIO3
      mode: INPUT_PULLUP
      inverted: true
    name: "Button 2"
    on_press:
      then:
        - light.toggle: led2
        - homeassistant.service:
            service: switch.toggle
            data:
              entity_id: switch.sonoff_1000XXXXX1

You need to give your lights an id too, then refer to this Id in your toggle action.

By the way, the benefit of using a monochromatic light over a binary light is you can dim them. Even if I just want an on/off I typically use a monochromatic so I can set a custom on brightness (and sometimes have different day/night brightness.)

1 Like

Dunno why you have a homeassistant service there.

Plus what @Mahko_Mahko said

1 Like

I think the sonoff is a seperate esp.

1 Like

Perhaps. But then the original post is very ill-defined.

1 Like

yes, sonoff is another esp

OKOK, Thanks a lot for the advise, added id, the error gone
sorry, i just start to play esphome, and i just copy the code from webpage, so, it may be something wrong…

1 Like