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

HA tends to do that with auto discovered devices sometimes. reloading the webpage usually fixes it or in rare cases restarting HA

if you deleted the device or cleared mqtt discovery topics you will need to reboot the esp32 or HA/MQTT to get the mqtt discovery messages sent again

Yea, the rest of the sensors appeared 15 mins later. All good now.

It may not work while the ESP32 is busy with switchbot control actions though

I have just tried the code and it does work, but will not work while the esp32 is busy with an action. Could this be fix?

I put in logic so that if an open or motion is missed while controlling a device, it should send the open or motion message after

it uses the last motion/contact trigger time to do this

scanning cant happen while sendinv a BLE command. I am assuming even the switchbot hub cant scan while controlling

I don’t have the contact sensor yet (coming). I think you mentioned in a post that with the contact sensor it is always scanning (blue led is always lit).
I am monitoring mqtt explorer and I notice if the status is scanning, the pir sensor will trigger. Though when there is an action performing, the status changes to idle and the pir does not trigger.

it is working for me, so not sure. if motion or contact sensor are included the esp32 will be idle for only a few milliseconds. it will return to scanning right away after control is done

you will need to wait for control/set to be done to see the open or motion. u may see a delay but there is no way to avoid a delay while controlling

the esp32 will be idle for only a few milliseconds. it will return to scanning right away after control is done

Yes, it will return to idle a few moments after. The only issue I see is if the device is kinda far from the esp32, it will stay at idle for some time until it finds it and preform the action then it will return to scanning.

ya. I will add a ‘controlling’ status. after every retry the status goes to idle, so that is probably confusing you. It isnt actually idle and that is a minor mistake. Shouldnt affect the functionality though

but eitherway. once the action is complete you should get a motion update

I added a v6.6

2 main changes:

  1. Will display “controlling” status when esp32 is busy with a set/control command
  2. Bot device entity type choices. You can decide what HA entity type you want each bot to be
  • Bot switch entity
  • Bot light entity
  • Bot button entity

v6.6 gives you the option to choose entity types for bots: switch, light, or button

use this list…

/* Switchbot Bot Device Types - OPTIONAL */
/* Options include: "switch", "light", "button" */
static std::map<std::string, std::string> allBotTypes = {     // OPTIONAL - (DEFAULTS to "switch" if bot is not in list) - Will create HA entities for device types
 /* { "switchbotone", "switch" },
    { "switchbottwo", "light" },
    { "switchbotthree", "button" }*/
};

Updated. yes, I see what you mean now. It makes more sense with the controlling status.

Hello devWaves,

Thank you very much, now I can control my curtain via HomeAssistant with MQTT
Sorry for my newbie questions,

  1. can I set the curtain open half? or open xx%??
  2. MQTT can use with TLS?? MQTTS? If yes, where to place the ca.crt??

Thank you so much~~~

hey @windmay

yes curtain supports position and has a slider within the HA entity.

currently the espmqttclient library does not natively support tls. It can be done but requires some modifications which arent ready for the released code Add support for SSL/TLS · Issue #32 · plapointe6/EspMQTTClient · GitHub

Hello devWaves,

WoW" Sorry for I missed to enter the entity , ha… I see the slide now

hope the TLS supported version will be soon released

And Thanks again, it’s help me to solve the BLE problem on Synology “no USB support anymore”
Now I can use ESP32 with your Smartbot project to control the Curtain from MQTT

Many Thanks~~

Hello. My contact sensor arrived and installed.

  1. Is it possible to change the state Open/Close → Locked Unlocked?
  2. I noticed contact state changes to TIMEOUT a few minutes after. (device to esp32 distanct issue? RSISI is -80). Answer is here.
  3. I am still confuse on how to use the button. I see from mqtt the button and it changes from idle to pushed. Do I need to create mqtt template sensor to have it show in HA?

Hi, what is the type of module that you used on platformio to upload to Quindors ESP32s? Thanks

I think I just used wemos d1 mini setting. Sorry can’t remember atm.

What I did have to do was find the appropriate serial to usb driver for the ch340c chip that the quindor esp32 uses. I can find that later if you dont have it. The usb serial chip is different between wemos d1 mini and quindors

I probably won’t put that in the native code, but there are only a couple spots in the code it would take you to change. I can send those edits if u want them

There are already 2 button entities created.
button and buttonCount

button is a binary sensor that changes from “idle” to “pushed”. it will only go “pushed” for a few milliseconds

buttonCount is the value that comes from the contact device. In the esp32 code I look for a change in the buttoncount value and if it changes I send a “pushed” message and immediately send an “idle” message after

I can send those edits if u want them

yes, please. Thanks.

button is a binary sensor that changes from “idle” to “pushed”. it will only go “pushed” for a few milliseconds

I got confused when you mentioned idle / pushed, but the binary_sensor.genkan_doorlock_button entity has states of on / off. I figured it out. This is the code I used and it seems to work.

- alias: 'Lights out Before leaving'
  trigger:
    - entity_id: binary_sensor.genkan_doorlock_button
      platform: state
      to: 'on'
  action:
    - service: script.lights_out

Change:

if (!contactA && !contactB) {
          contact = "CLOSED";
        }
        else if (!contactA && contactB) {
          contact = "OPEN";
          lastContacts[aDevice] = millis();
        }
        else if (contactA && !contactB) {
          contact = "TIMEOUT";
        }

to this…

if (!contactA && !contactB) {
          contact = "LOCKED";
        }
        else if (!contactA && contactB) {
          contact = "UNLOCKED";
          lastContacts[aDevice] = millis();
        }
        else if (contactA && !contactB) {
          contact = "LEFT UNLOCKED";  // I discovered after getting a contact sensor that from the documentation "TIMEOUT" = "LEFT OPEN", so you can put "LEFT UNLOCKED" or just "UNLOCKED" here
        }
1 Like