Toggle the state of switch on HA using a physical button connected to ESP8266

I am using Adafruit feather Huzzah (ESP8266 board) and a relay to switch an AC bulb using toggle switch on HA. It works really well. Now i came across a situation when the user donot have a cell phone with him and he wants to switch this bulb. There is no way he can do it.
In order to create a way i think i can connect a SPDT switch to one of the pin of esp8266 and monitor it in esp8266 software and when button is pressed it should turn on/off the relay. Also if i turn on the realy using the physical switch the state of toggle button on the HA page should also change. so that i can now turn the bulb off from the on screen button as well as from the physical button

Any suggestion on how this can be done?

Yup you can do it through MQTT via different firmware (ESP easy fw, arduino ide…) I personnaly like esp easy fw. wiki + download

How do you currently control the light from HA? Is it via MQTT or a web server or something else?

You can create a binary sensor this way and trigger the light depending on the payload status:

- platform: mqtt
  state_topic: "your/topic" 
  device_class: light
  name: "light switch sensor"
  qos: 0
  payload_on: "0"
  payload_off: "1"

I supposed that you use MQTT to control your relay? If it’s the case, just make your ESP publish an MQTT message whenever the button is pressed.
Than use the toggle function in an automation:

automation:
  - alias: Toggle lamp status using button
    trigger:
      platform: mqtt
      topic: home/MQTTButton1
    action:
      service: switch.toggle
      entity_id: switch.your_lamp_entity_id

@Kareem for your solution I suppose the button would have to be aware of the relay state to send either 0 or 1 as a payload.

1 Like

@touliloup not necessarly I forgot to attach this but you are right it needs the automation rule and look much logical to write it the way you did with a toggle thx for the advise

  - alias: "light switch sensor ON"
    trigger:
      - platform: mqtt
        topic: Switch2/Control
        payload: '0'
    action:
        service: light.turn_on

  - alias: "light switch sensor Off"
    trigger:
      - platform: mqtt
        topic: /Switch2/Control
        payload: '0'
    action:
        service: light.turn_off

i have HA running on raspberrypi and is communicating with esp using MQTT

Good, that makes things straightforward. Does the current set up return a response to the mqtt message from HA, or does HA currently assume that the switch has been activated?

If it replies with a message, then turning the new switch on the esp8266 needs to send this message. If it doesn’t currently, we need to make it do so (which is more reliable anyway).

HA assume the switch has been activated when it receive the MQTT publish from the ESP so HA should trigger on the real state of your switch.

switch pressed >Input detected by the ESP >ESP MQTT publish > HA MQTT subscribe > HA trigger the event (automation)

If you got a 2 position switch on/off (up/down) you should maybe do things a bit different and maybe wire 2 inputs on your ESP and punlish 2 diferent MQTT message depending on which input have been detected.

hope it’s enough helpful

@deebot : Did you get it to work … I am looking to do similar thing with NodeMcu 8 relay board but i am getting nowhere with it …

ya i got it working but partially, sometimes it doesnt work trying to fix will share once done

sir

how can i integrate 1 button to ON and OFF in home assistant with state showing ON/OFF. I can only press to ON with state in home assistant showing ON, but when I press OFF, it does not respond.
I try using physical remote and can only show ON in home assistant.
My push button only support single push button Data in Tasmota.
I’m using MQTT over the rf bridge