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

The contact sensor button will have 2 entities auto created for the button

  1. buttonCount = a rotating number of the number of button pushes

  2. button = binary sensor possible values “IDLE” and “PUSHED”

the contact sensor data only provides the button count, but in the code I made it so if the button count changes that will create a ‘PUSHED’ action for a binary sensor

Hey guys,

The integration has been working pretty well lately except 2 things:

  1. I’ve noticed 1 of my 2 curtains sometimes open itself when not requested. I saw another user posting about the same issue few days ago. It happens very rarely, maybe once a week and upon further investigation I noticed it usually happens after it goes unavailable. I tried moving the ESP32 to a different location (within 3 meters) thinking it might be clashing with other BLE devices nearby. I’ll see if it helps.

  2. On both curtains, yesterday for 1 and today for the other curtain I noticed they just didn’t respond anymore for some reason (tested open). When checking the chip it was quite hot and the blue and red light were both on (see screenshot). Couldn’t find anything suspicious in the HA or MQTT (Supervisor Logs). Is there any other type of log I could check see what the issue was?

It worked right away after unplugging/replugging the power cable and sending the open command.

It’s been running fine overall though, much better than the API integration!!

hey @notownblues. So my curtains don’t have any issues like that

  1. My only suspicion is that the mqtt set message is being retained. whenever the esp32 connects to the mqtt broker it subscribes to the ‘set’ topic of each device. If a message is retained on a topic it receives this message the same way as a new message and will process it

you can test this behaviour by shutting down the mqtt broker and restarting it. when the esp32 reconnects it will subscribe to the topics

the fact thay it only happens to one curtain also hits that it is a retained mqtt message for that curtain

you would want qos = 0 also

  1. the red led light is power. that will always be on. the blue led light will be on anytime the esp32 is busy doing something. if you only have curtains (no motion or contact sensors) the blue light will turn off and should only be on when a ‘set’ command is received and when it is scanning. if you have contact or motion sensors the blue led light will be on all the time because it is scanning all the time

when scanning the blue light will stay on until it finds the device. the blue light will also stay on while a ‘set’ message command is being performed

if you want to see the serial output of the esp32 from your pc you can set printSerialOutputForDebugging = true in the code to see more output

1 Like

First of all: thank you for this fantastic project. I have installed it and it is up and running. With a main topic of switchbot and a host name of esp32bot. For me it looks like it is up and running and communicating with my SwitchBot. But I can’t seem to understand what to publish to control it.

I have tried sending the payload PRESS and ON to the topic switchbot/esp32bot/bot/switchbotone/set

But nothing happens.

@eirikso what u have looks correct. does your bot have a password? if yes remember to add it in the code

if you are using HA though you shouldnt have to manually send MQTT messages. the code supports MQTT discovery so you should see devices show up automatically in HA

you can watch the bot topic to see the status of commands sent. if they were sent, and successful

 <ESPMQTTTopic>/bot/<name>/status
                    - <ESPMQTTTopic>/curtain/<name>/status
                    - <ESPMQTTTopic>/meter/<name>/status
		
                    Example payload:
                      - {"status":"connected", "command":"ON"}
                      - {"status":"errorConnect", "command":"ON"}
                      - {"status":"errorCommand", "command":"NOTVALID"}
                      - {"status":"commandSent", "command":"ON"}
                      - {"status":"busy", "value":3, "command":"ON"}
                      - {"status":"failed", "value":9, "command":"ON"}
                      - {"status":"success", "value":1, "command":"ON"}
                      - {"status":"success", "value":5, "command":"PRESS"}

you should see 3 messages.

  1. connected 2. commandSent 3. success

I’m currently just testing, so that’s why I do this manually in MQTT Explorer. I was sure I did not set a password in my bot, but was wrong. So once I added the password to the code now and uploaded everything works fine!

Thank you again!

Hello @devWaves

First of all thanks for all the past help. I now have two ESP32 you sent me privately.
For the moment, because I only have meters, I use an ESPHome implementation I’ve found to get temperature, humidity, battery and RSSI.
And now you provide a platform.io code, it is really easier to build using command line only which is perfect for me.

