Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / artificial-intelligence

How to Install Home Assistant Container on Windows and Publish an MQTT Message

5.00/5 (5 votes)
1 Feb 2023CPOL17 min read 19.8K  
In this article, I will show you how to install Home Assistant Container on Windows, using Docker and Portainer, get it working with Agent DVR, and send an MQTT message from Agent DVR using CodeProject.AI Server to detect a person.
This article goes through setting up Portainer, and using Portainer to install Home Assistant Container. Using Home Assistant Container, we are able to integrate Agent DVR and an MQTT Broker, and use CodeProject.AI to detect a person and send this as an MQTT message to Home Assistant.

Introduction - The Many Ways of Installing Home Assistant

There are a lot of ways to install Home Assistant on Windows. You could install it using a Virtual Machine (via VirtualBox, VMware, Linux KVM, or Hyper-V), you could install it using Windows Subsystem for Linux (WSL), or you could install it in a Docker container.

When I started the process, I began installing it using VirtualBox, but I was unable to get an IP address for Home Assistant, or connect it to the internet. Some very kind, very patient people tried to help me for several days, but ultimately it became a question of, does my Network Interface Card support Promiscuous Mode? I emailed the manufacturer to find out -- I'm still waiting for a response.

I tried installing Wireshark to force my NIC to use Promiscuous Mode, but that didn't work. I tried using VMware to set up Home Assistant, but same problem; when I created the virtual machine, Home Assistant CLI did not generate an IP address. Unless you're a network administrator, or have a lot of experience with networking, I wouldn't recommend trying to install Home Assistant on Windows using a virtual machine.

This led me to installing Home Assistant Container, using Docker and Portainer. I wouldn't say this is necessarily the ideal setup either. If you're just starting with Home Assistant and you're not a technical person, this won't make sense to you, but Home Assistant Container does not come with Home Assistant Supervisor, which is an integration that allows you to easily install Add-ons. Without Supervisor, you may have to install separate containers and try to figure out how to access them / Home Assistant Container using an SSH command line. I'll admit, I still haven't figured that out yet.

Part of me wants to install successfully install Home Assistant every possible way to show you how to do it, but I don't know if I hate myself that much. I suspect installing Home Assistant using WSL might be the ideal setup, so I might try that next.

But in this article, I will show you how to install Home Assistant Container on Windows, using Docker and Portainer, get it working with Agent DVR, and send an MQTT message from Agent DVR using CodeProject.AI Server to detect a person.

My Setup

In this article, I am using:

The Tools We Use and How They Fit Together

There's a lot of tools used in this article. It may seem a little confusing, but here's the basic layout of how all this works together.

Image 1

  • Docker houses everything we need for Home Assistant. It allows us to create a Portainer container, and through Portainer, create a Home Assistant Container, and creates an Eclipse Mosquitto container.
  • Portainer helps manage and create container systems. Here, we use it to create the Home Assistant container.
  • Home Assistant Container. This is the containerized version of Home Assistant. Everything else we have here in some way is set up to interact with Home Assistant, in some way.
  • Mosquitto Eclipse. This is an MQTT Broker that we wrap in a container so that we can send MQTT messages from Agent DVR to Home Assistant.
  • Agent DVR is the camera security software that gives us a shortcut to using a Wyze Cam with Home Assistant, as well as is what ties in with CodeProject.AI Server in order to do object detection. Agent DVR also allows us to create an action that sends an MQTT message to Home Assistant.
  • CodeProject.AI Server handles the AI detection. Here, we use CodeProject.AI Server to detect a person, and send this detection through Agent DVR as an MQTT message to Home Assistant.

Setting Up Portainer

When I started messing around with installing Home Assistant Container, I incorrectly assumed that everything would have to be installed through Portainer, but this assumption was incorrect. Nevertheless, Portainer is a common tool that gets used to install Home Assistant. If you're installing Home Assistant Container, I'm guessing one possible reason is because you don't want to install everything from a command line. Portainer has a lot of functionality to avoid the command line, and is a great visual interface to look in and out of containers, check the formatting of YAML files, or if you were using Docker Compose, copy the contents into the Stack page. So let's get started.

Go to the Start button on Windows and starting typing "PowerShell". Click on Windows PowerShell. Input the following:

BAT
docker volume create portainer_data

This creates a volume for Portainer which will be used to store its database. Next, run:

BAT
docker run -d -p 9443:9443 --name portainer 
--restart=always -v /var/run/docker.sock:/var/run/docker.sock 
-v portainer_data:/data portainer/portainer-ce:latest

