ESP32 Smart Relay for Home Assistant (ESPHome)


This guide shows how to build a safe ESP32 smart relay (Shelly-style) for Home Assistant using ESPHome (optional MQTT). Includes correct wiring (low-voltage vs mains), relay module selection, GPIO โ€œsafe pinsโ€, EMI/noise tips, and example ESPHome YAML for a reliable on/off switch with status, restore mode, and basic automations.

A โ€œShelly-styleโ€ smart relay is basically three things in one box:

  1. A microcontroller (ESP32)
  2. A relay (to switch a load)
  3. A safe power solution (ACโ†’DC or DC supply), plus proper wiring and noise control

This article focuses on making it reliable and safe, not just โ€œit works on a breadboardโ€.

Mains voltage can kill you and burn your house down.
If youโ€™re not confident working with AC wiring, build the low-voltage version first (12V/24V DC loads), or buy a certified relay device.

1) What you can build (use cases)

A smart relay is perfect for:

  • Lights (via contactor/relay depending on load)
  • Bathroom fan (with humidity automation)
  • Garage door trigger (pulse mode)
  • Irrigation valve control (via proper driver/relay)
  • Boiler enable input (only if you know the control circuit)

2) Choose your relay hardware (donโ€™t cheap out blindly)

Option A: 1-channel relay module (easy)

For beginners and quick builds:

  • Use an opto-isolated relay module designed for microcontrollers.

Look for:

  • Proper transistor driver on the board
  • Optocoupler (nice-to-have)
  • Clear labeling: VCC, GND, IN, COM, NO, NC

Avoid modules that:

  • Donโ€™t have a transistor/driver (some super-cheap boards are sketchy)
  • Have no diode/snubber considerations for inductive loads
  • Have bad isolation distances on the PCB

Option B: โ€œBare relay + transistor driverโ€ (best practice)

If you want to do it properly:

  • Use a relay + NPN transistor or MOSFET driver
  • Add flyback diode
  • Add snubber/MOV where needed (AC inductive loads)

This is closer to what commercial products do.


3) Powering it (the #1 cause of random resets)

ESP32 hates bad power. Relay coils and mains noise make it worse.

Recommended stable approach

  • Power ESP32 from a decent 5V supply (USB charger, buck module, or AC-DC module)
  • Then use the ESP32 boardโ€™s 3.3V regulator (or a good external 3.3V regulator)

Add capacitors near the ESP32:

  • 100 ยตF electrolytic on 5V rail (or 3.3V rail)
  • 0.1 ยตF ceramic close to the ESP32 VCC/GND

If using mains AC-DC modules (Shelly-style)

Typical ACโ†’DC module choices:

  • Encapsulated AC-DC modules (safer than bare capacitive droppers)
  • Proper fusing + enclosure + strain relief is mandatory

Again: if youโ€™re not experienced with mains, do the DC version first.

4) Wiring overview

4.1 Low-voltage DC load (recommended first build)

Example: 12V LED strip, DC fan, small DC device.

  • 12V+ โ†’ Relay COM
  • Relay NO โ†’ Load +
  • Load โ€“ โ†’ 12Vโ€“
  • ESP32 powered from a separate stable 5V/3.3V regulator
  • Grounds should be common if needed (depends on module), but keep power wiring tidy.

4.2 Mains AC load (danger zone)

For AC wiring youโ€™re switching LIVE typically:

  • LIVE โ†’ Relay COM
  • Relay NO โ†’ Load LIVE
  • NEUTRAL goes directly to load
  • Earth/ground must be continuous where applicable

Do not route mains over breadboards.
Use:

  • Proper screw terminals
  • Enclosure
  • Cable glands
  • Fuse
  • Adequate isolation/clearance

5) Best ESP32 pins for relay control

Use โ€œsafeโ€ GPIO that donโ€™t affect boot:

Good default pins:

  • GPIO25, GPIO26, GPIO27, GPIO32, GPIO33, GPIO18, GPIO19, GPIO23

