NederlandsKlik deze knop voor de Nederlandstalige website

Home Assistant, Node-RED & MQTT on Linux Mint

Home Assistant (HA) is a free open source home automation software. You can integrate it with countless devices and services. We install it together with Node-RED and Mosquitto MQTT broker on Linux Mint. For this tutorial, I used Linux Mint 19.2 “Tina”.

Manual installation versus HASS.IO

You can install Home Assistant on a Raspberry Pi using HASS.IO, a ready-made image. HASS.IO also has an installer for all sorts of Add-ons. However, on Linux Mint you have to install everything manually. Below is a tutorial to help you on your way. After completing these steps you have a basic installation with Home Assistant, Node-RED and Mosquitto MQTT broker.

Install Home Assistant

To install Home Assistant on Linux Mint you can for the most part follow the instructions in “Manual installation on a Raspberry Pi“.
Perform an update of the installed packages:
sudo apt-get update
sudo apt-get upgrade -y
Install new packages:
sudo apt-get install python3 python3-dev python3-venv python3-pip libffi-dev libssl-dev
Add user ‘homeassistant’ to the ‘diaout’ group. We do not include the “i2c” and “gpio” groups.
sudo useradd -rm homeassistant -G dialout
Create the installation directory:
cd /srv
sudo mkdir homeassistant
sudo chown homeassistant:homeassistant homeassistant
Create a “virtual environment” for user “homeassistant”
sudo -u homeassistant -H -s
cd /srv/homeassistant
python3 -m venv .
source bin/activate
Install Python wheel:
python3 -m pip install wheel
Install HA:
pip3 install homeassistant
Start HA:
hass

Home Assistant autostart

To have HA start automatically, we broadly follow the instructions under “Python virtual environment” in the article “Autostart using systemd” on the HA website.

Use the command below to open the text editor Nano and create the service file for systemd:

sudo nano -w /etc/systemd/system/[email protected]

Paste the code below into the file:

[Unit]
Description=Home Assistant
After=network-online.target

[Service]
Type=simple
User=%i
ExecStart=/srv/homeassistant/bin/hass -c "/home/%i/.homeassistant"

[Install]
WantedBy=multi-user.target

When you’re done, press CTRL-X and then Y to save the file and close Nano.

Install Mosquitto MQTT broker

As an MQTT broker we will install Mosquitto, which is easy with the following command:

sudo apt install mosquitto

To test whether Mosquitto works properly, we can follow the instructions in the “MQTT Testing” article. Go to “Developer Tools” in HA and open the “MQTT” tab.

For example, you can enter the following in the “Topic” field:

home-assistant/switch/1/power

You can then enter a message for the topic in the “Payload” field, for example:

ON

In the “Listen to a topic” field, type a # to see everything:

#

Press “Start Listening” and then press “Publish”. The result should be like the following text:

Message 23 received on home-assistant/switch/1/power/stat/POWER at 12:16 PM:
ON
QoS: 0 - Retain: false
Message 22 received on home-assistant/switch/1/power/stat/RESULT at 12:16 PM:
{
"POWER": "ON"
}
QoS: 0 - Retain: false

Install Node-RED

First install Node.js and npm, for example according to the instructions on this page:
sudo apt-get install curl python-software-properties
curl -sL https://deb.nodesource.com/setup_12.x | sudo bash -
sudo apt-get install nodejs
sudo apt-get install npm

Test Node.js
node -v

Test npm:
npm -v

Then install Node-RED according to the instructions at https://github.com/node-red/node-red:

sudo npm install -g --unsafe-perm node-red

Start Node-RED:

node-red

Open http://localhost:1880

Install Home Assistant nodes in Node-RED

To let Node-Red work together with HA, you can integrate the HA nodes into Node-RED. To do this, install the HA npm package “node-red-contrib-home-assistant-websocket“.

In Node-RED, open the menu (click on the icon with the three horizontal lines), then:

  1. Choose the menu option “Manage palette”
  2. Select the “Install” tab
  3. Search for “node-red-contrib-home-assistant-websocket”
  4. Click on “Install”
  5. When the installation is finished you will see the message “Nodes added to palette”

In the list on the left side of the screen, you will see new nodes below “home assistant”.

Home Assistant, Node-RED & MQTT op Linux Mint - HA nodes in Node-RED
Home Assistant, Node-RED & MQTT op Linux Mint – HA nodes in Node-RED

Start Node-Red automatically

To make Node-Red start when the computer starts up, you can follow the “Step 3 – Launching Node-RED on Startup” section of the instructions in this article, with a few minor adjustments.

Just like for HA, we create a service file with Nano:

sudo nano /etc/systemd/system/node-red.service

Paste the following code into the file:

[Unit]
Description=Node-RED
After=syslog.target network.target

[Service]
ExecStart=/usr/bin/node-red --max-old-space-size=128 -v
Restart=on-failure
KillSignal=SIGINT

# log output to syslog as 'node-red'
SyslogIdentifier=node-red
StandardOutput=syslog

# non-root user to run as
WorkingDirectory=/home/myusername/
User=myusername
Group=myusername

[Install]
WantedBy=multi-user.target

Pay attention:

  • Execstart must point to /usr/bin/node-red
  • WorkingDirectory, User and Group: enter the non-root username of your Linux Mint installation instead of myusername. In other words, the username with which you normally log in to Linux Mint.

Save the file with CTRL + X and then Y.

With the following command, Node-RED will now start automatically:

sudo systemctl enable node-red

You can start Node-RED manually with:

sudo systemctl start node-red

or stop with:

sudo systemctl stop node-red

Next steps

With these instructions, you make a basic installation with HA, Node-RED, and Mosquitto. Furthermore, it is wise to protect Node-RED and Mosquitto with passwords and TLS. Instructions for this can be found in the blogs “IoT server: Mosquitto and Node Red on Raspberry Pi” and “IoT server: secure MQTT communication using TLS“, in a later article we will see how we can do this with Linux Mint.

6 Comments