Differences

This shows you the differences between two versions of the page.

Link to this comparison view

iothings:proiecte:2022:home_assistant_using_mqtt [2023/01/20 06:15]
andrei.francu
iothings:proiecte:2022:home_assistant_using_mqtt [2023/01/20 07:33] (current)
andrei.francu [Home Assistant using MQTT]
Line 1: Line 1:
 ====== Home Assistant using MQTT ====== ====== Home Assistant using MQTT ======
 Student: Frâncu Andrei\\ Student: Frâncu Andrei\\
-Video: \\ +Video: ​[[https://​youtube.com/​shorts/​P5f7dom1Wt4?​feature=share|Demo]]\\ 
-Project archive: \\+Project archive: ​{{ :​iothings:​proiecte:​2022:​automation_scripts.7z | Automation Scripts }}\\
  
 ======1. Objective ====== ======1. Objective ======
Line 24: Line 24:
   * Sony Bluetooth speaker   * Sony Bluetooth speaker
     * The speaker is used for the environment preset feature of the home assistant. ​     * The speaker is used for the environment preset feature of the home assistant. ​
-{{ :​iothings:​proiecte:​2022:​fa_sony_speaker.png | Sony Bluetooth speaker}} 
- 
-  *Implementation: ​ 
- 
-**ADD HARDWARE PIC** 
 ======3. Software description & implementation ====== ======3. Software description & implementation ======
  
Line 34: Line 29:
  
 **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** 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.\\
-{{ :​iothings:​proiecte:​2022:​fa_dashboard.png | Home Assistant Dashboard}}+{{ :​iothings:​proiecte:​2022:​fa_dashboard6.png | 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.\\ ​ 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.\\ ​
  
-**ADD BUTTON CARD AND YAML CODE PIC**\\ ​+<code c> 
 +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"​ 
 + 
 +</​code>​
   ​   ​
 **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**.\\ **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**.\\
Line 44: Line 50:
 **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: **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). ​   * 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). ​
 +<code c>
 +# 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
 +
 +</​code>​
   * 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// **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. \\   * 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. \\
-**ADD CODE FOR SCRIPTS** 
  
 ======4. Challenges ====== ======4. Challenges ======
 During the elaboration of this project I've encountered some challenges: During the elaboration of this project I've encountered some challenges:
   - **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.    - **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. 
-  - **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.  ​+  - **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. ​ 
 +  - **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.  
 +  - **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 ====== ======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 ====== ======6. Resources ======
 [[https://​www.raspberrypi.com/​documentation/​|Raspberry Pi 4 Doc]] \\ [[https://​www.raspberrypi.com/​documentation/​|Raspberry Pi 4 Doc]] \\
iothings/proiecte/2022/home_assistant_using_mqtt.1674188149.txt.gz · Last modified: 2023/01/20 06:15 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