Defining & Customizing a Shelly Switch via MQTT in Home Assistant

Shelly switches are a fantastic way to make mundane or legacy device smart. The are very versatile and powerful, but there is a learning curve — especially if you’re dealing with mains wiring. But if you can get safely past that — you can do amazing things with these affordable devices.
Remember: If you’re not confident in wiring up a system, choose a simpler and safer method; or get help from a professional
Getting Started
For this article, I will be using and referring to a Shelly 1. The process is similar for some other Shelly devices; so check the Shelly API Reference Site for the particulars for other devices. I also expect that you’ve already set up your MQTT broker, and enabled your Shelly device to use MQTT. If not — please check out this guide.
I have some simple LED downlights in my kitchen, which are connected up to a LED driver and give off light (meaning, work fine). But the challenge is they are not smart; I’d like to be able to switch them on when I’m walking near the kitchen at night, or switch them off when I leave the house.
By defining them in Home Assistant, and connecting them via an MQTT Broker (e.g. Mosquitto), we have the ability to switch them on directly, or via an automation. Let’s make it work.
MQTT is an open OASIS and ISO standard lightweight, publish-subscribe network protocol that transports messages between devices. It is designed for connections with remote locations where a “small code footprint” is required or the network bandwidth is limited.
- Wikipedia
Making It Work
For implementation, we need to define the Shelly as a switch in the configuration.yaml file, and define the topics — which is always a key part of working with MQTT. Before you start you’ll need to find and record the ID of your Shelly device. You can find your Shelly ID on the device, via the local webserver under Settings → Device Info, or via the Settings Webpage, accessed directly via http://192.168.86.XXX/settings.
Once you have that, use the MQTT format to outline the switch name, the Topics, Retain and QOS settings. You can’t customize the entity here, but you can do that in customize.yaml
following the creation step.
For switches, remember to set RETAIN =
false
, otherwise you will get phantom/erroneous switching via MQTT. Also, QOS =2
is highly recommended.
Here is what this looks like in YAML:
Finally, we can also give the switch a meaningful Icon in customize.yaml
(code above), and then restart Home Assistant for the switch to be imported. Now your Shelly switch is ready to go, and controllable via MQTT in Home Assistant.
