Support for SwitchBot Plug Mini (W1901400) with ble tracking

I got a good deal on a 4 pack of SwitchBot Plug Minis on Prime day so I thought I’d try to get them working in ESPHome. I’m fairly new to ESPHome so this might not be the best way to do this but I had some luck and didn’t see where anyone else had posted about it yet so am posting my config in case others are interested. My goal was to have a locally controller on/off outlet that supported energy monitoring and integrated well with HA and these appear to fit that goal well and a great price.

Link to the product: SwitchBot Plug Mini

There’s already been some great work to support these in Tasmota, including an OTA flasher so I started there. These are the basic steps I followed:

  1. setup plugs in SwitchBot app like normal
  2. use switchbota to install Tasmota
  • this is a bit involved in that it requires running a local server and spoofing DNS to point the local server but it’s well explained on the page
  • I had to initiate the update directly via Bluetooth using nRF Connect on my iPhone which is explained here
  1. connect to Tasmota hotspot and configure it for the local network
  2. created and downloaded an ESPHome firmware with the config below and downloaded it in legacy format
  3. upload firmware through the Tasmota local webpage

So far it seems to be working great. Few caveats:

  • no webserver in ESPHome since it isn’t supported yet in the esp-idf framework (which is preferred for the esp32-c3 processor that’s in the plug)
  • the value used for the voltage_divider I came to experimentally comparing to my old kill-a-watt
substitutions:
  name: "switchbot"
  friendly_name: "SwitchBot"

esphome:
  name: ${name}

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
    sdkconfig_options:
      CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y
      CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
      CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10"

# Enable logging
logger:

# all the usual wifi configs
<<: !include common/wifi.yaml

# Enable Home Assistant API
api:
  password: !secret api_password

ota:
  password: !secret ota_pass

# Sync time with Home Assistant.
time:
  - platform: homeassistant
    id: homeassistant_time

switch:
  - platform: gpio
    name: "Relay"
    id: "relay"
    device_class: outlet
    pin: 6
    restore_mode: RESTORE_DEFAULT_OFF
    on_turn_on:
      then:
        - light.turn_on: white_led
    on_turn_off:
      then:
        - light.turn_off: white_led

binary_sensor:
  - platform: gpio
    internal: true
    pin:
      number: 2
      mode: INPUT_PULLUP
      inverted: true
    name: "Button"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - switch.toggle: relay
      
light:
  - platform: binary
    internal: true
    name: "white led"
    id: "white_led"
    output: white_output
    
output:
  - id: white_output
    platform: gpio
    pin: GPIO7
    inverted: true

# set blue led as status
status_led:
  pin:
    number: GPIO8
    inverted: true

sensor:
  - platform: hlw8012
    sel_pin:
      number: 20
      inverted: true
    cf_pin: 18
    cf1_pin: 19
    model: BL0937
    voltage_divider: 1467
    current:
      name: "Current"
    voltage:
      name: "Voltage"
    power:
      name: "Power"
    energy:
      name: "Energy"
    update_interval: 1s
    change_mode_every: 4

edit: added sdk config options needed to flash with larger firmware or use ble
edit2: added config to set blue led as status_led

3 Likes

I had exactly the same thought (I’ve got like 20 xiaomi temp/hum sensors throughout the house). I did try to add the “esp32_ble_tracker” module into the config but it failed to compile. I haven’t had time to dive into why but suspect that module isn’t supported in esp-if framework yet. It’ll be a great option once it’s supported.

1 Like

I played around some more and found some good info in an GitHub issue that let me get ble tracking working. You have to add a few sdk config options to the config to get it to compile and OTA update properly.

In short once you have the above config working you need to modify the esp32 section to look like this:

esp32:
  board: esp32-c3-devkitm-1
  framework:
    type: esp-idf
    sdkconfig_options:
      CONFIG_BT_BLE_50_FEATURES_SUPPORTED: y
      CONFIG_BT_BLE_42_FEATURES_SUPPORTED: y
      CONFIG_ESP_TASK_WDT_TIMEOUT_S: "10"

Then you need to flash that before adding the esp32_ble_tracker to your config otherwise the image gets too big and timeout when trying to OTA update. After that’s pushed you can add the esp32_ble_tracker to your config.

I can now get rid of the various ble listeners I have around the house and replace them with these much nicer looking plugs.

1 Like

Great write-up! I think about converting mine to ESPHome as well. BTW, what do you mean by “legacy format” for ESPHome Firmware?

In the ESPHome interface when you click Install then “Manual Download” it offers you options for either the “Modern format” or “Legacy format”, I selected Legacy.

Thanks! I only used command line for OTA, didn’t know those options from GUI.