Home Assistant using MQTT with LoRa and Tuya integration

Student: Zsugya Norbert-Gabriel

Mater: ACES II

Video, Presentation and Source code: https://1drv.ms/f/s!AiFSSSAJY4tDjVj-gACJLlt7F4Vg?e=sLH02d


I will implement a control system for house lights. I will use 2 ESP8266 boards to communicate with each other by LoRa RFM95 modules. One ESP board will have a microphone sensor for clap detection and will transmit over radio this event. On the other side, the other ESP board will receive over radio the microphone status and will communicate over MQTT with Home Assistant server. A smart switch will be connected to Home Assistant using Tuya plugin, and will create a scenario, each time clap is detected, the switch will change its state.

Hardware description & implementation


The schematic diagram of the connections between ESP8266, Microphone and LoRa RFM95 is the following:

|System Schematic Diagram

Software description & implementation

The application is based on 3 different parts like in Figure 5:

  • Server - Home Assistant server is the central place which collects data and facilitates user interaction with the system.
  • ESP8266 bridge – it is intermediating the communication between the sensor (using LoRa radio protocol) and the server (using MQTT protocol)
  • ESP8266 sensor – it detects the claps using the microphone and sends status to the bridge when a clap is detected.

|System architecture

Having a sensor and a bridge enables the fact that the sensor can be placed at a long distance from Wi-Fi router, feature enabled by LoRa protocol. This architecture is IoT Level 5, meaning that we have a cloud application, a coordinator node and many end nodes.

  • Home Assistant dashboard

The dashboard for controlling the lights can be used from both web browser or mobile application and it is fully customizable.


  • Home Assistant scene

For controlling the smart switch using Tuya plugin, I set an automation scene which sets the state of the switch when a new value on MQTT clap detection topic is reached.

  • Home Assistant MQTT

There are necessary the following MQTT settings for Home Assistant server to be able to communicate with the subscriber.

#add mqtt devices
    - state_topic: "home/livingroom/temp"
    - unique_id: livingroom_led_switch
      name: "Livingroom Led Switch"
      state_topic: "home/livingroom/led_switch"
      command_topic: "home/livingroom/led_switch/cmd"
      #  - topic: "home/bedroom/switch1/available"
      payload_on: "255"
      payload_off: "0"
      state_on: "255"
      state_off: "0"
      optimistic: false
      qos: 2
      retain: true
void callback_LoRa(int packetSize) {
  // received a packet
  TRACE_LOG("Received packet '");
  // read packet
  String message = LoRa.readString();
  // print RSSI of packet
  TRACE_LOG("' with RSSI ");
  if(message == LIVING_LED_SWITCH_CMD.path){
    led_state = 255 - led_state;
    digitalWrite(LED_PIN, 255 - led_state);  
    client.publish(LIVING_LED_SWITCH_STATE.path.c_str(), String(led_state).c_str());  

Clap detection function features:

  • Detects if so many claps within a short period of time and disables for some time the clap detection feature. This feature allows noise detection having a safety purpose, so the switch and light do not turn on and off so many times in a short interval if there is noise inside the house.
  • There are 3 versions of clap detection function which are implemented. The last version does an average reading on the digital input pin and performs average on the reading values. This feature is also added for robustness, so small microphone noise inputs to be ignored.
//reset clap_counts
if(clap_counts >= NOISE_CLAPS_NO){
  if ((millis() - last_clap_millis) <= NOISE_DETECTION_INTERVAL){
    TRACE_LOGln("Detected " + String(clap_counts) + " claps in " + String(NOISE_DETECTION_INTERVAL) + "s. Disabling Clap feature for " + String(CLAP_FEATURE_DISABLE_TIME) + "s");
    clap_enable = DISABLED;
  clap_counts = 0;
  last_clap_millis = millis();
  TRACE_LOGln("Reset clap abundance phase");
// enable again clap feature
if ((millis() - last_clap_millis) >= CLAP_FEATURE_DISABLE_TIME && clap_enable == DISABLED){
  clap_enable = ENABLED;
  TRACE_LOGln("Clap feature enabled");

The dashboard for controlling the lights can be used from both web browser or mobile application and it is fully customizable.


  • One challenge was the implementation of various clap detection functions, to test on different environmental conditions. The challenge was to find a pretty good approach to being robust to noise, but also still detect the claps.
  • Another challenge was setting up the smart switch because it is not able to function on 5GHz network. So, it was necessary to firstly debug why I couldn’t find it in Tuya application.
  • Finding smart switches compatible with Tuya and with old home electrical wiring (Involving a capacitor for the LED bulb).


In conclusion, in this project I managed to create a full home smart light automation system with clap detection flavor. I learned how to work with MQTT and LoRa protocols and integrate all with Home Assistant. I also experimented with house electrical circuits.


iothings/proiecte/2023/homeassistantsmartswitch.txt · Last modified: 2024/01/15 11:17 by norbert.zsugya
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