devWaves / SwitchBot-MQTT-BLE-ESP32

Allows for multiple SwitchBot bots and curtains to be controlled via MQTT sent to ESP32. ESP32 will send BLE commands to switchbots and return MQTT responses to the broker. Also supports Temperature, Motion, Contact sensors
MIT License
549 stars 68 forks source link

Switchbot Curtain intermittently going unavailable for less than a second #107

Open ahaverty opened 1 year ago

ahaverty commented 1 year ago

I have an esp32 (AZ-delivery) working well with two switchbot curtains (paired). It's less than a metre away from the curtain bots.

I haven't actually noticed any functional issues yet, but I can see it's registering as unavailable a few times throughout the day. The link quality seems good, and I'm not having any other network issues that'd cause this as far as I'm aware.

I'm actually not 100% on how best to debug this, I can't see any logs relating to esp/Mqtt in home assistant.

Any help debugging would be much appreciated!

image image
devWaves commented 1 year ago

This is caused by the MQTT "offline" message being received on the lastwill topic

Are u using the default MQTT user Id or did you create one specifically for switchbot? if you didnt create one for the esp32 switchbot I would suggest trying that

ahaverty commented 1 year ago

Thanks @devWaves I do have this esp32 setup on a dedicated user already, named "switchbot" in MQTT (Mosquitto Broker) unfortunately. Would you have any advise on debugging further? Cheers

ahaverty commented 1 year ago

Here's a snippet of the mqtt info which I believe proves I'm on the right switchbot user(?).

In my MQTT setup:

The ring devices have always seemed solid. Eufy on the other hand isn't as solid, and having a look at the last 24hrs, there is one short 'unknown' state on the Eufy device, within 60 seconds of the Switchbot's last 'unknown' state too. Perhaps Eufy's lastwill is hitting switchbot too somehow?

image
devWaves commented 1 year ago

This doesn't exactly show that you are using a dedicate switchbot userId/password. It just shows you are using the 'switchbot' topic

Do you have a new HA account created and you configured these values in the esp32 code ?

static const char* mqtt_user = "YOURUSERID";                         //  MQTT Broker username. If empty or NULL, no authentication will be used
static const char* mqtt_pass = "YOURPASSWORD";                         //  MQTT Broker password

the issue is that something is happening with MQTT that disconnects the esp32 temporarily. Sometimes a fresh MQTT install can also fix random issues. This would also happen if the esp32 looses wifi temporarily

ahaverty commented 1 year ago

I am using a MQTT user/password unique to this switchbot esp32 yes.

I can't exactly rule the wifi out I suppose, but it is sitting about a metre away from the nest wifi, and I haven't noticed any issues on other wifi devices. Considering homeassistant is seeing the device as closed -> unavailable -> closed again in under 2 seconds, does that sound like it could be a physical/wifi connection issue to you?

I'd happily pull some logs off either the esp or MQTT if it'd help. Any particular logs/files you think would be worth checking, please let me know. Thanks for the help!

devWaves commented 1 year ago

You said ypu are using a nest wifi? do ypu have multiple routers in a mesh? it might be disconnecting from one and connecting to the other

not sure if you van with the nest wifi, but some/most mesh routers you can bind a mac address to only connect to one of the routers

another option is to change this line of the code

client.enableLastWillMessage(lastWill, "offline");

to this

client.enableLastWillMessage(lastWill, "expire");

HA is expecting 'offline' to be sent. You can manually send that yourself if you haven't heard from a device in awhile. This will stop the unavailable devices

ahaverty commented 1 year ago

I do have a nest mesh (1 nest router, 1 nest 'puck' and an older google wifi)! And this esp is sat fairly in the middle of the main nest router, and the google wifi puck. I'll have a look around for binding to one of them, thanks for sharing suggestions @devWaves !

ahaverty commented 1 year ago

Binding to a particular router seems to be more of a client concern, at least for Nest's system.

@devWaves feel free to tell me to get lost, but would you happen to know if I could slap in a BSSID on this line for one of my routers?: https://github.com/devWaves/SwitchBot-MQTT-BLE-ESP32/blob/main/PlatformIO%20Files/SwitchBot-BLE2MQTT-ESP32/src/SwitchBot-BLE2MQTT-ESP32.cpp#L4269

I see it's an optional param, and from what I understand, it would only connect to the BSSID I specify: https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/station-class.html#begin

Perhaps a feature addition (adding an optional BSSID to the config) for this repo for others on Mesh networks. I don't mind being a guinea pig if you think that's worth trying (I should get time this week).

devWaves commented 1 year ago

hey. no problem. most likely that is the cause then. so I did see that bssid setting, but the line of code you mentioned is only really a starter for the wifi (somewhat redundant). Feel free to try it out though and see if it helps. The wifi connection is actually maintained by the espmqttclient library. Maybe worth asking them to update and add bssid as an option https://github.com/plapointe6/EspMQTTClient

and then I can update code from there