Avoid for relay input (especially if the relay board pulls the pin at boot):

  • GPIO0, GPIO2, GPIO12, GPIO15 (boot/strapping pins)

For this guide weโ€™ll use GPIO25.

6) ESPHome YAML (simple, reliable smart relay)

6.1 Minimal config (ESPHome API)

esphome:
name: esp32-smart-relay
friendly_name: ESP32 Smart Relayesp32:
board: esp32dev
framework:
type: arduinologger:
api:
ota:wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
ap:
ssid: "Relay Fallback"
password: !secret ap_passwordcaptive_portal:

6.2 Relay switch (GPIO25)

switch:
- platform: gpio
name: "Relay"
pin: GPIO25
id: relay
restore_mode: RESTORE_DEFAULT_OFF

restore_mode notes

  • RESTORE_DEFAULT_OFF is safest (after power outage, relay stays off).
  • If you need โ€œreturn to previous stateโ€ after outage, use RESTORE_DEFAULT_ON or RESTORE_INVERTED_DEFAULT_OFF depending on wiring, but be carefulโ€”some loads must not start unexpectedly.

6.3 If your relay module is โ€œactive LOWโ€

Many relay modules turn on when the input is pulled LOW. If yours behaves inverted, add:

switch:
- platform: gpio
name: "Relay"
pin:
number: GPIO25
inverted: true
restore_mode: RESTORE_DEFAULT_OFF

7) Add a physical button (optional but very โ€œShelly-likeโ€)

Wire a momentary button from GPIO4 โ†’ GND.

binary_sensor:
- platform: gpio
pin:
number: GPIO4
mode: INPUT_PULLUP
inverted: true
name: "Wall Button"
filters:
- debounce: 30ms
on_press:
- switch.toggle: relay

Now the relay can be controlled from:

  • Home Assistant
  • local physical switch
  • automations

8) Safety + reliability upgrades (worth doing)

8.1 Flyback diode (for relay coils if you build your own driver)

If youโ€™re driving a bare relay coil:

  • Add a diode across the coil (1N4148/1N4007) to kill voltage spikes.

Most relay modules already include itโ€”verify.

8.2 Snubber or MOV (for AC inductive loads)

For loads like:

  • fans
  • pumps
  • contactors
    โ€ฆuse an RC snubber or MOV to reduce arcing and EMI.

This reduces:

  • relay contact wear
  • ESP32 resets due to EMI spikes

8.3 Separate relay supply (when loads are nasty)

If the relay coil supply is noisy:

  • power relay coil from a separate 5V supply
  • keep grounds common only if necessary
  • route coil currents away from ESP32 ground paths

8.4 Enclosure and strain relief

A โ€œrealโ€ Shelly-style build must include:

  • insulated enclosure
  • strain relief
  • fuse (on mains input)
  • correct wire gauge
  • proper terminals

9) Home Assistant setup ideas (automations)

9.1 Bathroom fan automation (humidity)

  • ON when humidity > 70%
  • OFF when humidity < 60%
    (Add hysteresis so it doesnโ€™t flap.)

9.2 Safety timer (auto-off)

Turn relay off after X minutes:

  • Great for heaters, irons, pumps, etc.

9.3 โ€œPulse modeโ€ (garage door / gate trigger)

If you need a momentary pulse:

switch:
- platform: gpio
name: "Gate Trigger"
pin: GPIO25
id: relay
restore_mode: RESTORE_DEFAULT_OFF
on_turn_on:
- delay: 300ms
- switch.turn_off: relay

10) Troubleshooting (common failures)

Relay clicks but ESP32 resets

  • Power supply is weak or noisy
  • Add bulk capacitance, improve regulator, shorten power wires
  • Add snubber/MOV if switching inductive AC loads

Relay is ON at boot

  • You used a boot strap pin, or relay input floats
  • Move relay control to a safer GPIO (25/26/27/32/33)
  • Add proper pull-up/pull-down as needed (carefully)

Home Assistant shows โ€œunavailableโ€

  • Wi-Fi issues or power instability
  • Add fallback AP, improve power, check router RSSI

Share your love