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
- Start with the MQTT Mosquitto Broker on Raspberry Pi, Windows, MacOS and Linux
- Start with the Homie library (MQTT) for connected objects ESP8266
- First Homie project. A DHT22 probe
Equipment used

Circuit
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.
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> const int TEMPERATURE_INTERVAL = 300; 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("temperature").send(String(t)); temperatureNode.setProperty("json").send("{\"t\":" + String(t) + ",\"unit\":\"c\"}"); humidityNode.setProperty("humidity").send(String(h)); humidityNode.setProperty("json").send("{\"h\":" + String(t) + ",\"unit\":\"%\"}"); lastTemperatureSent = millis(); } lastTemperatureSent = millis(); } } void setup() { Serial.begin(115200); Serial << endl << endl; Homie_setFirmware("demo-dht22", "1.0.0"); Homie.setLoopFunction(loopHandler); Homie.setup(); } void loop() { Homie.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"
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
mqtt: 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
logger: default: error logs: 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
temperature: name: Sondes de température entities: - sensor.homie_temperature humidity: name: Sondes d'humidité entities: - 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.
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.
- Home Assistant. Install the snap on Synology NAS on an Ubuntu Virtual Machine
- Home Assistant. Essential plugins to install. Samba, HACS, File Editor, MQTT Mosquitto
- Home Assistant Community Store (HACS), easily install themes and components
- Home Assistant. Get started on Raspberry Pi 4. Procedure 2020
- ESP8266 + DHT22 + MQTT: make a connected object IoT and include it in Home Assistant
- How to Include RFLink Radio Home Automation Devices at Home Assistant (HASS)
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.
Hello Ari and thank you very much. Yes you’re right, the bookstore Homie is not very easy to take in May to start. You might be able to use the pubsub library by following this tutorial https://diyprojects.io/esp8266-dht22-mqtt-make-iot-include-home-assistant/. I think it’s easier to start. See you soon