A Step-by-Step Guide For Creating a Simple MQTT Switch in Home Assistant
If you're just diving into the exciting world of home automation with the help of Home Assistant, you're in the right place! Or maybe you've been around the field for a while, but you keep getting stumped on one specific task - creating a simple switch for your MQTT devices.
Don't worry a bit! Here, I'll provide a clear and straightforward explanation to make your experience a lot smoother the next time around. So, this post is as much for you as it is for me because, like many others, I keep trying and failing to remember these steps.
In this revamped guide, we'll cover three critical areas:
- How to define an MQTT device as a sensor in Home Assistant (optional).
- How to define an MQTT device as a switch in Home Assistant.
- Finally, how to create a simple on/off switch to toggle a state in your MQTT device.
But before we dive in, let's talk about a couple of prerequisites:
- You should be familiar with editing the configuration.yaml file.
- Make sure you have the "Mosquitto broker" add-on installed in your Home Assistant instance.
Alright, let's dive right in!
Laying the Groundwork: Defining an MQTT Device as a Sensor in Home Assistant (Optional)
Bear in mind that this step is totally optional but could lead to exciting future possibilities. Defining the MQTT devices as a sensor might not have any direct connection to creating a simple switch, but it offers the flexibility of creating triggers based on your MQTT device's state in future. If it doesn't interest you currently, feel free to skip to the next step.
For the adventurous souls looking to make their MQTT device a sensor in Home Assistant, the first step is defining it in the configuration.yaml file. Here is what a basic example might look like:
sensor:
- platform: mqtt # This informs Home Assistant it's an MQTT device
name: "LED Switch 1" # Give your device an easy-to-remember name
state_topic: "home/office/led/get" # The path to read the current state
Once you've added this sensor info, you'll be able to access your MQTT device state as "sensor.led_switch_1" or whatever name you specified (think "sensor.your_awesome_custom_name"). You'll be able to use this custom entity as a trigger for future automations.
The Main Event: Defining an MQTT Device as a Switch in Home Assistant
This step is where the magic happens! To define your MQTT device as a switch in Home Assistant, we're going to wander back into the configuration.yaml file. Add in the following:
switch:
- platform: mqtt # Ding, Ding, Ding! Another MQTT device
name: "LED Switch 1" # Make it something you'll recognize immediately
state_topic: "home/office/led/get" # The topic to read the current state
command_topic: "home/office/led/set" # This will publish your commands
qos: 1 # Quality of Service level
payload_on: 1 # May be "on", it depends on your MQTT device
payload_off: 0 # Or could be "off", again, MQTT dependent
retain: true # Decide if you want to hold onto earlier messages
The qos
or Quality of Service field can sound intimidating at first but it's very straightforward. Here's what it means:
- 0: Multiple messages can be sent and the connection is super stable (preferable in a scenario with a lot of data and a stable connection)
- 1: An assurance that a message gets sent multiple times to ensure the MQTT device received the message
- 2: A handshake process is initiated to ensure the message is received, but it can be sent only once
After you add this information to your configuration file make sure to restart your Home Assistant instance!
The Final Touch: Creating a Simple On/Off Switch to Toggle a State in Your MQTT Device
Woohoo! You're nearly there. Now, we'll create a visual element on your dashboard to control your MQTT device.
Choose the appropriate card, "Entities" for a simple on/off toggle and "Button" for a visual display of the device state. From then, it's a piece of cake to create a visual element for your MQTT device and control its state with a simple button press. You did it!
I hope this revamped step-by-step guide has been an exciting and useful journey! I know it took me hours of trial and error and hiccups along the way to finally get a grip on this process. Here's to saving ourselves precious time in the future and getting back to creating more amazing automations!
Posted on: Feb 9, 2022 Last updated at: Jan 7, 2024