logo elektroda
logo elektroda
X
logo elektroda

How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

p.kaczmarek2 1020 9
ADVERTISEMENT
📢 Listen (AI):
  • How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Sonoff R5 Scene Controller is a battery-powered 6-gang remote wall switch featuring three different button events (click, double or hold). It is usually paired with Sonoff Gateway, but here we will show you how you can pair it with ESP32 Tasmota device and use it with Home Assistant to create custom automations.
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Sonoff R5 Scene Controller features physical button (tactile switches), so it may be preferred by some users over the solutions based on touch buttons.
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    So, I assume that you have at least one R5 switch. You don't need the Sonoff gateway for this guide.
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Just make sure that your R5 has required batteries, as it runs on two CR2032.
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Then, you will need ESP32 device. Even a tiny board like ESP32-S3 Super Mini USB Type-C will work well.
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Flash it with Tasmota Mi32-bluetooth version (file ends with *-mi32.bin) - https://github.com/Jason2866/Tasmota-specials/tree/firmware/firmware/tasmota32/other
    Choose Tasmota matching your ESP - there are multiple ESP32 versions, C3, S3, etc. Use esptool or Tasmota online installer, or any other flasher of your choice.

    Once Tasmota is running and connected to your WiFi, run in Tasmota console:
    
    SetOption115 1
    

    This will enable BLE. Then, in Tasmota Berry scripting console, paste this to auto load scripts:
    
    code is shown below as image and also present in attachment - removed due to spam filter
    

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    This will fetch required scripts from Github. Remember, it requires internet access!
    Check the console output and once completed under Manage File system check if the needed files are present:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Click on create and edit new file, name it autoexec.be and paste this line in:
    
    code is shown below as image and also present in attachment - removed due to spam filter
    

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Device should restart and you will get a new button on tasmota device home page:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Now click a button on your Sonoff device it should appear above and click Save Device:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Once device is saved if you click on the eWeLink Remote button you can set up Alias name which will be important once you start integration into HA.

    Name the device something recognizable (Sonoff R5 in my case):
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Configure MQTT with the data of your choosing, just like you usually do with Tasmota:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Device will reboot and connect to your HA, or should say MQTT running on HA.

    Go back to tasmota console and type this in:
    
    EweTopicMode 3 %prefix%/remote/%alias%/%type%
    

    Now we can test out HA MQTT setup, go into MQTT and click configure, under Listen to topic type:
    
    /tele/remote/#
    

    and start listening, press a button on your device, and you should see your device come up:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    Each button event (click, double click or hold) is published via ESP32 to Home Assistant.

    Almost there, go back in your browser and while still in MQTT, click on the 3 dots on the side of core-mosquito integration entry and select Add MQTT Device.

    Name your device, give it a model and configuration URL (http://<tasmota_device_IP>)
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    On the next screen Type of entity should be Binary sensor and entity name you chose for yourself:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    On the next screen leave the device class empty, you can change this later if you need to.

    Click next and now we are configuring MQTT specific data. State topic should be:
    
    tele/remote/<your_device_name>/<model>
    

    Value template field should contain this string:
    
    {% if value_json.Button1.Action == 'single' %}ON{% else %}OFF{% endif %}
    

    Payload for on and off should remain the same, click on advanced settings and for OFF delay type in 1, leave expire after blank:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    We are done, finish the wizard and a new Sonoff R5 entry should appear, click on 1 device under it’s name and test:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?
    The same approach can be used for other buttons, just use names like Button2, Button3, etc. Similarly, you can use event names like "double" or "hold".

    The same way can be used to connect multiple R5 buttons to single ESP, just set different aliases:
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    That's how you can connect multiple R5 buttons to Home Assistant via ESP32. Of course, you can also use any other MQTT server and just process published data there, even with Node-Red. For more information about eWeLink Tasmota support, see Flobul/Tasmota-eWeLinkRemote readme:
    https://github.com/Flobul/Tasmota-eWeLinkRemote?tab=readme-ov-file#installation
    Full credit goes to Flobul, you can also check his repository for other supported devices like dimmers, etc. Also special thanks to @DeDaMrAz for testing, photos.

    That's all for now, but in the next part we may try to connect other devices to our gateway. Let us know if you've found this guide useful - do you have any experience with using ESP32 as BT gateway? Feel free to share.

    Cool? Ranking DIY
    Helpful post? Buy me a coffee.
    About Author
    p.kaczmarek2
    Moderator Smart Home
    Offline 
    p.kaczmarek2 wrote 13332 posts with rating 11155, helped 612 times. Been with us since 2014 year.
  • ADVERTISEMENT
  • #2 21591709
    DeDaMrAz
    Level 21  
    In case you want to publish and see data from the sensors this build is designed for - Mi thermostats use a rule, in tasmota console type:

    Rule1 on System#Boot do RuleTimer1 10 endon on Rules#Timer=1 do MI32Option2 1 endon

    activate it with

    rule 1 1

    and reboot the device.

    also to enable ESP32 internal temperature showing both in the tasmoat page and publishing it via MQTT in tasmota terminal type:

    setoption146 1

    More info on how to add these sensor to HA will follow soon, but the principle is basically similar.
  • ADVERTISEMENT
  • #3 21591835
    divadiow
    Level 36  
    I have no eWeLink bluetooth devices but I thought I'd play along as far as I could go.

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    I did find I already had autoxcec.be from factory,

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    Code: Text
    Log in, to see the code


    but no problem, create and edit anyway
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    and that's it. 🤣

    Added after 5 [hours] 39 [minutes]:

    divadiow wrote:
    I have no eWeLink bluetooth devices


    but I do have a collection of TH05F/THB3 Tuya bluetooth temp/hum devices
    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    Just converted one to BTHome opensource firmware https://github.com/pvvx/THB2 and here she is in Mi Tasmota

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    How to connect Sonoff R5 Bluetooth Remote to Home Assitant via ESP32 Tasmota Bluetooth Gateway?

    maybe the conversion steps of these types of devices needs its own thread.

    thanks @DeDaMrAz for bringing Tasmota Mi ESP32 to my attention
  • ADVERTISEMENT
  • #4 21592152
    p.kaczmarek2
    Moderator Smart Home
    Sure, that also would be an interesting presentation!

    I also wonder, would Tasmota BT gate also allow us to pair those cheap Tuya Bluetooth-only lamps? You know, like the ones we order with WiFi modules inside, but Bluetooth-only version.
    Here is a sample teardown of such device: https://www.elektroda.com/rtvforum/topic4105891.html
    Helpful post? Buy me a coffee.
  • #5 21592357
    divadiow
    Level 36  
    I wonder 🤔

    This is my first Bluetooth mini-adventure. I have a Tuya Moes breaker with BT2S (TLSR8250F512ET32) module. I also wonder if there are options for this. 🤔🤔🤔
  • #6 21763122
    insmod
    Level 29  
    Didn't know where to post it, so i decided this thread is appropriate.
    ESP32-C2 ESPHome Bluetooth Proxy config.
    Was a PITA to get it to work, and especially to make it fit into 2MB flash.
    Stable for 12h.
    esphome:
      name: esp32c2-btpxy
      friendly_name: esp32c2-btpxy
      platformio_options:
        board_build.flash_mode: qio
        board_build.flash_size: 2MB
        board_build.maximum_size: 2097152
        board_build.partitions: "/config/esphome/partitions-2mb.csv"
    
    esp32:
      board: esp32-c2-devkitm-1
      variant: esp32c2
      flash_size: 2MB
      framework:
        type: esp-idf
        sdkconfig_options:
          CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y
          CONFIG_BT_GATTS_ENABLE: n
          CONFIG_BT_HFP_ENABLE: n
          CONFIG_BT_HID_ENABLED: n
          CONFIG_BT_HID_HOST_ENABLED: n
          CONFIG_BT_RELEASE_IRAM: y
          CONFIG_BT_STACK_NO_LOG: y
          CONFIG_COMPILER_OPTIMIZATION_ASSERTIONS_SILENT: y
          CONFIG_COMPILER_OPTIMIZATION_LEVEL_RELEASE: y
          CONFIG_COMPILER_OPTIMIZATION_SIZE: y
          CONFIG_COMPILER_SAVE_RESTORE_LIBCALLS: y
          CONFIG_ESP_DEBUG_OCDAWARE: n
          CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_1: n
          CONFIG_ESP_PROTOCOMM_SUPPORT_SECURITY_VERSION_2: n
          CONFIG_ESP_SYSTEM_BBPLL_RECALIB: n
          CONFIG_ESP_SYSTEM_HW_PC_RECORD: n
          CONFIG_ESP_SYSTEM_NO_BACKTRACE: y
          CONFIG_ESP_SYSTEM_PANIC_SILENT_REBOOT: y
          CONFIG_ESP_SYSTEM_PMP_IDRAM_SPLIT: n
          CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10"
          CONFIG_ESP_WIFI_DYNAMIC_RX_BUFFER_NUM: "6"
          CONFIG_ESP_WIFI_DYNAMIC_TX_BUFFER_NUM: "6"
          CONFIG_ESP_WIFI_ENABLE_WPA3_OWE_STA: n
          CONFIG_ESP_WIFI_ENABLE_WPA3_SAE: n
          CONFIG_ESP_WIFI_ENTERPRISE_SUPPORT: n
          CONFIG_ESP_WIFI_IRAM_OPT: n
          CONFIG_ESP_WIFI_RX_BA_WIN: "6"
          CONFIG_ESP_WIFI_RX_IRAM_OPT: n
          CONFIG_ESP_WIFI_SOFTAP_SUPPORT: n
          CONFIG_ESP_WIFI_STATIC_RX_BUFFER_NUM: "3"
          CONFIG_ETH_USE_SPI_ETHERNET: n
          CONFIG_HAL_ASSERTION_SILENT: y
          CONFIG_HAL_LOG_LEVEL_NONE: y
          CONFIG_HAL_SYSTIMER_USE_ROM_IMPL: y
          CONFIG_HAL_WDT_USE_ROM_IMPL: y
          CONFIG_HEAP_PLACE_FUNCTION_INTO_FLASH: y
          CONFIG_HEAP_TLSF_USE_ROM_IMPL: y
          CONFIG_LOG_DEFAULT_LEVEL_NONE: y
          CONFIG_LOG_DYNAMIC_LEVEL_CONTROL: n
          CONFIG_LOG_TAG_LEVEL_IMPL_NONE: y
          CONFIG_LOG_TIMESTAMP_SUPPORT: n
          CONFIG_LOG_VERSION_2: y
          CONFIG_LWIP_DHCPS: n
          CONFIG_LWIP_DNS_SUPPORT_MDNS_QUERIES: n
          CONFIG_LWIP_IPV6: n
          CONFIG_LWIP_IPV6_AUTOCONFIG: n
          CONFIG_LWIP_ND6: n
          CONFIG_LWIP_TCPIP_RECVMBOX_SIZE: "16"
          CONFIG_LWIP_TCPIP_TASK_STACK_SIZE: "2560"
          CONFIG_LWIP_TCP_FIN_WAIT_TIMEOUT: "16000"
          CONFIG_LWIP_TCP_HIGH_SPEED_RETRANSMISSION: n
          CONFIG_LWIP_TCP_OVERSIZE_QUARTER_MSS: y
          CONFIG_LWIP_TCP_RTO_TIME: "1500"
          CONFIG_LWIP_TCP_SND_BUF_DEFAULT: "4096"
          CONFIG_LWIP_TCP_SYNMAXRTX: "12"
          CONFIG_LWIP_TCP_WND_DEFAULT: "2440"
          CONFIG_MBEDTLS_CLIENT_SSL_SESSION_TICKETS: n
          CONFIG_MBEDTLS_DEBUG_LEVEL_WARN: y
          CONFIG_MBEDTLS_DYNAMIC_BUFFER: y
          CONFIG_MBEDTLS_DYNAMIC_FREE_CA_CERT: y
          CONFIG_MBEDTLS_DYNAMIC_FREE_CONFIG_DATA: y
          CONFIG_MBEDTLS_ERROR_STRINGS: n
          CONFIG_MBEDTLS_FS_IO: n
          CONFIG_MBEDTLS_SERVER_SSL_SESSION_TICKETS: n
          CONFIG_MBEDTLS_SHA512_C: n
          CONFIG_MBEDTLS_SSL_ALPN: n
          CONFIG_MBEDTLS_SSL_KEEP_PEER_CERTIFICATE: n
          CONFIG_MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH: y
          CONFIG_MBEDTLS_TLS_CLIENT_ONLY: y
          CONFIG_MBEDTLS_USE_CRYPTO_ROM_IMPL: y
          CONFIG_NEWLIB_NANO_FORMAT: y
          CONFIG_SOC_RMT_SUPPORTED: n
          CONFIG_SPI_FLASH_ROM_DRIVER_PATCH: n
          CONFIG_SPI_FLASH_ROM_IMPL: y
          CONFIG_SPI_MASTER_ISR_IN_IRAM: n
          CONFIG_SPI_SLAVE_ISR_IN_IRAM: n
          CONFIG_VFS_SUPPORT_DIR: n
          CONFIG_VFS_SUPPORT_IO: n
          CONFIG_VFS_SUPPORT_SELECT: n
          CONFIG_VFS_SUPPORT_TERMIOS: n
          CONFIG_XTAL_FREQ_26: y
    
    # Enable logging
    logger:
      baud_rate: 0
      level: NONE
        
    # Enable Home Assistant API
    api:
      max_connections: 2
      on_client_connected:
         - esp32_ble_tracker.start_scan:
            continuous: true
      on_client_disconnected:
        if:
          condition:
            not:
              api.connected:
          then:
            - esp32_ble_tracker.stop_scan:
    
    wifi:
      ssid: !secret wifi_ssid
      password: !secret wifi_password
      domain: .lan
    
    packages:
      common: !include base/common.yaml
      ota: !include base/ota.yaml
    
    esp32_ble_tracker:
      id: ble_tracker
      scan_parameters:
        interval: 211ms
        window: 120ms
        active: false
        continuous: false
    
    bluetooth_proxy:
      active: false
    
    mdns:
      disabled: false

    base/ota.yaml
    api:
      reboot_timeout: 12h
      encryption:
        key: !secret apikey
    
    ota:
      - platform: esphome
        password: !secret apikey

    base/common.yaml
    substitutions:
      friendly_name: ${friendly_name}
      powersave: NONE
    
    switch:
      - platform: restart
        id: reboot
        name: Restart


    partitions-2mb.csv -> https://github.com/openshwprojects/OpenBK7231...lob/main/platforms/ESP-IDF/partitions-2mb.csv

    Free heap with debug enabled is about 20k.
  • #7 21763132
    DeDaMrAz
    Level 21  
    >>21763122
    I've been running a long term test on C3 and apart from some heating from the module (solved by adding a small heat sink on the chip) it has been running stable for at least 2 months.

    Great to know C2 can work in that role as well! Thanks for sharing.
  • #8 21763170
    p.kaczmarek2
    Moderator Smart Home
    Thanks for sharing. I'm curious how much work would take to do the same in OBK. I must find some time to tinker with it. I don't even yet know how BT devices communicate - JSON, XML, something else?
    Helpful post? Buy me a coffee.
  • ADVERTISEMENT
  • #9 21763181
    insmod
    Level 29  
    >>21763170
    Some basic info: https://learn.adafruit.com/introduction-to-bluetooth-low-energy/gatt

    Possible to do it in OBK via HA "Passive BLE monitor integration" and mqtt. I think...
    Won't be possible on 2mb devices, because even esphome binary bp5758d+bluetooth is almost 900kb, and that's with those sdkconfig_options.

    Plus that way only passive sensors can be added.

    Added after 1 [minutes]:


    Screenshot of a table showing Bluetooth device data including names, MAC addresses, and RSSI values
  • #10 21763188
    DeDaMrAz
    Level 21  
    insmod wrote:
    Was a PITA to get it to work, ...


    I was just commenting with @p.kaczmarek2 how I would not recommend to anybody to speed run Berry script or ESPHome 🤣🤣

    I am on my 28th attempt to get fully working Dooya remote blinds with ESPHome+CC1101 😁
📢 Listen (AI):

Topic summary

The discussion focuses on integrating the Sonoff R5 Scene Controller, a battery-powered 6-gang remote with tactile switches supporting multiple button events (click, double, hold), with Home Assistant using an ESP32 running Tasmota as a Bluetooth gateway, bypassing the need for the official Sonoff Gateway. The Sonoff R5 operates on two CR2032 batteries. Users explore configuring Tasmota rules and commands to enable Bluetooth Low Energy (BLE) functionality on the ESP32, including sensor data publishing and internal temperature reporting via MQTT. The conversation also touches on experimenting with Tasmota's Bluetooth gateway capabilities for other Bluetooth devices, such as Tuya Bluetooth-only lamps and Tuya Moes breakers with BT2S (TLSR8250F512ET32) modules, highlighting potential for broader device integration. Sample Tasmota console commands and rule setups are shared to facilitate BLE device pairing and data handling within Home Assistant.
Summary generated by the language model.
ADVERTISEMENT