Relay controller from HA and local GPIO

Hello. I have a project where I want to control a relay. Basically the logic is that I get +12V in from a control panel. This +12V I could step down to 3V to sense on GPIO on the ESP, or I could run the 12V through a relay to close a GPIO pin to high or low depending on the preference.

When this is done I want the ESP to close another relay automatically, no interaction from HA. In the HA interface / MQTT the status should update a relay switch to on. Should the 12V shut down to 0V the same relay output should open/disconnect.

The same relay switch should also be possible to control from HA, and preferrably also be able to read the status of the 12V input (does not matter if is represented as an on/off sensor or a voltage).

Sadly I suck at writing code. I guess I need to write a custom sensor. Could anyone help with this?

So far I have tinkered around the ESP32 to control a relay. That works.

esphome:
  name: relaynode
  platform: ESP32
  board: mhetesp32devkit
wifi:
  ssid: 'WIFI NETWORK'
  password: 'WIFI PASSWORD'
# Enable logging
logger:
# Enable Home Assistant API
api:
  password: 'HA API PASSWORD'
ota:
  password: 'OTA PASSWORD'
  
web_server:
  port: 80
switch:
  - platform: gpio
    name: "test_lights_deca Onboard light"
    pin: 2
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
    
  - platform: gpio
    name: "Relay_1"
    pin: 25
    inverted: True

Now I need the sensor for the 12V, logic to trigger the Relay_1 without having to talk to HA (preferrably) and report the sensor on/off status to HA.

esphome:
  name: relaynode
  platform: ESP32
  board: mhetesp32devkit
wifi:
  ssid: 'WIFI NETWORK'
  password: 'WIFI PASSWORD'
# Enable logging
logger:
# Enable Home Assistant API
api:
  password: 'HA API PASSWORD'
ota:
  password: 'OTA PASSWORD'
  
web_server:
  port: 80
switch:
  - platform: gpio
    name: "test_lights_deca Onboard light"
    pin: 2
    inverted: True
    restore_mode: RESTORE_DEFAULT_OFF
    id: output_relay  #### <--- Note this ####

binary_sensor:
  - platform: gpio
    pin: D2 # or whatever pin you use
    name: "Input Relay" #  Or whatever you want to call it
    filters:
      - delayed_on: 10ms  # debounce the relay contacts
      - delayed_off: 10ms  
    on_press:
      then:
        - switch.turn_on: output_relay
    on_release:
      then:
        - switch.turn_off: output_relay

See Binary sensor automation for more.

Thank you @tom_l. I will test this!

That worked just perfect @tom_l, thank you!

I had to move the sensor and relay control due to det pull_up in boot of the ESP. Cannot have the relay just switch at boot. Altered the code like this to make it work:

esphome:
  name: eber_d4_control
  platform: ESP8266
  board: nodemcuv2  

wifi:
  ssid: "replace"
  password: "replace"

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Eber D4 Control Fallback Hotspot"
    password: "replace"

captive_portal:

# Enable logging
logger:

# Enable Home Assistant API
api:
  password: "replace"

ota:
  password: "replace"

web_server:
  port: 80
switch:
  - platform: gpio
    name: "test_lights_deca Onboard light"
    pin: 2
    inverted: True
    restore_mode: ALWAYS_OFF
    id: output_led  #### <--- Note this ####
  - platform: gpio
    name: "d1_relay_signal"
    pin: D1
#    inverted: True
    restore_mode: ALWAYS_OFF
    id: output_relay
binary_sensor:
  - platform: gpio
    pin: D2 # or whatever pin you use
    name: "Input Eber Controlpanel D801" #  Or whatever you want to call it
    filters:
      - delayed_on: 10ms  # debounce the relay contacts
      - delayed_off: 10ms  
    on_release:
      then:
        - switch.turn_off: output_relay
        - switch.turn_off: output_led
    on_press:
      then:
        - switch.turn_on: output_relay
        - switch.turn_on: output_led

If anyone is curious this is what I am building. It is a heater control for a boat. :slight_smile:

But, the issue I am having is that when the ESP boots it defaults to “on” for the “Input Eber Controlpanel D801” even if it is off. How can I, either get the ESP to detect the state, or default the binary sensor to off?

Thanks again!

Enable the pull_up resistor for D2 (It’s floating at the moment - not good) and invert the signalling (when your switch D801 is on D2 is pulled to ground).

binary_sensor:
  - platform: gpio
    pin: 
      number: D2
      mode: INPUT_PULLUP
      inverted: True
    name: "Input Eber Controlpanel D801"
    filters:
...etc

That worked just wonderfully! I had to put in a bigger resistor on R1 to 5KOhm, but then it works just as I want it to. Love it! :smiley: Thanks @tom_l!

1 Like