SwitchBot bot/curtain/meter/contact/motion MQTT ESP32 bridge - Local control

that node red flow was for a previous version. the github main page has the mqtt info. that would be up to date. if you are using HA and you just want to press, MQTT discovery is available and creates the devices for you in HA

with just MQTT for a press you would use this topic

<ESPMQTTTopic>/bot/<name>/set

payload

"PRESS"

<ESPMQTTTopic> = <mqtt_main_topic>/<host>

  - Default = switchbot/esp32
1 Like

Aaaah thank you, that did the trick

cool. there is also a holdPress now, which I donā€™t think switchbot hubs even support

Hi. Great work with the code. Was working well with HA. However I reconfigured MQTT in HA but now curtains are not being discovered. Other devices like Tasmota ones required a manual trigger to readd them. Is there a mqtt command I can send for the switchbot esp32?

rebooting the esp32 resends the HA discovery stuff

Thanks but that was first thing I did. I also tried renaming devices and mqtt names in code. They show up in mqtt explorer on reboot of esp32 but not discovered in HA so likely a HA issue. I guess for now can manually add mqtt devices in HA.

Is this going to reach the 1k comments? :wink: Thanks once again for your great work devWaves.

Hope your still not tired.

that would be cool

no problem. I enjoy this stuff. Iā€™ve also used my share of free community stuff, so I am just giving back

3 Likes

Worked great, I was able to convert it to platformio support with just some simple function defines at the top of the sketch and changed extension to cpp.

One thing that was really throwing me off, was in the MQTT user and password section, it mentions to set the user to empty if you dont use auth with MQTT.

I did set it to an empty string, bu that DOES NOT work. It required me to set it to NULL. There is a differenceā€¦

yes you can also use plaformio as mentioned

with my testing, I have 2 MQTT brokers setup. the HA mosquito one with username/password and also a standard mosquito install with no authentication

when I use my basic MQTT broker I just leave the username password as the default ones, the broker doesnā€™t seem to care what I put. I think the documentation use to just say ignore it

If it is giving you issues though Iā€™ll make some changes so empty string converts to NULL or something in the next release

works for me though. cheers

Since a couple people asked for some features I released a v5.1

You can now simulate ON/OFF for bots in PRESS mode using these 3 lists in the code

/*** Bots in PRESS mode to simulate ON/OFF - ESP32 will try to keep track of the ON/OFF state of your device while in PRESS mode***/
// Add bots while in PRESS mode that will simulate ON/OFF. Default state will be used if no MQTT retained on state topic
// false = default state = OFF
// true = default state = ON
static std::map<std::string, bool> botsSimulateONOFFinPRESSmode = {
  /*{ "switchbotone", false },
    { "switchbottwo", false }*/
};

//Add bots OFF hold time for simulated ON/OFF, if not in list, the current hold value will be used. Device must be in botsSimulateONOFFinPRESSmode list
static std::map<std::string, int> botsSimulatedOFFHoldTimes = {
  /*{ "switchbotone", 3 },
    { "switchbottwo", 10 }*/
};

//Add bots ON hold time for simulated ON/OFF, if not in list, the current hold value will be used. Device must be in botsSimulateONOFFinPRESSmode list
static std::map<std::string, int> botsSimulatedONHoldTimes = {
  /*{ "switchbotone", 15 },
    { "switchbottwo", 1}*/
};
/********************************************/

If your deviceā€™s ON/OFF press action hold time would be the same for ON/OFF, simply include the devices in then botsSimulateONOFFinPRESSmode list

If you deviceā€™s ON/OFF uses a different hold time for ON vs OFF, also add your devices to botsSimulatedOFFHoldTimes and/or botsSimulatedONHoldTimes. So when On is pressed it will hold for X seconds, when Off is pressed it will hold for Y seconds

If the state is incorrect, or you want to control the state of the device, you can manually call the set function for ā€œSTATEOFFā€ or ā€œSTATEONā€ to correct the state

when the ESP32 boots, it will attempt to grab the last retained state message to set the bot state in ESP32

Note: If you physically push the button (not using switchbot), the ON/OFF state will be wrong. You will need to call set ā€œSTATEOFFā€ or ā€œSTATEONā€ to correct the state

I try to use this great project! i set everything and flashed my nodemcu. HA shows my curtain and also detects when he is op or closed. But when i push open or close there will nothing happened.

Are u using v5 or v4?

what is your LinkQuality Rssi value that shows up in HA

I am now at version 5.1 i have a link quality of -91 rssi, itā€™s to weak i think?

yes that could be the reason. -91 is not a good signal. place the esp32 close to your curtain then try again

i will try that!

v5.2 released

A couple fixes for simulated ON/OFF

so it looks like switchbot released code for their motion and contact sensor. I donā€™t own those but am willing to look into making some changes to support them

send me a message if you have either of those and want to test. I havenā€™t coded the changes yet, but I will try to take a look this weekend

Hey everyone,

first of all thank you for this great development.
i am not familiar with esp32 and this is my first project to integrate the switchbot curtain into HA. but iā€™m stuck.

I bought an ESP32 Devkit V1 and followed the steps on this page https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32.
I think I managed to get the esp32 installed with arduino IDE. (step 2).

(I did manage to do the test with the board so that the led blinks and I ran a sample program to scan for the WiFi network).

Then I was able to add the different libraries in the arduino IDE, but I donā€™t know if this is also on the board. However, I canā€™t find any settings or configuration anywhere to adjust the wifi/mqtt and such as mentioned in step 8ā€¦ So I think I donā€™t fully understand a number of things.

Can someone help me describe the steps in more detail? Or give me a hint how to set everything up?

thank you very much in advance!

you need to edit the configurations in the code for the file SwitchBot-BLE2MQTT-ESP32.ino

the configurations are all under this line in the code

/****************** CONFIGURATIONS TO CHANGE *******************/

and stop when you see this lineā€¦

/********** ADVANCED SETTINGS - ONLY NEED TO CHANGE IF YOU WANT TO TWEAK SETTINGS **********/

all of the lists are empty with example code commented out in the middle to show you how to use it

example: if you are only using one bot change thisā€¦

/* Switchbot Bot Settings */
static std::map<std::string, std::string> allBots = {
  /*{ "switchbotone", "xX:xX:xX:xX:xX:xX" },
    { "switchbottwo", "yY:yY:yY:yY:yY:yY" }*/
};

to thisā€¦

/* Switchbot Bot Settings */
static std::map<std::string, std::string> allBots = {
  { "switchbotname", "a8:y6:8r:r4:8u:99" }
};