Table of Contents

Home Assistant using MQTT

Student: Frâncu Andrei
Video: Demo
Project archive: Automation Scripts

1. Objective

The main objective of this project is the implementation of a home assistant based on MQTT. The main hardware component is the Raspberry Pi board which will host the Home Assistant server along with the Mosquitto broker and will run the Zigbee2MQTT gateway application and the automation scripts. The principal features are the automation of smart plugs to turn on/off at the desired hours, a wake-up light using smart lights and different environment presets to set the desired light and music (ex. workout, party, study). All smart devices can also be controlled manually from the Home Assistant web server or mobile application.

Home Assistant is an open-source software for home automation and throughout this documentation it is referred to using uppercase initials or the HA acronym. The lower case notation: “home assistant” refers to the scope of this project.

2. Hardware description & implementation

 Raspberry Pi 4B Board

 Sonoff Zigbee Dongle

 Sonoff S26 smart plug

 Ikea Tradfri LED E27 bulbs

3. Software description & implementation

Eclipse Mosquitto is an open source message broker that implements the MQTT protocol. The role of the broker is to route the published messages to the subscribed clients.

Home Assistant is an MQTT supervisor. It offers a visual interface for the state of the integrated smart devices and updates it accordingly, when a message is published on the corresponding topic. All the devices can be grouped and accessed through a customizable dashboard. The Home Assistant software also allows the users to create automations using the application. In this project, HA is only used as a visual interface and to activate the different environment presets.
 Home Assistant Dashboard

The widgets that Home Assistant offers to customize your dashboard are called cards. Each card can have one or more devices attached to it. To activate the environment presets, I created a virtual switch device that publishes an MQTT message on a given topic when toggled and attached this entity to a button card.

mqtt:
  switch:
    - command_topic: "mode/party/set"
      state_topic: "mode/party/set"
      unique_id: party_switch
      payload_on: "ON"
      payload_off: "OFF"
      state_on: "ON"
      state_off: "OFF"

Zigbee2MQTT is a nodejs Gateway application that connects Zigbee networks to MQTT networks. It allows the usage of a single unified bridge for all the connected Zigbee devices instead of each vendor's bridge. It currently supports 2640 devices from 351 different vendors.

Eclipse Paho MQTT is a Python library that provides a client class which allows the user to connect to a broker and publish and subscribe on different topics. In this project I've used paho-mqtt to implement all my automation scripts:

# m h  dom mon dow   command
30 21 * * * python3 /home/andrei-francu/turn_off_prize_plante.py
40 6 * * * python3 /home/andrei-francu/turn_on_prize_plante.py
45 6 * * * python3 /home/andrei-francu/wup_light_script.py

4. Challenges

During the elaboration of this project I've encountered some challenges:

  1. Zigbee2MQTT / Sonoff Dongle limitation: The number of identical consecutive messages on the same topic is limited to 20.
    Solution: making the brightness increment bigger, in order to cover the full brightness range of the LED.
  2. Zigbee2MQTT / Sonoff Dongle limitation: The wake-up light script takes about 15 minutes to finish after the brightness incrementation starts (for a total of about 30 minutes runtime at this time of the year). When this script is scheduled before the turn on plugs script there is a point in time in which the two scripts have to run in parallel. Because these scripts are run on an exact schedule and the brightness increment is done every minute there is a possibility that the messages are published at the same time, thus resulting in a race condition and the corruption of the data. As a result the smart plugs were unable to receive any message until the restart of the devices.
    Solution: schedule the turning on of the plugs before the wake-up light.
  3. Selenium Python package limitation: Due to the fact that I am using message callbacks to open the YouTube radio, the opened window closes after the function is finished executing. You can make the window persisent but then it it impossible to close it using selenium.
  4. Home Assistant core limitation: I tried using the Radio integration of Home Assistant but I was not able to play anything because the Pulse Audio server refuses to connect to HA.

5. Conclusions

In conclusion, even though I could not implement a fully functional radio, I feel that I created a useful system that will be an integral part of my home. We've long struggled with waking-up early in the winter and it was always an issue to close the plant lights when we were gone for a longer period of time, so this might be the end of those problems.

6. Resources

Raspberry Pi 4 Doc
Mosquitto
Home Assistant
Zigbee2MQTT
Paho MQTT