Do you think I could have some benefits to migrate to your solution from ESPHome?
When (or if) Switchbot release BLE documentation for humidifier I would surely migrate, for now I would loke to know if it could change anything, especially on Home Assistant side.

If I migrate, is there a way to choose host name for the ESP32? It is the case with ESPHome which is really good.
Do you think it could be possible to add mdns to access the ESP32? Which would be very good for OTA, avoiding using IP address.

Yep you were right all along @devWaves, it just opened on its own again now and after checking MQTT logs I can see the last open message being retained.

Do you know the possible reasons why it’s being retained and how to prevent that?

@notownblues the ‘state’ topic message is supposed to be retained.

what you want to look at is the ‘set’ topic, where a command is sent. This should not be retained

the esp32 only subscribes to ‘set’. the esp32 does not subscribe to ‘state’ it only publishes to state

hey @Nardol

since meters only require to read data, the esphome setup should provide the same or similar results as my esp32. The esp32 code is setup to scan on a set interval for meters, where the esphome setup may scan all the time. I do not know exactly

where you will currently benefit is if you want to control a device like bot, curtain, and hopefully eventually humidifier

people are working on esphome BLE control but I don’t believe it is fully supported yet

if/when esphome does support switchbot, it may or may not support retrying to send a command when it fails. My esp32 code supports retrying when it fails. retrying on failure requires reading the response from the switchbot device. Not sure if esphome would do that or not

my esp32 code can also deal with multiple commands sent to it at once and will process them in order received, this may be tricky with esphome

yes host name can be set and host name is the first variable to set in the esp32 code

I have mdns coded, but cant confirm it works atm. I will have to try later since I always use the IP address out of habit

1 Like

Thanks for your reply @devWaves

I just had a look and the open command looks like it’s being retained on the set topic too. Not sure why it does that, the upstairs bot seems to work better with the integration.

there are a bunch of ways mqtt messages can be set to retained so youll want to figure out what did it

once a message is retained it it always retained until cleared. you can clear a mqtt topic by sending a empty string payload to it and set retained = true

if it goes to retained again, then something is doing it

1 Like

Hey mate,

Having the same LED_PIN issue. How did you fix that?

Also what’s the best setting for an Ali Express special ESP32dev?

Thanks all

EDIT;

Ok, so I just put this is the top of the cpp and it works :slight_smile:

#define LED_BUILTIN 2

This is awesome! Finally not having to wait for the API to kick over and its proper local!

Thanks for this, great little project :slight_smile:

ya there is a comment at the line where it fails in the code for LED_BUILTIN

different esp32s have different led pins (esp32cam is pin 33)

glad u got it working though. cheers

Thanks for that :slight_smile:

It will be interesting, I have a bunch of ESP32s running either ESPHome or WLED and they drop off a fair bit sadly. Im not sure if its the software or hardware. So its going to be interesting to see the stability with this set up.

Do you find it ever drops off the wifi?

I was previously running a d1 mini esp32 and it was solid, no disconnects

I have a few more switchbot products now and for a month now I have been running one of Quindors ESP32s with external antenna and it has also been solid quindor esp32 with external antenna

I’ve got a bunch of Ali Express modules. But its just to hard to know if you order something more expensive, are you getting the same thing and they are just ripping you off. Or if you’re actually getting a betting product.

Anyway, we will see.

But second day and it seems great. After going from the API to local, the response is a big difference.

So thanks again for this :slight_smile:

I think you have mentioned this awhile back on how to fix this, but I cannot find the post. This is my meter, but it has the bot sensors. What are the steps to fix it so that is gets pick up as the meter?

deleting the device and rebooting the esp32 may fix it

or you probably have retained mqtt discovery messages on the homeassistant/# topic with the meter MAC address but for bot sensors

an easy way to clear all retained messages is to reinstall mosquitto mqtt, or you can the clear them individually by sending an empty string payload with retain = true

deleting the device and rebooting the esp32 may fix it

I did the first and it is showing as meter now with just one sensor, but the other 3 are available just not under mqtt device shown below. Maybe there is a delay?