How to set up home assistant MQTT sensor on raspberry pi

Home automation is an exciting thing once installed and appropriately maintained. You can find many available out of box home automation solutions that are not free. With some technology understanding enthusiasm, you can start building your own system. In this post, I will share the necessary steps needed to set up home assistant MQTT sensor on raspberry pi.

Building a home automation system from scratch may be a tedious job. You need to take care of many things, including data management, security, communications, user interface, scheduling, and many other different tasks. Imagine the coding and knowledge, which is required to make all things fork fluently. Additionally, a sound home automation system should be capable of handling DIY and purchased devices such as light controllers, smart outlets, or sprinklers. If it connects to the internet, you should be able to control it with the home automation system.

home assistant sensor

You could get away with a DIY solution running on the Raspberry Pi script for one or two devices such as temperature sensor or blinds closing motor, but when things start to build up, you will start losing the battle of managing things. This is why the best solution is to use a ready home automation platform, which takes care of many things.

Best free home assistant software for Raspberry Pi

You probably are expecting a positive statement on which home automation platform is the best to use with Raspberry Pi. It would not be fair to price one system over another, because there are many great solutions with their history and communities.

 I found that two major platforms are heavily maintained and are Raspberry Pi friendly – OpenHAB and Home Assistant. They both are running custom OS for Raspberry Pi. The OpenHAB is developed in Java while Home Assistant in Python 3. They are both similar in terms of flexibility, community activity, and installation. However, Home Assistant appears to be more user friendlier and require a bit less coding and configuring.

There are even more open source home automation systems that may be worth trying – Domoticz, Mycroft, MisterHouse, Jeedom, and smaller ones that also be attractive at some point. Anyway, this is not the post on comparing home automation systems. I think Home Assistant (HA) is a great system to start if you never tried one. In the end, we all want things to get working, and if not, expect some help in the community.

Set up Home Assistant on Raspberry Pi

The setup of Home Assistant on Raspberry Pi is straightforward. All you need is to download a prebuilt image from the list. Flash image to the MicroSD card and boot the system with LAN connected to PC. If you need a preconfigured WiFi or static IP, you need to ad CONFIG/network/my-network file with the specified network settings to the SD card before booting.

Apply power to Raspberry Pi and allow up to 20 minutes for it to complete the installation. During this time, It downloads the latest version of HA and configures it.

hassio start window

You may access Home Automation application via the web interface by typing address on the browser: http://hassio.local:8123. In case your router does not support mDNS you may need to enter devices IP address as follows: http://192.168.0.101:8123 – be sure to type your own assigned IP.

After installation is complete, you will be prompted to create a user name which will be used to login to your HA, select your location, time zone, and other non-critical parameters. After that, HA window opens. In the Overview window, you will tour first data – weather information which is pulled according to your selected location.

hassio weather sensor

Installing MQTT broker ad-on

If you are planning to interface various sensors around the house, the MQTT protocol is the best choice. It allows sending short messages between devices and has great support among different devices with LAN and Wi-Fi Capability. In this example am using a separate Raspberry Pi, which reads temperature data from DS18B20 1-wire sensor and sends data using MQTT messages.

You have a couple of choices on how to use MQTT broker. One option is to run it on a separate device or computer or install HA add-on. Both work well, just you have less control over add-on, and it may break when performing updates. I had a bit of a hard time configuring MQTT on HA because latest updates brought few changes that made it challenging to deliver anonymous messaging.

TO set up A MQTT broker on HA, go to Hass.io menu on sidebar and go to ADD-ON STORE top menu. Select Mosquitto broker and click install.

home assistant mosquitto broker

After it finishes the installation, in the window you will see a Config window, where you will need to make some changes. Earlier it was possible to send MQTT messages without authentication since add-on version 4.1 authentication is required. Edit Config as follows:

{
  "logins": [
    {
      "username": "mqttm",
      "password": "mqttm"
    }
  ],
  "anonymous": false,
  "customize": {
    "active": true,
    "folder": "mosquitto"
  },
  "certfile": "fullchain.pem",
  "keyfile": "privkey.pem"
}

Start Mosquitto broker.

There is also a couple of necessary configurations are needed to make it work. At first, you need a way to edit configuration files. The easiest and fastest is to install Configurator add-on, which enables editing configuration files on the webpage of UI. It is located in the same place as the MQTT add-on. Just select and install it.

Now we need to add MQTT user to ACL (access control list) which is not created automatically. For this, we need to create a couple files in the system. This is where Configurator editor comes in handy. Go to Hass.io->Dashboard and select Configurator and then Open Web UI.

Then browse to the share/ folder:

mosquitto folder in share location

enter the mosquito folder and create a couple files. Firs file is named acl.conf and second accesscontrollist.

In the acl.conf enter the following line:

acl_file /share/mosquitto/accesscontrollist

and in accesscontrollist:

user mqttm
topic readwrite #
user homeassistant
topic readwrite #

Here mqttm is the user name, which was included in MQTT Conf when we imported the Broker add-on. The user names must match and may be different from mine.

After editing files, do not forget to save:

saving HA configurations

Finally, go to the Configuration menu and click Integrations. You will find that MQTT has been discovered and needs further configuration.

MQTT configure in integrations menu

Click on Configure and select “Enable discovery” and then click submit. This is done only once.

MQTT enable discovery in home assistant

If everything goes well, you should see a Success message that Mosquitto broker has been configured. I hope that this process will be simplified sometime soon with the upcoming releases.

Create your first sensor on Home Assistant

Creating a simple sensor on HA is easy. All you need is to enter a few lines into configuration.yaml file. We are going to receive and display a temperature value. For this, we need to add a sensor entity in the configuration.yaml file. To this, go to Configurator as previously, open configuration.yaml file and enter the following lines of code:

sensor ds18b20:
  - platform: mqtt
    state_topic: "home/pi/t"
    name: "MQTT Sensor 1"

Be sure that indentations of lines are correct. Luckily, each time you edit the configuration file, there is an option to check for errors. Go to the Configuration menu and then General. Where you will find a button Check Config.

check configurations in home assistant

After that always restart the server to apply new configuration settings.

Restart home assistant server

Wait for server reconnection.

In the overview window, you will see a new sensor with no value

New sensor in home assistant UI

Testing HA sensor by sending MQTT message

Now we can proceed with values. Initially, you can test if MQTT works properly by sending a message with the value from any device having MQTT client. You can do this from the phone app. Since I intend to use Another raspberry pi, with mosquito client, I send the following message which puts some value:

mosquitto_pub -h 192.168.0.101 -t "home/pi/t" -u "mqttm" -P "mqttm" -m 75
home assistant sensor value

You can see that the sensor is updated with the value of 75. Once you got over your first home assistant MQTT sensor it is time to start integrating more devices, automate and control everything around the house.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.