This installs Portainer on Windows Subsystem for Linux (WSL).

Image 2

Now, place https://localhost:9443/ in your browser. You can also (and may have to) use https://youripaddress:9443. If you do not know your IP address, go to the Start button in Windows and start to type "CMD" then select Command Prompt. Then, type ipconfig. The address you want is the IPv4 Address.

Image 3

A scary warning. I'm going to say, "don't worry about it." If you really want to, you can install an SSL certificate later, but I don't cover that here. Click Go Back, then Advanced..., then Accept the Risk and Continue.

Image 4

Now it's time to create a Username and password for Portainer. I'm leaving the Username as admin. Input a password that's at least twelve characters long, and make sure you save it somewhere. Click Create user.

Image 5

This actually made me start and stop the service again, and re-enter the password, but after that, I got to the Portainer.io home page.

Image 6

Click Get Started.

Image 7

Then click local to get into our Portainer container.

Setting Up Home Assistant Container

Now it's time to use Portainer to install Home Assistant Container.

Click on the Volumes tab on the left. Then click + Add Volume.

Image 8

This is similar to what we just did in Windows PowerShell. In the Name field, input something like "homeassistant_data" and hit Create the volume.

Image 9

Next, click on the Containers tab on the left-hand panel, which takes you to the Containers page. Here the Portainer container is happily running. Click + Add Container.

Image 10

Give the container a name in the Name field. I think "homeassistant" is a pretty good name. Then, in the Image field, put "homeassistant/home-assistant:latest". Next, click + publish a new network port and put in "8123" to both the host and container field.

Image 11

There's actually a pretty handy search button next to the Image field that allows you to search through Docker Hub for all available images, extensions, and plugins. When you find the image you're looking for, the heading in bold is what you put into the Image field in Portainer.

Image 12

Now it's time to enter the Advanced container settings. First, click on the Volumes heading, then + map additional volume. In the container field, put /config, and in the volume dropdown, select the homeassistant_data volume we just created.

Image 13

Then hit the Env tab and click + Add an environment variable. Put "TZ" into the name field, and then your Country/Timezone. I tried putting in Canada/Edmonton, but it was saved as "America/Edmonton" so I'll just put that in the value field.

Image 14

The next tab we need is the Restart policy tab. Simply click Always here.

Image 15

Finally, click the Runtime and Resources tab and toggle Privileged mode into the on position. Now scroll up and hit Deploy the container.

Image 16

If you input everything correctly, there is a little pop-up that says "Success Container successfully created" and Home Assistant Container now runs and is visible in the Container list.

You'll probably notice how similar this is like a visual version of a docker command to pull Home Assistant. If you were to run the docker command to do this, it would look something like this:

BAT
docker run -d --name homeassistant --privileged 
--restart=unless-stopped -e TZ=MY_TIME_ZONE 
-v /PATH_TO_YOUR_CONFIG:/config -p 8123:8123 home-assistant/home-assistant:stable

Where MY_TIME_ZONE is your time zone (America/Edmonton for me) and PATH_TO_YOUR_CONFIG is the path to an empty folder you create on your computer. But you can see how each element in this line matches up with what we just did in Portainer: the name of the container, privileged mode, restart, host and container ports, and image. You could skip setting up Portainer and install it this way, it's simply a different process.

Image 17

All right, let's log into Home Assistant. In the browser, put http://localhost:8123. This was the screen that eluded me for days when trying to install Home Assistant from a virtual machine, so when I saw this screen, I got a little excited.

Image 18

Input your Name (which automatically starts entering that as your Username in lower case), then choose a password. Make sure you write this password down somewhere, you're going to need it a lot. Hit Create Account.

Image 19

