Configuring a temperature sensor with Home Assistant (DHT11)
How to connect a DHT11 to Home Assistant (Container)
I struggle a few weeks trying to understand how to connect a thermometer (DHT11/22) to a Raspberry Pi, and a few more on how to read the sensor values on Home Assistant (running in the same RPI on a container -AKA “non-managed HA”-).
In order for me to remember I decided to write this post. It was a fun process that lead me to learn how to use queue services (mosquito-mq) and also how hard can it be to send and receive data from different services (if you don’t know what you are doing)
I do no longer run Home Assistant in a container, but I think this process already teach me how to do it for other sensors/controllers that might not be supported out-of-the-box. -and other external services-
So stay tune for this no-so-easy to follow tutorial on how connect the DHT11/22 thermometer to Home Assistant -the hard way-.
Instructions
First we will need to connect the DHT sensor to the RPI via RPI-GPIO. There is plenty of tutorials online, so you can use the one that fits you the best. I can recommend these two:
- This is the one I use
- And here is another one more structured
Once that everything is working (hopefully) you will be able to read the values from the sensor using a python script. This is a checkpoint to test for the status of: RPI, connection, and sensor.
Install Home Assistant on a container using the docs
I use the container install because I have other things and services running on my RPI. Home Assistant strongly suggest you to use the “supervised envirorment installation”. These instructions are for people like me that, for some reason, don’t want to dedicate the entire RPI for HA. If you are like me, this tutorial is for you
Now let’s connect the sensor to the HA, this is when things get ugly. The diagram illustrates the various services that are in place for this system.
The human explanation: given that our HA is effectively isolated(recently) from the RPI -running on his docker- It can’t know what is happening on the hardware connected to RPI, so we need a way to share data with the HA. How can we do that ? we can use a “messenger”, AKA “message broker”. This message broker is nothing more than an always-running-process that will receive messages, for example: sensor readings, and it will forward them to any subscriber for that “topic”. So that what we will put in place:
- “The message generator”: a service that will be reading the GPIO that will send messages to the broker MQTT-IO
- “The message broker” (or “data collector”): a “message queue service” always running that will receive and forward the readings MosquitoMQTT
- “The message subscriber”: an integration service running on the HA that will subscribe to the broker to read the sensor readings MQTT Service
There are a few other ways that might work to directly do the connection with the DHT11 using HACS you can install manually a repo. But they didn’t work (for me). So at the end, reading this reddit-topic and this tutorial that “the easiest” way was via an MQTT service
Anyway. let’s deep dive on how to configure each one of these services (1,2,3)
Let’s set-up the MQTT-IO services -the message generator/sensor reader-. We need to install
MQTT-IO
in our environment or directly on the base env (easiest but not always a good idea).1
pip3 install mqtt-io
After that we create a
config.yml
this file will provide the sensor configurations and the address of the broker (not installed yet) and some other properties1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
mqtt: # this will be the address broker, this can be a local service (like in this tutorial) # or an online one host: localhost # this is the topic prefix, this can be customized based on the # sensors added to this process topic_prefix: home #DHT11 sensor configuration sensor_modules: - name: dht11_sensor module: dht22 type: dht11 pin: 17 # this may change if you add additional sensors or don't use pin 4 from above # queues configuration sensor_inputs: - name: temperature module: dht11_sensor digits: 2 interval: 5 type: temperature - name: humidity module: dht11_sensor digits: 2 interval: 5 type: humidity
we will save this file on a location and before running it we will start the broker on the PI so we will have a queue where to storage the messages.
Configure the message broker (AKA
mosquitto-mq
). You can follow this guide that contains some validation steps and a more detailed instructions. the TLDR:run
1 2 3
sudo apt-get update sudo apt-get upgrade
Install mosquitto
1 2 3
sudo apt-get install mosquitto sudo apt-get install mosquitto-clients
Enable mosquitto service
1
sudo systemctl enable mosquitto
After that the mosquitto service should be running on the local host, to start, stop or restart the service use the following commands
1 2 3 4 5
sudo systemctl start mosquitto sudo systemctl stop mosquitto sudo systemctl restart mosquitto
Now we are able to send the messages from the DHT11 to the broker, we can start “the sender” service running the following command:
1
python3 -m mqtt_io config.yml
it should show you a log like the following one (in this case I have other sensors but should be similar)
That means that you are sending the messages to the broker.
[Optional] you can run this capture service in the background and restart when rebooting, you can follow these steps. For my case what worked was crontab
1
crontab -e
Add this line:
1
@reboot cd ~/<my_config_directory>; python3 -u -m mqtt_io config.yml >> nohup.log &
I change directory and just to save the logs on the directory of my config, so I can debug if everything is working properly. You can test that the sensor is posting data running
1
mosquitto_sub -h localhost -p 1883 -t home/sensor/+ -v
it should output something like this:
1 2 3 4 5
toor@raspberrypi:~ $ mosquitto_sub -h localhost -p 1883 -t home/sensor/+ -v home/sensor/temperature 27.00 home/sensor/humidity 25.00 home/sensor/temperature 27.00 ...
you can also open the service to be accessible on the local network using the following configuration
1
sudo nano /etc/mosquitto/mosquitto.conf
then adding the following lines at the end of the config file
1 2
listener 1883 allow_anonymous true
to setup a user/key follow the tutorial
To finalize we should configure “the listener” service (MQTT Service). This Home Assistant integration will be subscribed to the broker. To do that we add the
MQQT
integration. We follow the instructions. We uselocalhost
as the address, and1883
as the port (the default one)Then we modify the
configuration.yaml
of the Home assistant installation to add the sensors from the queues of the broker. We add this on the bottom of the file.1 2 3 4 5 6 7 8 9 10 11 12 13
# DHT sensor mqtt: sensor: - name: "Temperature" state_topic: "home/sensor/temperature" unit_of_measurement: "°C" force_update: true - name: "Humidity" state_topic: "home/sensor/humidity" unit_of_measurement: "%" force_update: true
[Optional] Adding filters. There is a known issue that the DHT sensor can have misreadings from time to time (its a cheap sensor, don’t expect much). but you can improve the data adding this simple filters on the HA interface.
We use the filter integration. There are several filters that we can stack on top of the signal, I use outlier and moving average you can configure your parameters based on your needs.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# DHT11 added filtered sensor: - platform: filter name: "filtered humidity" entity_id: sensor.humidity filters: - filter: outlier window_size: 10 radius: 2.0 - filter: time_simple_moving_average window_size: "00:01" - platform: filter name: "filtered temperature" entity_id: sensor.temperature filters: - filter: outlier window_size: 10 radius: 2.0 - filter: time_simple_moving_average window_size: "00:01"
That’s it, hopefully this helps someone to learn a little more about GPIO/HA/MQTT/RPI/Sensors :)