Help with ESPHome and HomeAssistant

Hi! I’m new to automation and stuff. I’ve been working on HomeAssistant and ESPhome lately. I’ve set up a few nodes on ESPhome using NodeMCU. On one of the nodes, I’ve relays hooked to the lights in my bedroom. I wish to control my lights using both the physical switch on my switch board and the virtual switches in HA dashboard. So I created a Binary Senor on one of the GPIO pins of my NodeMCU and attached it to the physical switch. Under the automations section in HomeAssistant, I’ve assigned automation to toggle my light switch every time the state of my binary sensor changes. This for instance is working but I don’t think it is the wisest way of doing it. So, I wish to figure out if there is a way to include the code to toggle the relay connected to my light, everytime I turn on or off my physical switch on the switch board.

This will localize the automation as it doesn’t require HomeAssistant’s intervention to trigger the entities connected to the same node from the same node. Even if my network goes off or my homeassistant goes unresponsive, I can still be able to trigger my lights using the physical switch.

P.S. It is a switch, not a push button.

Thanks in advance!

Hi Krishna,

sorry I know that my answer will not really fit what you have questioned but I hope it will help you to get on the right track (its my garagedoor opener with a push-button, yes different from yours but it shows how actions are performed on the device itself) :

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: !secret esphomepassword

ota:
  password: !secret esphomepassword


# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time

# Text sensors with general information.
text_sensor:
  # Expose WiFi information as sensors.
  - platform: wifi_info
    ip_address:
      name: torneuegarage_IP
    ssid:
      name: torneuegarage_SSID
    bssid:
      name: torneuegarage_BSSID

# Sensors with general information.
sensor:
  # Uptime sensor.
  - platform: uptime
    name: torneuegarage_uptime

  # WiFi Signal sensor.
  - platform: wifi_signal
    name: torneuegarage_WiFi_Signal
    update_interval: 60s

binary_sensor:
- platform: gpio
  id: button
  pin:
    number: GPIO0
    mode: INPUT_PULLUP
    inverted: true
  name: "torneuegarage schalter"
  device_class: garage_door
  filters:
    # Small filter, to debounce the button press.
    - delayed_on: 100ms
    - delayed_off: 100ms
  on_press:
    # Only turn on the chime when it is active.
    - switch.turn_on: relay
  on_release:
    # On release, turn of the chime. 
    - switch.turn_off: relay



- platform: gpio
  pin:
    number: GPIO14
    mode: INPUT_PULLUP
    inverted: false
  name: "torneuegarage_door"
  device_class: garage_door

switch:
- platform: gpio
  id: relay
  pin:
    number: GPIO12
    inverted: False
  restore_mode: ALWAYS_OFF
- platform: template
  name: "torneuegarage relay"
  turn_on_action:
  - switch.turn_on: relay
  - delay: 500ms
  - switch.turn_off: relay

status_led:
  pin:
    number: GPIO13
    inverted: yes