Next input more specific details for Home Assistant. You can give Home Assistant a name (I'm very creative and call mine "Home"), then select a Country, Language, Time Zone, and Currency. Hit Next.

Image 20

Then is a screen about sharing data. I'm not interested in sharing so I click Next.

Image 21

This next page is where all your IoT devices would show up if have them. Nothing shows up for me. Click Finish.

Image 22

This is it. This is the Home Assistant Container Overview page. Isn't it beautiful? Let's add our Wyze cam and Agent DVR. First, launch Agent DVR. Go to your browser and put in http://localhost:8090/.

Image 23

Back in Home Assistant, click the Settings tab on the left. Then click Devices & Services.

Image 24

From here, hit + Add Integration.

Image 25

Start typing "Agent" and then select Agent DVR.

Image 26

Here, for the Host, you need to put in your IP address. If you don't know it, go to the Start button in Windows and start to type "CMD" then select Command Prompt. Then, type ipconfig. The address you want is the IPv4 Address. Leave the Port as 8090 and hit Submit.

Image 27

When you integrate Agent DVR to Home Assistant, it comes with two entities (depending on your camera setup). The first is an Alarm Panel. I presume you could use this to arm an alarm system if it were integrated with Agent DVR. For me, it's just a camera in my office, so I click the dropdown and select Add new area, input "Office" and hit Add. I select Office for both the Alarm Panel and the camera (I call my camera Eye Spy), and hit Finish.

Image 28

Now if you go back to Overview, boop! There I am.

Setting Up an MQTT Broker - Eclipse Mosquitto

Now let's set up our MQTT Broker. I actually tried to set up Mosquitto using Portainer, but when I tried to do this, I couldn't get Mosquitto set up in Home Assistant Container. I got an error: "Address not available".

There are a few ways to address this error. If you did install Mosquitto with Portainer, from Portainer you could command line into Mosquitto and alter the mosquitto.conf file to change the listener to 1883, persistance set to true, and allow_anonymous set to true. But I didn't go that route. Instead, I installed Mosquitto using a Docker command that used the no authentication configuration.

Image 29

Open Windows PowerShell, and type:

BAT
docker run -it --name mosquitto 
-p 1883:1883 eclipse-mosquitto:2.0.15 mosquitto -c /mosquitto-no-auth.conf

It's important to note that at the time of reading this article, Mosquitto is on version 2.0.15. That's why the line reads eclipse-mosquitto:2.0.15, but simply replace 2.0.15 with whatever version of Mosquiotto you desire (eclipse-mosquitto:<version>).

Image 30

Because we did not install Mosquitto through Portainer, the most important thing is to make sure that this mosquitto container is on the same network as Portainer and Home Assistant. To check, go back to Portainer and go to the Containers heading on the left. Then click on mosquitto in the Name field, then scroll down to Connected Networks. Underneath Network, you can see it says "bridge". If we go back to the Containers tab and click on homeassistant, then scroll down to the Connected Networks section, we can see that Home Assistant is also on the bridge network.

This process may be helpful in case you need to install other containers for your own use.

Setting Up MQTT in Home Assistant Container

Image 31

Now it's time to go back to Home Assistant. Go back to Settings -> Devices & Services and click + Add Integration. Then start typing MQTT, and click MQTT.

Image 32

A dialog appears asking which MQTT integration you would like to add. Click just plain MQTT.

Image 33

In the Broker field, input your IP Address. If you do not know your IP address, go to the Start button in Windows and start to type "CMD" then select Command Prompt. Then, type ipconfig. The address you want is the IPv4 Address. Leave the port as 1883, then for Username put your Home Assistant user name, which for me is "seancp", and then the password for Home Assistant. Then hit Submit. A little Success window pops up, then hit Finish.

Setting Up Agent DVR to Send an MQTT Notification to Home Assistant

Now let's do a practical application of MQTT. Agent DVR has great integration with Home Assistant, so launch Agent DVR by putting http://localhost:8090/ in your browser.

Image 34

In order to use Agent DVR for an MQTT message, you must have a business license. Head to the subscriptions page for Agent DVR and choose the desired license. For this, I only need the Basic plan. Click Choose this. This brings you to an Agent DVR tab. If you already have an account, click Login, if not click New Account.

Then navigate through the payment method (like PayPal) and there you go! You're set up to send MQTT messages.

Image 35

The first step is to set up MQTT in the Server Menu. Click the Server Menu button in the upper-left corner.

Image 36

Then from the Configuration column, select Settings.

Image 37

From the General dropdown, select MQTT. Here, we enter in all the settings for MQTT and make sure it is Enabled. Under Server, you want to put in your IP Address again. If you do not know your IP address, go to the Start button in Windows and start to type "CMD" then select Command Prompt. Then, type ipconfig. The address you want is the IPv4 Address. In the Port field, you want to put 1883. Then in the Username and Password fields, you again want to put in your Home Assistant username and password. Hit OK to accept the MQTT settings, then OK again to exit the Server Settings.

We're going to be detecting a person, so let's make sure Agent DVR is set up to detect a person.

Image 38

Go to the Camera Settings.

Image 39

Then from the General dropdown, select Alerts. And make sure Alerts are enabled.

Image 40

Now go back to the Camera settings, and from the General dropdown, and select Detector. I want it to be really easy to detect a person, so change the Detector to Simple. The Simple detector means that the AI detection will be triggered on motion. Hit OK.

Image 41

Now go back to the Camera settings, and from the General dropdown, and select Object Recognition. Make sure it is Enabled, that the AI Server is set to http://localhost:32168/. Then hit the ... button to get to the Configure settings.

Image 42

Make sure in the Find field it says "person" and set the Confidence to 50. Hit OK to accept the Configure settings, and OK again to accept the Object Recognition Settings.

Image 43

Now go back to the Camera settings, and from the General dropdown, and select Actions. We actually want to create two actions, one action so we can confirm that within Agent DVR, a person is being detected, and the other to send the MQTT notification to Home Assistant. Hit Add.

Image 44

In the If dropdown, select Object Found. In the Then dropdown, select Trigger Object Recognition, which should automatically select your Device, which for my is my Wyze Cam v3 that I've named "Eye Spy". Hit OK to create the action.

Image 45

Now for the action, we set this whole thing up to create. Hit Add to create another action. In the If dropdown, select Object Found. In the Then dropdown, select MQTT. This next bit is interesting and important. In the Topic field, you need to input a Topic and a Sub-topic. My Topic is "Agent" and my sub-topic is "alerts". You can name these whatever you want, but make sure to remember what these are. Input them as "Topic/Sub-topic". So in my case, "Agent/alerts". In the Post field, I want the MQTT message to contain what was detected, so I put "{AI}". Hit OK to create the action. Hit OK again to accept the action settings.

Setting Up Home Assistant Container to Receive MQTT Messages

Now let's see if we can use CodeProject.AI Server through Agent DVR to detect a person, and send an MQTT message from Agent DVR to Home Assistant.

Go back to Home Assistant by putting http://localhost:8123 in your browser and enter your username and password. Then go the Settings panel from the left-hand side, then click Devices & Services.

Image 46

From here, we can see our MQTT integration. Click Configure.

Image 47

This is where we need the Topic and Sub-topic from Agent DVR. In the Topic field, put your Topic/Sub-topic. For me, this is "Agent/alerts". In the Payload setting, you can put in a message here to test to see if your MQTT Broker can share a packet. If you hit Publish, it will do a test and display "alert!" I'm deliberately leaving this as "alert!" to demonstrate that when the MQTT message comes in, it's coming from Agent DVR. Now go down to Listen to a topic and put "Agent/alerts" in the Topic to subscribe to field.

Image 48

Because we just configured this MQTT settings, we need to reload it in Home Assistant. Click on the Developer Tools, and in the YAML tab click Manually Configured MQTT Entities. A green button flashes, indicating that MQTT Entities have been reloaded.

Image 49

Now launch CodeProject.AI Server by putting http://localhost:32168/ in your browser. I've previously installed a bunch of modules like Face Processing, and License Plate Reader, but for this, all we really need is Object Detection (YOLOv5 6.2), so I click the ... next to all the other services, and select Stop.

Image 50

Now let's go to Agent DVR and see if we can get a notification of a person detected. Look at that! In the tag in the top right-hand corner, it says "person". That's me!

Image 51

Finally, it's time to go to Home Assistant to see if our MQTT actions are firing. Go back to the MQTT settings page from within the Settings -> Devices & Services menu and click Configure on the MQTT integration. Hit Start Listening.

Image 52

As you can see, MQTT is spitting out the AI Object detected, which is "person". There is a stream of messages that start coming through, because if you go to the CodeProject.AI Server Server logs

Image 53

You can see that an object is being detected every 512 ms or less.

And that's it! In this article, we've gone through how to set up Portainer, and use Portainer to install Home Assistant Container. Using Home Assistant Container, we were able to integrate Agent DVR and an MQTT Broker, and use CodeProject.AI to detect a person and send this as an MQTT message to Home Assistant. It's not a very pretty result, but it is a proof of concept.

There are ways to use Home Assistant Container without the Home Assistant Supervisor and all its add-ons, it just requires more work. If for whatever reason you 'had to' install Home Assistant Container using Windows, this is how you would do it. The ideal is to install Home Assistant OS on a dedicated Linux device, or a Raspberry Pi. We'll demonstrate that later.

Thanks for reading and if you have any questions, please feel free to ask them in the article forum below.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)