ESP32 internal switch issue

I need help with stupid problem . I have an esp32 board, which I would like to use like a switch for relay and switch which will be connected to external SW to control same lights( something like a smart SW) I use GPIO4 to control relay ( working fine) and I connect 1 wire to GPIO15 and the other wire to GND , and these 2 wires I would like to connect to regular wall switch ( common and output) but for some reason this configuration not working. When I connect GPIO 15 to ground, nothing happened.

esphome:
  name: ld2410-presence-sensor
  friendly_name: Irina Closet Light

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:


# Enable Home Assistant API
api:
  encryption:
    key: "   "

ota:
  password: "  "
  platform: esphome

wifi:
  ssid: ssid 
  password: psw


  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "   "
    password: "   "
switch:
  ### Relay 1 ######
  - platform: gpio
    pin: GPIO4
    id: relay1
    name: "Irina Closet Light"
    inverted: False
    icon: mdi:wall-sconce-flat
  - platform: gpio
    pin: GPIO15
    id: relay2
    name: "Irina Closet Light-1"
    #inverted: True
    icon: mdi:wall-sconce-flat
    internal: True
    on_turn_on:    
    - switch.turn_on: relay1
    on_turn_off:    
    - switch.turn_off: relay1




web_server:
  port: 80

So, gpio4 controls the relay and gpio15 is the switch? Its kind of counterintuitive but, you dont configure it as a switch, you configure it as a binary_sensor and because your using active Low(switch closes and connects to Gnd, pulling gpio15 Low) you need to turn on internal pull up resistor.

1 Like

Thank you for your help, it’s working now the way I wanted.

switch:
  ### Relay 1 ######
  - platform: gpio
    pin: GPIO4
    id: relay1
    name: "Irina Closet Light"
    inverted: False
    icon: mdi:wall-sconce-flat
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO15
      inverted: True
      mode:
        input: true
        pullup: true
    id: relay2

If you don’t mind to answer, one more thing. Can I use internal automation with binary sensor?

    on_turn_on:    
    - switch.turn_on: relay1
    on_turn_off:    
    - switch.turn_off: relay1

definately! You can create very simple to very complex automation’s that run internally on the esp32. I try to run as much of my automation’ s on my esp nodes as I can. Doing it that way prevents you from losing 100% control of devices/automations in the event HA ever goes down or wifi because, everything is on the esp board and doesn’t need HA or wifi in most cases.

1 Like

Thank you. I read all this information before and use some of it for the other projects, but today I was stuck ( brain freeze). You save me a lot of time, thank you. Working like a charm now.

switch:
  ### Relay 1 ######
  - platform: gpio
    pin: GPIO4
    id: relay1
    name: "Irina Closet Light"
    inverted: False
    icon: mdi:wall-sconce-flat
binary_sensor:
  - platform: gpio
    pin:
      number: GPIO15
      inverted: True
      mode:
        input: true
        pullup: true 
    id: relay2     
    on_press:    
      - switch.turn_on: relay1
    on_release:    
      - switch.turn_off: relay1