MQTT and Home Assistant

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
you may have heard the term mqtt come up when people start talking about their home automation setups usually you hear it alongside things like zigbee or zed wave and home assistant well have you ever wondered what exactly mqtt is or how it works do you want to know how to set it up in home assistant you've come to the right place because in this video i'm going to take you through exactly what mqtt is and why i think it's cool and then i'm going to show you how you can set it up in home assistant let's take a look hey home automation guy start the show mqtt was invented by a bunch of smart nerds working for ibm in 1999 and it was created because big manufacturing and energy companies needed a way to monitor all sorts of sensors and industrial components from across the world these were things like elevator sensors oil pumps centrifuges and temperature sensors that lived inside often remote infrastructures like oil rigs or nuclear power stations these remote components became the internet of things and a system needed to be created that would allow these sensors to send back telemetry information back to a control center so that engineers could make sure that everything was running smoothly and nothing was going to imminently explode these remote sensors often ran on battery powered and had to communicate over really slow gsm or satellite networks so this system needed to use really low amounts of power and work over low bandwidth and thus the mqtt protocol was born or the message queuing telemetry transport protocol as it used to be known it provides a really lightweight way for iot devices and sensors known as mqtt clients to send data to a central location called an mqtt broker from there any number of systems or applications can subscribe to the mqtt broker and they will get told when a new mqttd message is waiting for them this is what makes it really popular in smart homes too mqtt is great at gathering information from low power and low band with home automation sensors and switches the home automation platform that i use called home assistant works really well with mqtt and a specific broker called mosquito these are battery-powered zigbee temperature and motion sensors and they can work for up to two years using a single low-power cr2032 battery these sensors simply sit around inside my house and wait for the temperature to change or for motion to be detected they then send a small message to my zigbee usb coordinator dongle which passes that message into a specific mqtt topic that home assistant is listening to once home assistant has this new information from my temperature sensor it updates any dashboards that i have other home automation systems like my frigate security camera software also uses mqtt to tell home assistant if it has detected any people on my camera footage it then sends this message directly to the mqtt broker over ip so there's no need for any middleware mqtt can also be used to change the state of any home automation devices for example if i want an automation to turn on a light home assistant can publish an mqtt message to a particular topic asking zigbee to turn on a smart bulb that was a crash course in what mqtt is and how it works i'll now show you how you can set up an mqtt broker called mosquito and use it with home assistant there are two parts to this setup firstly you need to install and configure the mqtt broker itself then secondly once that's set up and running you'll need to add the mqtt home assistant integration which it uses to talk to the broker over ip on tcp port 1883. you could use an mqtt service running in the cloud if you wanted to there are lots of those available which work well with home assistant but the best solution is to install your own broker locally so that your data remains private inside your own network and that your lights and sensors keep working as normal if your internet connection goes down running a local mqtt broker will also make sure that things work much faster and your lights are going to turn on instantly if you're using the home assistant operating system then you can really easily add the mosquito add-on via the add-on store you just need to go to the configuration section find the add-ons area go to the add-on store and search for mosquito then click the install button once it's installed i'd recommend turning on auto update and then you can click the start button and your mqtt broker will be up and running just like that there's a configuration tab at the top which you can use to change any advanced settings but in my experience it all works just fine with the default settings we can now move on to step two which is installing the home assistant mqtt integration via the integration section you'll probably see that home assistant has already automatically discovered your new mqtt broker and you can click the configure button to get it set up this should immediately tell you that home assistant knows that you're using the add-on and ask you if you want to go ahead with the default settings and voila you now have the mosquito mqtt broker set setup and you have home assistant configured to talk to it the add-on makes this so freaking easy and it's one of the main reasons i always recommend that people install a home assistant operating system when possible but if you're a massive nerd like me who has decided to run home assistant container on top of docker then you'll need to work a little bit harder to get mosquito and home assistant integrated into your smart home don't worry once it's properly set up and installed you probably won't need to touch it ever again i installed mine two years ago and it's been rock solid ever since i'm going to show you how to install mqtt using a docker container via docker compose these steps are pretty technical and you need to make sure you type all the commands in correctly or it won't work this video will be a high level overview of the process and i might go through some of the screens pretty quickly to make it easier for you i've written up a companion blog post which i've linked in the description below that takes you through all the steps in way more detail i've also included all the code samples that i use so you can copy and paste them into your own configuration files this tutorial also assumes that you're running docker and docker compose on some sort of linux system like debian or ubuntu and that you have ssh access to it i'm going to get started by using vs code to add an entry to my docker compose yaml file for my mosquito container make sure that you don't mess up your indenting i'm using the eclipse mosquito image and i've simply called my container mosquito be aware that mosquito has two t's at the end of it i've been tripped up by this so many times wondering why things aren't working and then eventually realizing that i've misspelled it it's annoying back to my docker compose file you can see that i'm also mapping my slash opt mosquito directory through from my linux operating system to the slash mosquito directory in my container this slashop mosquito directory will be where all my mosquito config files will be stored this will allow them to be backed up by my container backup system i'm also mapping through ports 1883 and 9001 from the host os to the container and then i'm going to save the docker compose yaml file before we start the container i want to create the folder structure and a few configuration files as mosquito won't work how i want it to unless these exist i'm using vs code to create a new folder in the slash op directory called mosquito and then another folder inside that called config i'm now going to create a new file inside this config directory called mosquito.conf you can see that vs code has opened that file up for me enabling me to copy and paste in my configuration the persistence part of this config file tells mosquito to store its messages on disk rather than just in memory and to store them in the slash data folder i also tell it to log information to the log file location that i've specified here you can run your mosquito broker with no authentication but this means that any system on your network will be able to publish and read messages that your devices are sending we don't want that so we'll be setting up a username and password that will be needed to use the broker save this mosquito.com file and then we'll open up a new ssh session to the linux server you can see that we're already in the slash op directory where our docker compose.yaml file lives and so we're going to run the docker compose up hyphen d command to download the mosquito images and start up the container if we switch over to home assistant and open up portena you'll be able to see the new mosquito container running here a quick check of the logs shows no error messages which is all good we'll now use portena to open up a console or shell on the mosquito container itself if you try to use the default bash console you'll see it doesn't work you need to use the sh console which you can select using this drop down here this opens up a terminal into the mosquito container kind of like sshing directly into that container and you can use this to run any mosquito commands on the command line we'll be doing this to set up a new username and password which will be used to access the broker here we're going to run the mosquito password command with the hyphen c switch which creates a new mqtt user it will create the user in this path which matches the one we put into our mosquito con file earlier the username for the account we're creating is called hass when we press enter it's going to ask us what password we want to set for this user and give us the opportunity to confirm it in the background this will create a new user and password in this file and we can exit on out of here going back to vs code you should now see a new file inside the mosquito config directory called password.txt this is the file that was created by the command we just ran and just stores the has username and an encrypted version of the password you typed in let's switch back to the mosquito.com file and uncomment out the authentication lines and then save the file looks like there's been a permissions error when we tried to save that file this is because the mosquito server has messed something up and will need to reset the permissions on that file now look i'm definitely not a linux expert and i'm not really sure why this happens or the best way to fix it i'm sure what i'm about to show you is not the best way to deal with this and definitely not the most secure but it worked for me and i'm not really too worried about these file permissions to fix this i run the chon command like this this gives my allen username ownership of all the files and folders inside the slash op directory saving now works properly and i can once again uncomment out those lines and re-save the file as we've made changes to the configuration file we'll need to switch back to home assistant and to the portainer screen and then restart the mosquito container so that it picks up these changes a quick check of the log file shows us that there are no error messages you've now finished part one of the setup which is installing and configuring the mosquito mqtt broker as a docker container you now need to move on to step 2 which is installing the mqtt home assistant integration and telling it how to talk to the broker to do this go to the integration section of home assistant and add a new integration search for the mqtt integration and type in the ip address of your linux server as the broker address leave the port as 1883 and then enter the has username and password you created earlier if everything worked as expected you should see the success message and the integration installed just like it was with the add-on installation method before you now have an mqtt broker running alongside home assistant which you can use to pull in messages from all different kinds of smart home services in the next video i'm going to show you how to install zigbee to mqtt alongside mosquito and home assistant which will give you the ultimate zigbee setup i talk about zigbee in more detail in a video that i've linked here if you want to know how to get home assistant and docker set up like i have then check out this playlist here as always make sure you're subscribed to the channel so that together we can make your home smarter
Info
Channel: Home Automation Guy
Views: 108,099
Rating: undefined out of 5
Keywords:
Id: cZV2OOXLtEI
Channel Id: undefined
Length: 11min 38sec (698 seconds)
Published: Wed Apr 20 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.