Installing Home Assistant in KVM Virtual Machine
Thursday, September 28, 2023
Reading time 5 minutes
One of the things I’ve had pending for many years, although I had tried sometime to get to grips with the most important part of the topic, is configuring and learning to use Home Assistant. Thanks to this powerful software tool, you can configure a smart home system, as the software can integrate and integrate with many other hardware devices and already existing home automation ecosystems. In addition to being able to control many devices and services from its web interface, such as devices with Alexa, temperature control, universal remote controls, televisions, smart plugs, etc.; we can also create automations (to perform a task whenever a condition is met), scripts (to execute code that can call a service or get something from an API), templates (to take values from sensors and be able to perform calculations on them), and many other things.
In this article I’ll only detail what I’ve been learning about the installation and some initial configurations of this tool. This way, if I have to go through this process again, I’ll already have something I can turn to to avoid forgetting everything that might be necessary to have a functional system.
Which variant to install?
Home Assistant has several types of installation. Not all have the same functionalities, so it’s important to keep this in mind before even installing the tool. There are also a couple of devices that can be acquired and come with the operating system with Home Assistant installed, by the way.
- Home Assistant Operating System: This is a complete system, solely dedicated to maintaining Home Assistant. It’s configured with supervisor as process manager and docker. It gives access to the Home Assistant add-on store, which allows installing extra applications that can integrate within the web interface. This option is the most recommended because it offers greater possibilities. It can be installed on a dedicated device, or imported into a virtual machine.
- Home Assistant Container: This is a container, like docker, prepared to run the Home Assistant software. Since this is already a container, the add-on store is not available and some integrations may not work, especially if they require installing additional things besides Python code to maintain their operation.
- Home Assistant supervised: Provides instructions to install Home Assistant on an operating system manually. It’s essentially almost identical to using the Home Assistant operating system, but the user must take care of updates.
- Home Assistant core: Instructions to install Home Assistant through a Python virtual environment. This option is perhaps the one that requires the most time and offers the least functionalities for the end user. Containers cannot be used, so the add-on store stops being available, processes are not managed with supervisor and it’s not possible to restore a backup.
Having seen this, the option I decided on, in light of my recent tests with Libvirt. Is to import the disk image of the Home Assistant operating system, which is available on the Installation page for Linux.
Installing Home Assistant
- The first thing is to download the disk image in qcow2 format, for which we can use a command similar to this, depending on what the most recent version is at the time:
$ wget https://github.com/home-assistant/operating-system/releases/download/10.5/haos_ova-10.5.qcow2.xz
- Once the image is downloaded, it must be decompressed. For this we need the xz-utils package, and this command:
$ xz -d -v haos_ova-10.5.qcow2.xz
- Then you have to move the image to a directory where the libvirt user can write it. The default directory will be sufficient:
$ sudo mv haos_ova-10.5.qcow2 /var/lib/libvirt/images
- Finally, you can now import the virtual machine. According to the required specifications, with 2 CPU and 2 GB of RAM the software can work well, although in my case I’ve decided to double both parameters. It’s important that Home Assistant has access to the home LAN network where it’s being installed, so it can see and control other devices, for which we’ll use the “bridged-network” network, which we created in the previous article and is connected to the virtual network bridge “br0”:
$ sudo virt-install --import --name hassos --memory 4096 --vcpus 4 --cpu host --disk haos_ova-10.5.qcow2,format=qcow2,bus=virtio --network network=bridged-network,model=virtio --osinfo detect=on,require=off --graphics none --noautoconsole --boot uefi
- After a few minutes, you’ll be able to see the device called “Homeassistant” in the list of connected devices on the router. Because we’re not using a NAT configuration, libvirt cannot know the IP address that has been assigned by the router to the VM. One way to make this configuration somewhat easier is by having the router’s DHCP server always assign a static IP to the VM’s MAC address. To get the VM’s MAC address, you can execute this command:
$ sudo virsh dumpxml hassos | grep "mac address" | awk -F\' '{ print $2}'
Conclusion
Once installed, it’s just a matter of accessing the IP that has been assigned to the Home Assistant instance through port 8123 to start configuring it. As a note related to accessibility, during the initial configuration, the software asks you to indicate the location where the home is. This address is used for things like presence detection and weather conditions. You can indicate coordinates (latitude and longitude) as well as use a system that detects the approximate location of a public IP. I’ve found it much easier to search for the coordinates in a service like Google Maps and place them manually, as maps in general are not accessible, and don’t offer the possibility to enter an address. It should also be said that if the location is not set correctly, changing it later involves editing a yaml file, which may be less desirable.
After having performed the installation and initial configuration, Home Assistant is supposed to be able to show the integrations activated by default (sunrise/sunset and weather) in Lovelace, the software interface. This interface is a dashboard that can be modified to the user’s liking in interesting ways. We can also discover, in the settings menu, if there are devices that the tool has been able to find via wifi, for example, and proceed to configure them. From here you can do several things:
- Configure the desired integrations, from the settings>devices and services menu.
- Buy “Home Assistant cloud”, which will allow the tool to be accessible from outside the home network, as well as integrate with Alexa and Google Assistant, access to high-quality TTS voices, etc.
- Install add-ons, from settings>add-ons.
Linux Administration NAS tutorials