Cheap and Easy Control of 8 Relays through Home Assistant

Following up on my previous post of the Cheap and Easy series, I’m going to show you how you can control 8 relay channels for about 25 bucks and in about 30 minutes. If you’re not familiar with Home Assistant or ESPHome, check out my previous post.

In this project, I use a Wemos D1 Mini as the wifi enable the controller to interface with the relay module using the IO expansion shield. The magic of this setup is using the IO module and soldering it directly onto the relay board. This eliminates the narly mess you see you when would typically have 8 channels of relays. As a bonus, since you are only using 8 of the available 16 channels on the IO module, you could use the remaining 8 IO to do all sorts of other things.

Shopping list:

Wemos D1 Mini – The Wemos D1 Mini is an ESP8266 based microcontroller that will handle the heavy lifting of WiFi and communicating to your Home Assistant instance [ Amazon, 3 Pack, Amazon, 5 Pack ] MCP23017 IO Expansion Module – This is what you will use to allow you to drive all 8 relay channels with minimal effort [ Amazon ] 8 Channel Relay Board – I added the DHT to get temperature and humidity data [ Amazon ] Assorted Dupont Jumpers – Always have a bunch of these handy, they have a tendency to disappear and get used up in projects. [ Amazon ]OPTIONAL: If you want to control 12v or 24v loads Appropriate DC power supply for your loads – This will vary on what you are trying to control, links to some options. [ Amazon 12v 5A | Amazon 24v 3A ] LM2596S DC-DC power regulator – It converts garage door’s 12V DC to 5V DC for the Wemos or use a microUSB cable. [ Amazon ] Soldering Iron Kit – It’s likely that you already have this if you’ve been DIYing for a while [ Amazon ]

Installation is cake

You will need to provide 5v DC power to power the system. To get started, you will want to place the IO module over the pins of the relay module. Make sure to line these up correctly as I’ve indicated below. If you don’t, it’s a big pain to remove it and put it back on correctly, especially if you’ve soldered it in place. Ask me how I know.

The IO module will have a total of 4 wires connected to it: two for +5v and GND from the power supply, and two for the I2C (SDA and SCL) from the Wemos.

The IO module will have a total of 4 wires connected to it: two for +5v and GND from the power supply, and two for the I2C (SDA and SCL) from the Wemos.

That’s it!

Since it’s likely that you will want to switch/power things that are of higher voltage than the 5v rail you need to power the Wemos, I’ve included the 12v and 24v optional power supplies. Be sure that you do not connect any of these higher voltages to any of the logic pins on the Wemos or the IO, only connect them to the relay output terminals.

ESPHome config

I created the 8 channels as output switches and for the time being omitted the remaining 8 channels which can be configured as inputs or outputs. I created a new device, named it esphome_relay_board using a D1 mini board with the following config:

esphome: name: esphome_relay_board platform: ESP8266 board: d1_mini wifi: ssid: 'insert' password: 'your_own' fast_connect: on api: ota: web_server: port: 80 logger: i2c: sda: D1 scl: D2 scan: True mcp23017: - id: 'mcp23017_hub' address: 0x20 switch: - platform: restart name: "Relay Channel Board REBOOT" - platform: gpio name: relay_channel_1 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 15 mode: OUTPUT inverted: True - platform: gpio name: relay_channel_2 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 14 mode: OUTPUT inverted: True - platform: gpio name: relay_channel_3 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 13 mode: OUTPUT inverted: True - platform: gpio name: relay_channel_4 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 12 mode: OUTPUT inverted: True - platform: gpio name: relay_channel_5 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 11 mode: OUTPUT inverted: True - platform: gpio name: relay_channel_6 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 10 mode: OUTPUT inverted: True - platform: gpio name: relay_channel_7 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 9 mode: OUTPUT inverted: True - platform: gpio name: relay_channel_8 icon: mdi:electric-switch pin: mcp23017: mcp23017_hub number: 8 mode: OUTPUT inverted: True status_led: id: status_led pin: number: GPIO2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 esphome : name : esphome_relay_board platform : ESP8266 board : d1_mini wifi : ssid : 'insert' password : 'your_own' fast_connect : on api : ota : web_server : port : 80 logger : i2c : sda : D1 scl : D2 scan : True mcp23017 : - id : 'mcp23017_hub' address : 0x20 switch : - platform : restart name : "Relay Channel Board REBOOT" - platform : gpio name : relay_channel_1 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 15 mode : OUTPUT inverted : True - platform : gpio name : relay_channel_2 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 14 mode : OUTPUT inverted : True - platform : gpio name : relay_channel_3 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 13 mode : OUTPUT inverted : True - platform : gpio name : relay_channel_4 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 12 mode : OUTPUT inverted : True - platform : gpio name : relay_channel_5 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 11 mode : OUTPUT inverted : True - platform : gpio name : relay_channel_6 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 10 mode : OUTPUT inverted : True - platform : gpio name : relay_channel_7 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 9 mode : OUTPUT inverted : True - platform : gpio name : relay_channel_8 icon : mdi :electric-switch pin : mcp23017 : mcp23017_hub number : 8 mode : OUTPUT inverted : True status_led : id : status_led pin : number : GPIO2

The components above include

An I2C component to enable I2C communication

A MCP23017 component for the IO module

A switch restart component to reboot the Wemos

A switch GPIO component for the relay

A status LED component for indicating networking/error status.

Now can compile the firmware and flash it to your Wemos D1. Again, if you’re not familiar with how to do this, reference the videos linked above. Once it’s flashed, power cycle the device. Now you should be able to open the web GUI for the device by either going to it’s IP or going to http://esphome_relay_board.local in your browser, or whatever you named it. When the page loads up, you should test out the relay switches by hitting the button next to them.

Integration into Home Assistant

Now to integrate it into Home Assistant, you will need to go to configuration > integrations. Assuming you have discovery enabled, you will have a new ESPHome device, click configure and add it to your system. If you don’t have any existing conflicting entities, your entity names should match the ones I have. I’m using the following config for lovelace:

- type: entities title: Garage Relay Module + IO show_header_toggle: false entities: - switch.relay_channel_1 - switch.relay_channel_2 - switch.relay_channel_3 - switch.relay_channel_4 - switch.relay_channel_5 - switch.relay_channel_6 - switch.relay_channel_7 - switch.relay_channel_8 - switch.garage_relay_module_reboot 1 2 3 4 5 6 7 8 9 10 11 12 13 - type : entities title : Garage Relay Module + IO show_header_toggle : false entities : - switch . relay _ channel _ 1 - switch . relay _ channel _ 2 - switch . relay _ channel _ 3 - switch . relay _ channel _ 4 - switch . relay _ channel _ 5 - switch . relay _ channel _ 6 - switch . relay _ channel _ 7 - switch . relay _ channel _ 8 - switch . garage _ relay _ module _ reboot

This provides you with a simple switch list who’s icon shows you the state of the relay.