Two (or more) ESP8266 to control one lamp

Hi guys!

I´m starting my adventure with Home Assistant and ESPHome. What a great tools! Congratulations to all developers.

Starting with the basics, I’m able to control one lamp using a switch on the ESP and a relay. I can control it with Home Assistant too (entity “light.lamp_1”).

The code is as follows:

esphome:
  name: huzzah_tester
  platform: ESP8266
  board: huzzah

wifi:
  ssid: "xxxx"
  password: "xxxx"
  manual_ip:
    static_ip: 192.168.1.101
    gateway: 192.168.1.1
    subnet: 255.255.255.0

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

dallas:
  - pin: GPIO13

# Individual sensors
sensor:
  - platform: dallas
    address: 0x5600000828510328
    name: "Temperature Sala"

output:
  - platform: gpio
    pin: GPIO12
    id: main_relay
    
light:
  - platform: binary
    name: "Lamp 1"
    id: rele1
    output: main_relay
    
binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: True
    name: "Int. Lamp. 1"
    filters:
      - delayed_on: 50ms
    on_press:
      then:
        - light.toggle: rele1

Now, I want to add another ESP8266 with a switch to control the first one but I don´t know how to start.
If I upload the same code to the second ESP I get another entity in HA named “light.lamp_1_2”.
How can I just have one entity to control the lamp? Can you point me some directions?

Thank you!

If you have two ESPs you have two devices that HA can discover. If you don;t want HA to discover the second device or use it from the front end dont include api: I have no idea why you would want this though. How are you going to control the first switch with the second switch if you cant discover the second switch?

Same as @tom_l why do you want two ESP control same physical device ? it has non sense for me !

Tom, Vincen, thank you for your reply.
I don´t know if I was clear enough.
I want to control the same light in more than one point as seen in the picture (bedroom):


I also want to turn the light on and off in HA.
Ideas are appreciated :wink:

But you have two switches. Why don’t you want them both visible in HA?

In HA I want one control for the light:
image (of course labeled as bedroom light :upside_down_face:)
With that control I must be able to turn on or off the light.
From each one of the ESP I must be able to do the same and reflect it in HA.
All ESP has one switch (push-button) and only one of them (the one with the relay) controls the lamp.

1 Like

you need to set the other two esp devices as only switches that show up in HA then use those switches in an automation to control the first esp device.

I think this might get you started:

esphome:
  name: huzzah_tester_2
  platform: ESP8266
  board: huzzah

wifi:
  ssid: "xxxx"
  password: "xxxx"
  manual_ip:
    static_ip: 192.168.1.101
    gateway: 192.168.1.1
    subnet: 255.255.255.0

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: True
    name: "switch 2"
    filters:
      - delayed_on: 50ms

I believe this will get you a “switch” that will show up in HA. you can then use the entity that is created to control the first device.

I’m pretty new to ESPHome so I could be wrong on any or all of the above but the basic concepts are correct.

If that doesn’t work then you may have to create a “switch” component in ESPHome and expose that to HA and then use that entity to control the other device.

Anything in ESPHome that contains the term “name:” will show up in HA. If you leave out that entry then you won’t see it in ESPHome and it only be available to be used internally within the device itself.

Yes, that’s it :smiley:
Did some research on automation and this code is working as I need:

  trigger:
    platform: state
    entity_id: binary_sensor.quarto_sw_2
    to: 'on'
  action:
    service: homeassistant.toggle
    entity_id: light.lamp_1

The second ESP has the following code as suggested by finity:

esphome:
  name: huzzah_tester_2
  platform: ESP8266
  board: huzzah

wifi:
  ssid: "xxxx"
  password: "xxxx"
  manual_ip:
    static_ip: 192.168.1.102
    gateway: 192.168.1.1
    subnet: 255.255.255.0

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:

binary_sensor:
  - platform: gpio
    pin: 
      number: GPIO4
      mode: INPUT_PULLUP
      inverted: True
    name: "quarto_sw_2"
    filters:
      - delayed_on: 50ms

Thank you all!
:hugs:

1 Like

You could also set the automation on the ESPHome device itself if you don’t want to have too many automations on your HA.

So you could for example set on_press/release of your other two wall switches to toggle the light you have already configured on your first device with the relay

binary_sensor:
   - platform: gpio
     id: bed_switch_2
    name: Bedroom Switch 2
    pin:
      number: GPIO13
      mode: INPUT_PULLUP
      inverted: true
    on_press:
      then:
        - homeassistant.service:
            service: light.toggle
            data:
              entity_id: light.bedroom
    on_release:
      - homeassistant.service:
          service: light.toggle
          data:
            entity_id: light.bedroom
5 Likes

Hi,
Thanks for your solution. I would like to know if is it possible to toggle more lights with the same on_release.
I get an error if I add multiple entity_id after data.

Thanks

Are you adding the lights alone or literally multiple entity_id: xxxxxx

This should work

 - homeassistant.service:
     service: light.toggle
     data:
       entity_id: 
         - light.bedroom
         - light.kitchen
         - light.front_door

If it’s a lot of lights it also possible to make a light group in HA and then define that light.xxxx in the ESPHOME YAML
https://www.home-assistant.io/integrations/light.group/

1 Like
  1. that would be perfect, but i get this error:
    Must be string, got <class ‘esphome.helpers.EList’>. did you forget putting quotes around the value?. (Before I was adding multiple entity_id.)

  2. I prefer not using HA yaml as far as I can.

May I ask you if using homeassistant.service in esphome makes mandatory to keep the server online?
What I mean is if the light button would stop working in case the server goes offline…

I suppose a workaround to that would be to just repeat the above under then: for each light seperately.

The whole point of the above is to control entities that exist in HA. This does not communicate with the lights directly if that is your question. It talks to the HA API therefore requiring HA to be online.

1 Like

That was my first option before bothering you with my question :slight_smile: but probably I was missing something. This now works great.

Thank you for your clarification, unfortunately this was not plain in the documentation:
One less single point of failure: In the ESPHome native API each ESP is its own server. With MQTT, when the broker shuts off nothing can communicate anymore.
This led me to think that devices could somehow be able to talk to each other for simple achievement like this.

But since nodes won’t be reliable without the server, I should consider to give a chance to HA grouping. At least I should not update the firmware to add more lights in the future.

Thanks for precious support and point of view.

1 Like

You could also do the automation in HA itself rather than the ESPHOME There you could easily add remove lights in the action part of the automation perhaps easier than in yaml that you then have to compile and upload to the esphome. Since whether you add them to the ESP or to HA, in both cases they rely on each other.

The only advantage of automation within the ESP is when you set up automations that do things to the ESP itslef that should be local in which case HA doesn’t matter. E.g. saying when this button is pressed on the ESP toggle the relay on the ESP

I agree with this: :slight_smile:
| |
V

As far as automation are somehow more static.

1 Like