In an effort to simplify my Home Assistant install in the future. Here is the list of steps that I go through to install it.
Prerequisites:
- Download the latest copy of Raspbian image here.
- Download and install Etcher to use it for writing the Raspbian image to the removable drive.
Install Raspbian on a USB Flash Drive—
I have had troubles with using a micro SD card to run Home Assistant on. After about 3 weeks it would just look up and I couldn’t even SSH into the pi to see what happened. I tried rebooting and nothing seemed to fix it unless I re-formatted the SD card and started all over. and then a few weeks later I would have the same issue again. I have read that this could be an issue with the all the writing to the SD card and so I am going to try running HA on a Flash Drive instead.
To do that I follow this blog post by LazyManJoeto set up my Flash Drive and SD card. Here is the TLDR of that post:
- Format the Flash Drive with a FAT32 file system.
- Using Etcher, write the Raspbian .img to the Flash Drive.
- Open up the flash drive’s root directory and add a text file that is named “ssh” (this is required to enable ssh by default.
- Format the SD Card and name the partition “boot”.
- Copy all of the contents from the Flash Drvie’s “boot” partition to the SD card.
- Edit the “cmdline.txt” file on the SD card by deleting the text that reads root=/dev/mmcblk0p2 and replace it with root=/dev/sda2.
- Save the file. This tells the Pi to boot from the USB drive instead of the SD card.
- Put both the SD card and the Flash Drive into the pi and apply power.
- To expand the partition of the Flash Drive, preform the following steps:
- SSH into the pi with user “pi” and the default password “raspberry”
- sudo fdisk /dev/sda
- press p to see the partitions.
- note the start position for the sda2 partion. it will be something like 137216.
- press d then 2 then ENTER (this will delete the 2nd partition).
- press n then p then 2 then ENTER (this will start creating the new partition).
- provide the start position that as noted before.
- leave the end position blank to use the entire drive.
- press w to commit the changes.
- you may get an error message about the partition table is busy, but that is ok.
- sudo reboot
- once rebooted, sudo resize2fs /dev/sda2 (*This didn’t fork bot me, but df -h showed it was ok).
- sudo reboot
- confirm the changes by checking the allocated space by df -h
Install Home Assistant —
To install home assistant I like to follow along with Ben’s BRUH Automation post. On that post he walks through setting put the pi, installing HA, Samba, Duck DNS and Let’s Encrypt. You can follow along his post for more detail. I just like the using his post for setting up the pi, installing HA and Samba. I currently don’t have my pi’s port forwarded to the internet and I just use my HA only when I am on my home wifi network.
- passwd to change the pasword for the user pi. You will need to enter the current password “raspberry” and then your new password twice.
- sudo apt-get update
- sudo apt-get upgrade (press Y when prompted)
- sudo raspi-config (no need to expand the file system, so just change the time zone.)
- sudo reboot
- I use the pi 3’s wifi for an access point for my devices and have it connected to my home network via an Ethernet cable. So I skip the section where Ben describes setting up the wlan0.
- Now its time to install HA. Copy the All-In-One (AIO) command found on HA webpage.
- Run the AIO command as described on the webpage. *
- If it fails within the firest few minuets, read a few lines above where it fails and it may tell you a command that needs to run to fix something to allow the install to complete. after running that command, re-run the AIO command.
- Enter the password for the use pi when prompted. (about 15- 20 minuets into the install). The install will take a few hours.
- sudo reboot
Install SAMBA —
SAMBA is how I edit my HA configuration.yaml file from my Windows PC over the network.
- sudo apt-get install SAMBA
- sudo nano /etc/samba/smb.conf
- press and hold control-k to clear initial config, paste the following configuration
-
[global] netbios name = raspberrypi server string = The Pi File Center workgroup = WORKGROUP hosts allow = remote announce = remote browse sync = [HOME ASSISTANT] path = /home/homeassistant/.homeassistant comment = No comment browsable = yes read only = no valid users = writable = yes guest ok = yes public = yes create mask = 0777 directory mask = 0777 force user = root force create mode = 0777 force directory mode = 0777 hosts allow =
- sudo smbpasswd -a pi
- type a new password went prompted
- sudo service smbd restart
Install HostAPD —
HostAPD is the software that sets up an access point using the pi 3’s on-board wifi adapter. I use this to setup a new wireless network for all my home automation sensors to connect to. I like to keeps these separate from my usual home wifi network to keep down on the number of ip addresses and network traffic on my primary wifi.
To setup HostAPD, I like to follow along with a post on the lean.adafruit.com website. I have tried to consolidate these steps here.
- sudo apt-get install hostapd isc-dhcp-server
- sudo apt-get install iptables-persistent
- On the two config screens, select yes to both of them.
- sudo nano /etc/dhcp/dhcpd.conf
- Find the two lines that say
-
option domain-name “example.org”;
option domain-name-servers ns1.example.org, ns2.example.org;
-
- Comment them both out by adding a # in front of them.
-
#option domain-name “example.org”;
#option domain-name-servers ns1.example.org, ns2.example.org;
-
- Now find the lines that read
-
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
-
- And un-comment the last line by deleting the #, so it reads
-
- # If this DHCP server is the official DHCP server for the local
- # network, the authoritative directive should be uncommented.
- authoritative;
-
- Then scroll down to he bottom and add the following lines.
-
subnet 192.168.42.0 netmask 255.255.255.0 {
range 192.168.42.10 192.168.42.50;
option broadcast-address 192.168.42.255;
option routers 192.168.42.1;
default-lease-time 600;
max-lease-time 7200;
option domain-name “local”;
option domain-name-servers 8.8.8.8, 8.8.4.4;
}
-
- Save the file by pressing Control-x then y then Enter
- sudo nano /etc/default/isc-dhcp-server
- Find the test that reads, INTERFACES=”” and change it to INTERFACES=”wlan0″
- Save the file by pressing Control-x then y then Enter
- sudo ifdown wlan0
- sudo nano /etc/network/interfaces
- Keep the line that says allow-hotplug wlan0, but comment out any other lines dealing with the wlan0 (we don’t want the lines that start with iface or have the wpa-supplicant.conf file)
- Add the lines
-
iface wlan0 inet static
address 192.168.42.1
netmask 255.255.255.0
-
- Save the file by pressing Control-x then y then Enter
- sudo ifconfig wlan0 192.168.42.1
- sudo nano /etc/hostapd/hostapd.conf
- Add the following lines. Be sure to change the ssid= and wpa_passphrase= lines to correspond with what you want you network to have.
-
interface=wlan0
#driver=rtl871xdrv #driver=nl80211
ssid=Pi_AP
country_code=US
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=Raspberry
wpa_key_mgmt=WPA-PSK
wpa_pairwise=CCMP
wpa_group_rekey=86400
ieee80211n=1
wme_enabled=1 - The code above is for using the Raspberry pi 3’s on-board wifi. If using a USB wifi adapter then try using one of the drivers that is listed in the second line by un-commenting the line.
-
- Save the file by pressing Control-x then y then Enter
- sudo nano /etc/default/hostapd
- Find the line #DAEMON_CONF=”” and change it to DAEMON_CONF=”/etc/hostapd/hostapd.conf”, (don’t forget to remove the # in front)
- Save the file by pressing Control-x then y then Enter
- sudo nano /etc/init.d/hostapd
- Find the line DAEMON_CONF= and change it to DAEMON_CONF=/etc/hostapd/hostapd.conf
- Save the file by pressing Control-x then y then Enter
- sudo nano /etc/sysctl.conf
- Scroll to the bottom and add net.ipv4.ip_forward=1 on a new line.
- Save the file by pressing Control-x then y then Enter
- sudo sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward“
- sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
- sudo iptables -A FORWARD -i eth0 -o wlan0 -m state –state RELATED,ESTABLISHED -j ACCEPT
- sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
- sudo sh -c “iptables-save > /etc/iptables/rules.v4”
- Time to test it….
- sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf to start it manually
- If you see any errors or warnings, please check the Adafruit tutorial for help.
- Try to connect to the newly created wifi network using the wireless device of your choice.
- Once everything is working, run sudo service hostapd start
- sudo service isc-dhcp-server start
- sudo update-rc.d hostapd enable
- sudo update-rc.d isc-dhcp-server enable
Setup Mosquitto MQTT —
When the All-In-One command is used to install Home Assistant, it also installs Mosquitto to be used as a MQTT broker. It has a base configuration that is good, but you need to setup a username and password for your IoT devices to use to connect. To do this I like to follow along with Ben’s BRUH Automation video in which he describes the process of setting it up.
- sudo nano /etc/mosquitto/mosquitto.conf
- Make sure these lines can be found somewhere in the mosquitto.conf file and that they are not commented out.
-
allow_anonymous false
password_file /etc/mosquitto/pwfile
listener 1883
-
- Save the file by pressing Control-x then y then Enter
- sudo mosquitto_passwd -c /etc/mosquitto/pwfile <the user name you want> (i.e. sudo mosquitto_passwd -c /etc/mosquitto/pwfile username)
- Enter in a new password for that user when prompted.
- sudo reboot
Creating a backup —
Now that you have spent an entire day getting this set up and it seems to be working great. you should create a new image of the Flash Drive so if anything happens you can just re-write this new image to the Flash Drive and ahead of all these step. To do this I use Win32 Disk Imager.
- sudo shutdown -h now
- Remove the Flash Drive and insert it into your computer.
- Open Win32 Disk Imager and select the flash drive.
- Browse to a location on your hard drive and enter in a new file name to save the image as with a .img file extension.
- Click the read button and wait for it to complete.
Now that you have a backup .img, if anything happens to your system, you can recreate it in a matter of minuets instead of hours.