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
    • This the main hardware component of the project. It is used to host the Home Assistant server and the Mosquitto broker and to run the Zigbee2MQTT gateway application and the automation scripts.

 Raspberry Pi 4B Board

  • Sonoff Zigbee Dongle
    • The dongle is used as a hardware gateway, allowing the control of the Zigbee smart devices using MQTT.

 Sonoff Zigbee Dongle

  • Smart devices
    • 2 x Sonoff S26 smart plug

 Sonoff S26 smart plug

  • 2 x Ikea Tradfri LED E27 bulbs

 Ikea Tradfri LED E27 bulbs

  • Sony Bluetooth speaker
    • The speaker is used for the environment preset feature of the home assistant.

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:

  • The turn smart plugs on/off scripts are simple scripts that create a client which publishes “ON”/“OFF” on the smart plug state topic. The scripts are ran using the cron job scheduler at the given hours (i.e. in the morning and at night).
# 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
  • The wake-up light script increments the value of the published LED brightness every minute in order to create a sunrise effect. After the maximum brightness value is reached the user has to manually turn of the light from the HA application, similar to an alarm clock. The script is started using the cron scheduler, some time in the morning and it waits until there is one hour to sunrise to publish the first brightness value.
  • The environment preset scripts implement a client that waits for a message on a topic corresponding to each preset. After receiving it, the on_message callback is invoked which publishes a value for the LED brightness and color and opens a YouTube radio page, depending on activated environment preset.

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

iothings/proiecte/2022/home_assistant_using_mqtt.txt · Last modified: 2023/01/20 07:33 by andrei.francu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0