Monitoring Temperature with Home Assistant

As I embark on my first year with a greenhouse, one of the fundamentals I want to grasp is an understanding of the temperature and humidity inside the greenhouse and how it is impacted by the weather outside the greenhouse. Our whole-house “smart system” is centralized through Home Assistant so I wanted to continue the connection into the greenhouse by passing the monitoring from the greenhouse through Home Assistant. This post is the basics of how I accomplish this.

My Home Assistant instance is run from a Docker container and can integrate with MQTT quickly. The sensor in the greenhouse communicates via MQTT with a MQTT broker, which in turn is logged by Home Assistant. Before we script an MQTT client we’ll setup the MQTT broker infrastructure.

MQTT Broker and Home Assistant Client Configuration

The MQTT integration is established by creating an additional container for MQTT with the Eclipse-Mosquitto image. The commands below runs an MQTT container with a persistent configuration file stored to disk and opens the host ports as forwarded from the container. The persistent configuration file is stored in /mnt/datadisk/shares/mqtt/mosquitto.conf on the server – you would use a valid path for your environment.

docker run -it -d -p 1883:1883 -p 9001:9001 -v /mnt/datadisk/shares/mqtt/mosquitto.conf:/mosquitto/config/mosquitto.conf eclipse-mosquitto

sudo ufw allow 1883/tcp
sudo ufw allow 9001/tcp

This container is separate from the Home Assistant container instance, but since they are both run from the same Docker instance the broker IP address for the Home Assistant configuration is the host’s IP address.

Hardware Selection

Frankly, I’m not a hardware tinkerer or junky. I have a huge amount of respect for the patience and interest that it takes to want to utilize resistors, breadboards, or a soldering iron – but it’s just not for me. I can spend hours hammering away at a keyboard but to get started I hope for plug and play, so to speak. Because of this inclination towards one-piece units and the convenience of availability, I selected the MXChip AZ3166 as the hardware to take the temperature/humidity measurements and relay them back to the MQTT broker.

Functionality in Use

<p>
  With a little C (next section), I&#8217;m able to program the Arduino-compatible microcontroller to report back as an MQTT client. The AZ3166 contains the 3 components I need to do this task:
</p>

<ul id="block-f4e7a198-470d-4d1a-abfe-37f807b94932">
  <li>
    microcontroller
  </li>
  <li>
    temperature/humidity sensor
  </li>
  <li>
    wifi module
  </li>
</ul>

Extra Functionality

<ul>
  <li>
    accelerometer
  </li>
  <li>
    RGB LED
  </li>
  <li>
    IR emitter
  </li>
  <li>
    microphone and earphone socket
  </li>
  <li>
    2 buttons
  </li>
  <li>
    magnetometer
  </li>
  <li>
    atmospheric pressure sensor
  </li>
</ul>

As you can see from those 2 lists, in this current iteration of the greenhouse sensor there’s quite of bit of capability that the AZ3166 has that I don’t use. I’m ok with that because there’s always another project.

Arduino Code for MXChip

Armed with a handful of getting started tutorials for VS Code and a sample code library the chip we can get coding quickly.

Prepare your development environment: https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-arduino-iot-devkit-az3166-get-started#prepare-the-development-environment

<p>
  The Arduino examples interface has been converted into a proper viewlet in VS Code. You can find code samples in the lower left when the Arduino extension is activated. The two I relied on, MQTT/MQTTClient and Sensors/SensorStatus are in pictured to the right.
</p>

<p>
  After starting with either of one those samples and successfully uploading the code to your board you are ready to modify it to connect to your Home Assistant instance.
</p>
VS code examples for Arduino with MQTT folder and Sensors folders expanded
I believe I started with the MQTTClient example and consulted the code from SensorStatus.
// PSEUDOCODE FOR THE SKETCH

include libraries and declaring variables for sensors

declare function for connecting to wifi

declare function for sending MQTT message

setup function definition - automatically called on device start {
    // https://www.arduino.cc/reference/en/language/structure/sketch/setup/
    call wifi connection function
}

loop function definition - continuously run by device {
    // https://www.arduino.cc/reference/en/language/structure/sketch/loop/
    call MQTT message function
    wait a few minutes
}

Here’s a link to the actual code: https://gist.github.com/dzsquared/7c212fbb1c174bc5f060c90426ea41f7

Home Assistant MQTT Sensor

The MQTT sensor with value templates allows for the multiple values to be extracted from the JSON MQTT message. In the configuration file two sensors are added where the message data is extracted in the value_template lines.

- platform: mqtt
  state_topic: "temperatures"
  name: "Greenhouse Temperature"
  unit_of_measurement: 'F'
  value_template: "{{ (value_json.tempF)|round(2) }}"
  icon: "mdi:thermometer"
- platform: mqtt
  state_topic: "temperatures"
  name: "Greenhouse Humidity"
  unit_of_measurement: '%'
  value_template: "{{ (value_json.humidity)|round(2) }}"
  icon: "mdi:water-percent"

With the MXChip sensor reporting greenhouse temperature and Home Assistant logging other information, I can quickly see the greenhouse temperature along with the reported local temperature from a weather service.

temperature graph of greenhouse and outside from the last 2 days

What’s Next?

You might have noticed a spike on the graph above around 9am every morning. It’s been sunny every day and for a few moments the sun hits the temperature sensor directly. There aren’t any off the shelf cases for the MXChip so I’m fashioning it into a breathable Raspberry Pi case.

While our Home Assistant instance is secured with SSL, the MQTT broker is allowing unauthenticated clients to send/receive messages by default. Adjusting the security around MQTT on our network would be the next step before adding additional traffic.

A few other ideas:

  • button press to track watering
  • LED displaying whether watering should occur
  • report barometric pressure
  • record planting logs via voice

Any suggestions?