Get Started with the Home Assistant MQTT Integration

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] last time we looked at home assistants websockets API this time let's learn how to use mqtt for home automation home assistant has a rest and a websockets API and also an mqtt integration mqtt is a lightweight protocol designed for Internet of Things specifically optimized for low power low bandwidth devices devices that might occasionally lose connection and then resume a connection after a while the way it works is client devices interact with a centralized message broker clients will publish data to that centralized message broker and other clients that have subscribed to receive notifications about specific topics will then receive those notifications from the centralized message broker so you have your client in your broker and you have clients pushing data to that message broker and also receiving data from that message broker let's get started in our home assistant dashboard the first thing that we'll need is to create a new user or person to be using the mqt integration unless you want to be using this at your root level using your own personal credentials but for safety sake I'm just going to create a new user so under settings go to people manage who can access your home I currently only have myself the bottom will add a person and allow the person to log in and this is optional but I'm going to only let this user log in from the local network where my home assistant uh instance is running and create and confirm so here is the user that I'll be using to access my mqtt integration the next thing that we'll need is to add a mosquito broker or a message broker so let's go to settings and go under add-ons run extra applications next to home assistant so go to the add-on store and we'll look for something that helps us interact with mqtt and that's this message broker called mosquito broker and we'll need to install and then start this add-on this might take a few minutes so now we have our message broker installed and running the next thing that we'll need is to add the mqtt integration so again under settings we're going to look under devices and services you can see Integrations is here and uh home assistant may have discovered the mqtt for you but if it's not if you don't see mqtt here under discovered you can add the integration manually under add add under add integration and we'll configure it and once it's configured it'll show below the configured section here so let's take a look at it and uh home assistant has a way for you to rudimentarily publish and uh publish And subscribe that's that uh pattern of communication where you publish data and listen to a specific topic and you can see to make sure it's working but one more thing is we'll reconfigure mqtt um and we'll want to make sure we update the username and password with that role we created so instead of using it at the root level uh we'll enter that user information here and there's some additional um parameters that you could take a look at if you're familiar with mqtt like um last will in tesate or retain messages in case the client Falls offline you can configure that there otherwise we will save our changes okay once again again we can use home assistant uh interface right here to test Pub sub but let's take a look at how we can do it a little bit more easily in The Next Step I'm logged into Postman and I'm looking at a public workspace called program smart lights there's a couple collections here including the home assistant websocket API that we looked at in the last video this time we'll Fork this collection here containing a single request to our own workspace so that we can edit and make changes okay so the fork created let's go over to that workspace where we forked our collection and load up our request so you can see the details about the connection we're going to be trying to make a connection with home assistant. local um and then there's other information that's very common in mqt a message payload that we'll be sending to a specific topic topic that we can subscribe to to receive notifications about any data published to that topic and also authorization so in the first step we created a new user to interact with our mqtt integration we'll need that here right now these placeholders have not been defined but you can see the variables that we're using are at the scope collection level so let's define our credentials now let's select our collection under variables you'll put in your own information that you have previously created uh in the home assistant dashboard and I'm in a personal workspace now you can see by this uh single person icon um but you'll want to be very careful if you're working in a teamw space or a public workspace make sure to keep any secrets that you want to keep Secret Under the current value only these will not get saved to any instance anywhere it won't get saved to your account and if you log out and then log back in again you won't even have this current value and you'll need to to get it from the home assistant dashboard so I'll just leave it like this but make sure to save the changes and then go back to our request okay so let's send a message that's text Hello to a topic everything slsm slome let's connect connected to the broker the mosquito broker that we um enabled and have running on home assistant and send our message okay so you can see by the up arrow that the postman client I'm using Postman as my client device to send information to my message broker well you can also subscribe remember clients can Pub and sub so let's subscribe under topics to that same topic that we just published to now let's update our message and now we are subscribed to receive messages and we are able to publish messages so let's send and you can see by the up Arrow this is what the client has sent and also received because we are subscribed to receive those notifications okay so you could have done that in the home assistant interface they have a very limited interface to be able to test out to make sure that the mosquito broker is working as expected um but let's take a look at what that might look like with an actual uh smart device so I have saved messages here just prepared and your unless you have the exact same device setup that I have it will not look the same for you so whatever you're choosing to configure and stream mqtt data to and from uh will look slightly different on this particular step so I've just loaded up another saved message this one has the text toggle in all caps as well as a topic here so before we um publish our data let's go to topics and update our subscription we only want to subscribe to this topic that we will be um publishing too and go back to our message and clear these messages so we have a little bit more room and now we can send toggle on and if you are looking at uh my screen you can see that the light behind me is a tasmota switch um is a light connected to a tasmota Smart Switch and I can toggle it on and off all right and this is just a onetime toggle send I could have used a rest API for this or I could have used websockets for this um but again what mqtt is really good for is uh low bandwidth low power devices and those devices might be sending a constant stream of data may maybe we're measuring the temperature in this room and every few seconds that data is going to be coming in if you found this video helpful like And subscribe and let me know what you want to learn next
Info
Channel: Joyce Lin
Views: 9,499
Rating: undefined out of 5
Keywords:
Id: tZjW1IHZ3Lc
Channel Id: undefined
Length: 9min 6sec (546 seconds)
Published: Fri Nov 17 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.