Home | Difficulty level | Advanced | Include ESP8266 + DHT22 + Homie MQTT IoT in Home Assistant (HASS)

Include ESP8266 + DHT22 + Homie MQTT IoT in Home Assistant (HASS)

After introducing the Homie library in the two previous articles (well starting with Homie, programming principle), it is time to move on to a complete example of application. In this tutorial we will learn how to include a temperature probe in the home wizard Home Assistant developed based on an ESP8266 and a DHT22 probe. To realize this probe, we will use the code developed in the previous tutorial.

If you’re starting out with Homie or developing connected objects, you can start by reading these articles

Equipment used

esp8266 Wemos D1 mini Wemos D1 Mini
chargeur raspbery pi 3 5v 3000ma 5/3A micro-usb power supply


wemos d1 mini dht22 shield Shield DHT22 or DHT11 for Wemos D1 Mini

Probe connected on D4 Pin (GPIO2)


The circuit is a classic of its kind. If you use a shield DHT22 or DHT11 for Wemos d1 mini, the sensor is connected to the D4 (GPIO-2) of the ESP8266.

IOT object connecté domotique home-assistant ESP8266+DHT22+MQTT_bb

Arduino code of the Homie project

Create a new sketch from the Arduino IDE and paste the following code.

Modify the spindle on which the DHT22 is connected and then upload the code. You can change the frequency of sending measurements by changing the TEMPERATURE_INTERVAL  variable.

Follow the instructions in the Homie introduction tutorial to configure the connection to the Wi-Fi network and the MQTT broker (Mosquitto for example).

Check that everything works as expected

Once the Wi-Fi network and the MQTT broker have been configured, it is good to check that messages are being received from the ESP8266. To do this, simply open a Terminal and execute this command

If you have set up an authentication, complete and run this command

homie esp8266 mqtt library arduino json dht22 mosquitto subscribe

Including a Homie IoT in Home Assistant

Now that everything is ready, we can include the Homie object in Home Assistant.

Configuring the connection to the MQTT broker

Move to the Home Assistant directory. If Home Assistant is running in a virtual python environment (recommended since December 2016), it is probably located there

Then open the configuration file

Add a mqtt section and adapt the connection settings to your configuration

Enable Debugging

During debugging, you can retrieve MQTT messages directly from the Home Assistant event log. To do this, add this configuration to the logger block

Configuration of sensors

This is not required, but it is preferable to separate the configuration parameters in separate files. If you do not yet have a configuration file for the sensors, add the following parameter

Save the file (CTRL + X then O or Y)

Open the sensor configuration file sensors.yaml

Paste the following configuration. Modify the Topics to match your Homie messages. It will be necessary to move the platform yr otherwise you will get a beautiful error that will prevent HASS from launching.

If you want to leave the sensors in the configuration.yaml file, the writing is a bit different. A sensor block must be defined for each sensor like this.
Explanation of other parameters

  • name: it is used to define the label that will be displayed on the screen. It will be used to integrate the sensor in a group or a view by replacing the spaces with the character _
  • unit_of_measurement: the unit of measure that will be displayed
  • value_template: value formatting mask. Here we make a rounding. For example, a significant number for temperature and none for moisture

Add measurements to groups

All we have to do now is to include the sensors in the groups. Here I have already prepared groups from previous tutorials on MySensors, RFLink and Xiaomi Mi Home Aquara. To add a sensor, use the usual formality sensor.sensor_name. The name is obtained by replacing the spaces with the symbol “_”. Which give

Save all configuration files (CTRL + O or CTRL + X then O) and then restart the Home Assistant service

Or with this command if Home Assistant is not installed in a Python virtual environment

To monitor that everything is properly configured, run the following command to track the Home Assistant runtime log

And then, after a few moments, you will receive the first MQTT messages from the ESP8266.

home assistant homie esp8266 dht22 wemos d1 mini journalctl

And here is the display

It is really easy to integrate connected objects developed with the Arduino Homie library. Homie is a very powerful library that has been thought for home automation. It supports very well the relay commands but also the dimmers very used in Led lights.