Help with LED and switch

Hello all.
I am a newb and just starting to play around with Homeassistant/ESPhome. I have a very simple application that I would like to design and am looking for some advice. Here is what I would like to do:

Wemos D1 mini 1
(Push a button wired to it)

Wemos D1 mini 2
(LED wired to it)

Whenever the pushbutton on the Wemos 1 is pressed, light up the attached LED on Wemos 2.
If the pushbutton on Wemos 1 is not pressed, do NOT light up the LED on Wemos 2.

I think this is fairly simple, but despite searching, I am struggling. Can anyone offer some advice? I have both successfully connected to Homeassistant and I can see them in my interface. In fact, the pushbutton shows its status in the interface perfectly, but the LED is not working. Below are my current attempts for each Wemos.

===============
Wemos 1:

binary_sensor:

  • platform: gpio
    name: “Lock”
    pin:
    number: D3
    inverted: True
    mode: INPUT_PULLUP
    on_state:
    then:
    - output.turn_on: lockled

===============
Wemos 2:

binary_sensor:

  • platform: gpio
    name: “LockLED”
    id: lockled
    pin:
    number: D3
    inverted: True

===============

Thanks!

Please learn how to properly quote your code.

The devices do not communicate with each other or have share namespace like lockled in your example.

But home assistant is the way to do it.

ESP1 needs to set a entity in HA to on. Like a binary sensor.

And ESP2 needs to read the state of this sensor and do the action.
Change the platform of #2 to homeassistant and it should read what #1 does.

platform: homeassistant
id: lockled
entity_id: binary_sensor.NameOf#1BinarySenorInHomeAssistant

Upload the codes and enter the log window of #2 and see if the code works when you press #1 button.

1 Like

Sorry, I suppose neither of you understood my current situation. Both devices are recognized in Homeassistant, but I am having trouble with them working together in the way that I would like. Here is the complete code:

Wemos 1


esphome:
  name: frontdoor
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "SSID"
  password: "PASSWORD"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Frontdoor Fallback Hotspot"
    password: "fIG3haXqbWlN"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    name: "Lock"
    pin:
      number: D3
      inverted: True
      mode: INPUT_PULLUP
    on_state:
      then:
        - output.turn_on: lockled

Wemos 2

esphome:
  name: bedroom
  platform: ESP8266
  board: d1_mini

wifi:
  ssid: "SSID"
  password: "PASSWORD"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Bedroom Fallback Hotspot"
    password: "N1EyBuhFP0i6"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    name: "LockLED"
    id: lockled
    pin:
      number: D3
      inverted: True

OK… sure…
Come back when you actually want help.

I am not sure why you are being rude and difficult? Why bother even replying with an answer like that?

I tried to be concise in my original post and highlight what I assumed to be the problem. It’s true, I did not use the code syntax when posting, but I figured it was a simple and short enough code so it was not essential.

Then, I posted the code in full as requested and you just throw insults at me? I am confused, where is the problem?

Your configs posted above doesn’t reveal a led connected to either wemos AFAICT.

You probably want something like this:

light:
  - platform: binary
    name: "Desk Lamp"
    output: light_output

output:
  - id: light_output
    platform: gpio
    pin: GPIO16

on the one with the led.

In home assistant use an automation that is triggered on the binary sensor on the wemos with the button, and turns on the light on the other wemos.

Thank you, I appreciate the help.

I read through the examples and thought (with little confidence) that I could light the LED by simply enabling a GPIO pin. Clearly I was not correct. For what it’s worth, I did not come up with that code on my own I copied it from some examples I saw…I am still trying to understand the flow and variables available.

Please correct me if I am wrong…I assume the “Light” section tells home assistant that it should use a light icon or something? I had used the other code because I didn’t quite understand why a relay or switch would be different (electrically) from an LED, ie. You make a pin HIGH, how does the micro controller know the difference?

Regardless, I am happy to try your suggestion and will post back with an update.

Now you need to go to HA and create an automation to turn on the led, when the switch is pressed.

I posted a step along the way to get you going and you completely dismiss it without trying with the comment that I don’t understand.

I expected you to at least try, anything less than that is in my opinion quite rude.
YMMV.

I have a different question…maybe not right for this forum.

I understand the logic of setting each esp to connect to HA and letting HA handle the automation (if button pressed, then light up), but is it possible to have the two units operate more independently and have HA simply monitor status?
One example I can think of:

ESP1->publishes status to MQTT server
ESP2->reads MQTT status and responds
HA->monitors MQTT server and shows results

Is there any benefit to this approach or something similar?

1 Like

Yes that should work. So should using the http api.

There’s a benefit if you don’t use Home Assistant, but still want to make the two devices talk.

There’s not a benefit if you are already running Home Assistant. Mind that letting them talk through MQTT requires an MQTT broker, so you switch one external component with the other and still end up with three components in total. It doesn’t make them more independently.

If you want to let the two connect directly to each other, I there’s there is some integration work being done on something called ESP-Now, but I have never really looked at it closely. No idea how mature or complete it is. It might be worth checking out though.

the MQTT broker works providing a BUS for messages. when posting a message, the TOPIC is read by the devices that subscribed that topic, so …
if your ESP1 publish in the topic that ESP2 is subscribed, it will trigger the action .

nevertheless , if you already have the MQTT broker, I would recommend to have HA doing the automation. More simple and more managable IMHO

Thank you all I had some more time to play with this today and I was able to add the light element and create an automation to get things working!

With a working example, I will tweak and hopefully build from here. Perhaps I will look into the MQTT approach at a later date, but this was not very difficult to get running (once I had the light code corrected).

I appreciate the help.

1 Like