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

Table of Contents
drapeau france
level advanced

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

Wemos® D1 Mini NodeMcu Lua WIFI ESP8266 Development Board
CHF 4.89
AE__banggood Banggood
Geekcreit® DHT22 AM2302 Digital Temperature Humidity Sensor Replace SHT15 Logger
CHF 7.82
AE__banggood Banggood
Wemos® DHT22 Single Bus Digital Temperature Humidity Sensor Shield For WeMos D1...
CHF 7.82
AE__banggood Banggood
Last update was on: 10 August 2022 3 h 06 min


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).

#include <Homie.h>
#include <DHT.h>

unsigned long lastTemperatureSent = 0;

const int PIN_DHT22 = D4;    // Broche - Pin DHT22
DHT dht(PIN_DHT22, DHT22);

HomieNode temperatureNode("temperature", "temperature");
HomieNode humidityNode("humidity", "humidity");

void loopHandler() {
  if (millis() - lastTemperatureSent >= TEMPERATURE_INTERVAL * 1000UL || lastTemperatureSent == 0) {
    float t = dht.readTemperature();
    float h = dht.readHumidity();
    // Affiche les mesures dans le journal - send values to the logger
    Homie.getLogger() << "Temperature: " << t << " °C | humidity: " << h << "%" << endl;
    if ( !isnan(t) ) {
      temperatureNode.setProperty("json").send("{\"t\":" + String(t) + ",\"unit\":\"c\"}");
      humidityNode.setProperty("json").send("{\"h\":" + String(t) + ",\"unit\":\"%\"}");
      lastTemperatureSent = millis();
    lastTemperatureSent = millis();

void setup() {
  Serial << endl << endl;

  Homie_setFirmware("demo-dht22", "1.0.0");


void loop() {

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

mosquitto_sub -h localhost -t "homie / #" -v

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

mosquitto_sub -h localhost -t "homie / #" -v -u "USER" -P "PASSWORD"

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

cd /home/homeassistant/.homeassistant

Then open the configuration file

sudo nano configuration.yaml

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

  broker: localhost           #si le Broker est installé sur le même poste qu'Home-Assistant 
  port: 1883                  #par défaut
  client_id: home-assistant-1 #peu important  
  keepalive: 60 
  username: USERNAME          #si l'authentification est active
  password: PASSWORD          #idem
  protocol: 3.1.1             #par défaut

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

  default: error
    homeassistant.components.mqtt: debug

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

sensor: !include sensors.yaml

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

Open the sensor configuration file sensors.yaml

sudo nano 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.

- platform: yr

# temperature Topic
- platform: mqtt
  state_topic: homie/demohomie-projetsdiy/temperature/temperature
  name: "Homie DHT22"
  qos: 0
  unit_of_measurement: "°C"

# humidity Topic
sensor dht_hum:
  platform: mqtt
  state_topic: homie/demohomie-projetsdiy/humidity/humidity
  name: "Homie DH22"
  qos: 0
  unit_of_measurement: "%"

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.

# temperature Topic
sensor dht_temp:
  platform: mqtt
  state_topic: homie/demohomie-projetsdiy/temperature/temperature
  name: "Homie Tempertaure"
  unit_of_measurement: "°C"
  value_template: "{{  value | round(1) }}"

# humidity Topic
sensor dht_hum:
  platform: mqtt
  state_topic: homie/demohomie-projetsdiy/humidity/humidity
  name: "Homie Humidity"
  unit_of_measurement: "%"
  value_template: "{{  value | round(0) }}"

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

  name: Sondes de température
    - sensor.homie_temperature

  name: Sondes d'humidité
    - sensor.homie_humidity

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

sudo systemctl restart home-assistant@homeassistant

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

sudo systemctl restart home-assistant

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

sudo journalctl -fu home-assistant@homeassistant

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

home assistant homie esp8266 dht22 wemos d1 mini

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.

Click to rate this post!
[Total: 0 Average: 0]

Thanks for your reading

Did you like this project ? Don't miss any more projects by subscribing to our weekly newsletter!

Are you having a problem with this topic?

Maybe someone has already found the solution, visit the forum before asking your question
  1. I think your knowledge is very great in this area. But I’m trying to run it for hours and I can’t do it. A single and complete tutorial (simplified) would be better.

Leave a Reply

Read more
DIY Projects
DIY Projects
%d bloggers like this: