Support for SwitchBot Plug Mini (W1901400) with bluetooth proxying

I am running Home Assistant on a Raspberry Pi 4.

It looks like you were right! I loaded up HassOS on a virtual machine, installed the ESPHome add on, created and uploaded the firmware with your config and it seems to be working now.

Hopefully an update will come out that will allow this to work on arm processors.

Thanks for your help.

Has anyone figured out a way to hard reset these after getting Esphome loaded? I must have screwed something up in the yaml causing 2 of my plugs to not connect to my home wifi. I can see it has defaulted to its own AP but connecting to that does not give out an IP. I tried manually setting my device to something in the 192.168.4.x network but I cannot ping 192.168.4.1.

Other than figuring out how to open the plug up I’m guessing its bricked?

The partition table that is generated here switchbota/part.csv at b622e9096dcbfd76caaa33ef2293c4743b4ce57f · kendallgoto/switchbota · GitHub might not be large enough for the Bluetooth proxies so the performance might be slow connecting to devices unless you do a serial flash

Ah darn, is there any way to modify the partition table if we compile the espressif stuff from the switchbota repo?

Or is the part.csv file in that repo mirroring the actual partition scheme that’s already there?

Here is the partition table that esphome uses when you select esp-idf:

https://github.com/esphome/esphome/blob/6b7b0768755b21cca0e4b67515de93b9ca7b3821/esphome/components/esp32/__init__.py#L404

Is it possible to apply that partition table via an OTA flash? Or is it only doable over USB/serial?

Esphome only changes it via serial flash. It may be possible to do it with another tool but that’s probably a bit more risky and you may end up opening it up anyways.

What’s the concern with the partition table? Is there something that isn’t working correctly?

Looking at the two different tables it looks like the OTA partitions in the switchbota table are actually slightly bigger (although it’s been a while since last doing hex conversions so I might have mixed that up).

Anyway, mine seem to be working as bluetooth proxies (I have a lot of bluetooth proxies around the house so haven’t tested them exclusively though).

ESPHOME NVS SIZE = 446464 bytes
nvs, data, nvs, , 0x6d000,

The partition table linked above has an NVS of only 24K

If you only have a few bluetooth devices you will never notice but if you have a lot some of them will be slower as you’ll run out of space to cache the service lists.

I found a nice PR that enables web_server component on the esp-idf frame work so now the web server is running well on these plugs too. Hopefully it’ll get merged into mainline esphome soon, but for now it’s easy to add as an external component. I’ll update the top post but here is what you need to add to your config:

external_components:
  - source: github://pr#3500
    components:
      - web_server
      - web_server_idf
      - web_server_base

web_server:
  port: 80
  auth:
    username: !secret web_user
    password: !secret web_pass

Interesting note on the size of the NVS partition. I’ve got a few plugs I haven’t flashed yet so I may try to see if I can change it during the conversion process.

1 Like

The web_server pr#3500 was merged two weeks ago. Do I just remove the whole external_components section?

yes, but leave web_server: in

Great that it’s finally in mainline! I tested it successfully and updated the top post to remove the external component. Should now work without it from ESPHome 2023.7.0 on.

Note I’m successfully using this project to ble proxy a bunch of Xiaomi LYWSD03MMC similar to the linked article, except I get to use esphome on the switchbot plug instead of tasmota, woo.

1 Like

Thank you for the post on this!! Working great for me.
I do have a suggestion for an edit to your .yaml at top to include the ${name} in the sensors and switches, otherwise they all have no-so-useful names as entities. See my adaption of your config below:

substitutions:
  name: "switchbot1"
  friendly_name: "SwitchBot1"

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"

web_server:
  port: 80
  auth:
    username: !secret web_user
    password: !secret web_pass

# 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

# Enable Bluetooth Proxying
esp32_ble_tracker:
  scan_parameters:
    interval: 1100ms
    window: 1100ms
    active: true

bluetooth_proxy:
  active: true

switch:
  - platform: gpio
    name: "${name} Relay"
    id: "${name}_relay"
    device_class: outlet
    pin: 6
    restore_mode: RESTORE_DEFAULT_OFF
    on_turn_on:
      then:
        - light.turn_on: "${name}_white_led"
    on_turn_off:
      then:
        - light.turn_off: "${name}_white_led"

binary_sensor:
  - platform: gpio
    internal: true
    pin:
      number: 2
      mode: INPUT_PULLUP
      inverted: true
    name: "${name} Button"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - switch.toggle: "${name}_relay"
      
light:
  - platform: binary
    internal: true
    name: "${name} white led"
    id: "${name}_white_led"
    output: "${name}_white_output"
    
output:
  - id: "${name}_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: "${name} Current"
    voltage:
      name: "${name} Voltage"
    power:
      name: "${name} Power"
    energy:
      name: "${name} Energy"
    update_interval: 1s
    change_mode_every: 4


1 Like

Do you happen to know if it would be safe/possible to update the switchbota switchbota part.csv to that ESPHome partition table you linked? I believe this would depend on Tasmota supporting it which I’m not sure would be in your domain of expertise, but I thought I’d ask.

We spoke briefly about this on discord.

I haven’t used the SwitchBot conversion tool so I can’t offer a safe tested way to modify it.

The goal is to make sure that the NVS partition is large enough that the ESPHome proxy won’t run out of space caching services which would make reconnection to Bluetooth devices up to 4-5x slower (or worse)as services would have to be re-resolved every connection because the cache is out of space.

Just to confirm, this is a working ESPHome config for the SwitchBot W1901400 plugs?

Correct, that’s the model I’ve been working with.

Thank you!