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

thanks for this dev. long time HA user, but noob ESP32 and MQTT. I have everything working and the ESP32 is perfectly reliable for my application

but i can’t get curtain position to work

NOT working for me:

      - service: mqtt.publish
        data_template:
          topic: switchbotMQTT/control
          payload: '{"id":"curtainone","value":"50"}'

The open/close/pause curtain functions work fine with the same topic, id and payload format. any clues? is the curtain position buggy?

Also on the ESP32 - what is the purpose of the port 80 upload file webserver? can we \give it a compiled binary in case of config changes rather than plugging in the USB?

cheers

did u try v0.18? 0.17 had a bug in position. I did = instead of == in the code. fixed in v0.18

yes. ota works with .bin file so u dont need to unplug

for OTA update the port 80 is the default port so u just need to go to your esp32 ip address in a web browser. if u use a port other than 80, put :port after the ip address

awesome. fixed. sorry for posting without checking latest.
note - a trick for the noobs, you might be missing an end comment */ after the new password fields { “switchbottwo”, “switchbottwoPassword” } . not using passwords in my case, so failed first compile as i didn’t use/check new password area.

More advice please -

With HA, I’m just using simple script to fire the MQTT… so im not getting any status like battery or position. im new to MQTT… whats the best practice config for HA? is there an automation to check battery and curtain position? if so, how?

script:
  upstairs_ac_toggle:
    alias: Upstairs AC Toggle
    sequence:
      - service: mqtt.publish
        data_template:
          topic: switchbotMQTT/control
          payload: '{"id":"switchbottwo","value":"press"}'
  curtain_open:
    alias: Curtain Open
    sequence:
      - service: mqtt.publish
        data_template:
          topic: switchbotMQTT/control
          payload: '{"id":"curtainone","value":"open"}'

thanks. I fixed the missing end comment tag

to get battery etc you can make another mqtt call to switchbotMQTT/requestInfo or switchbotMQTT/rescan. messages will be published per device

for curtain look for an mqtt msg on switchbotMQTT/curtain/curtainone

the payloads are the same for both. requestinfo is for one device. rescan is for all devices

so you can call a requestinfo like 1-10 seconds after every control call, or a rescan every 30 min. or both

Just updated to v.18. Quick test. Seems ok. Thanks.

Here is how I control my bot and curtain devices. The script is basically a template that will be call from your xx amount of curtain or bot devices. Each time you call your bot, it will also update the data info in mqtt.

Switchbot Bot
This should cover for xx amount of bots you have setup. You only need to create the script.yaml once.
For the switch.yaml, create one for how ever many bots you have changing the bot name. Some goes with the input_boolean.yaml.
The delay was trial and error on my part. Adjust to your liking.
Note: I have changed the MQTT topic to all lower caps. You will need to edit that if you left as is.

Update: As of v.19+, we no longer need to maunally call requestInfo to update the bot data. I have removed what is not needed any longer.

script.yaml

  enable_bots_tpl:
    sequence:
      - service: mqtt.publish
        data:
          topic: 'switchbotmqtt/control'
          payload_template: "{\"id\":\"{{ bot }}\", \"value\":\"press\"}"

  switchbot_update_device:
    sequence:
      - service: mqtt.publish
        data:
          topic: 'switchbotmqtt/requestInfo'
          payload_template: "{\"id\":\"{{ bot }}\"}"

switch.yaml

  - platform: template
    switches:
     24h_fan:
       friendly_name: '24h Fan'
       value_template: "{{ states('input_boolean.washroom_fan') }}"
       turn_on:
         - service: script.enable_bots_tpl
           data:
             bot: '24hr_fan'
         - service: input_boolean.turn_on
           entity_id: input_boolean.washroom_fan
       turn_off:
         - service: script.enable_bots_tpl
           data:
             bot: '24hr_fan'
         - service: input_boolean.turn_off
           entity_id: input_boolean.washroom_fan
       icon_template: >-
         {% if is_state('input_boolean.washroom_fan', 'on') %}
            mdi:fan
         {% else %}
            mdi:fan-off
         {% endif %}

input.boolean.yaml

  washroom_fan:
    name: Washroom Fan
    icon: mdi:fan

Curtain Bot
Create one script for each curtain devices you have. This script will work for open, close, stop, & position. The delay was trial and error on my part. Adjust to your liking.

script.yaml

  enable_cr_curtains_tpl:
    sequence:
      - service: mqtt.publish
        data:
          topic: 'switchbotmqtt/control'
          payload_template: "{\"id\":\"{{ bot }}\", \"value\":\"{{ modus }}\"}"

### If you are onlying running one device for your curtain and not a combo, you can delete the below. Otherwise, change the bot name to your second device. ###
      - delay: 15
      - service: script.switchbot_update_device
        data_template:
          bot: 'cr_curtain_rt'

cover.yaml

  - platform: template
    covers:
      computer_room_curtain:
        device_class: curtain
        friendly_name: "Computer Room Curtain"
        value_template: >-
          {% if states('sensor.cr_curtain_left_position')|float < 90 %}
            open
          {% else %}
            closed
          {% endif %}
        open_cover:
          service: script.enable_cr_curtains_tpl
          data:
            modus: "open"
            bot: 'cr_curtain_lt'
        close_cover:
          service: script.enable_cr_curtains_tpl
          data:
            modus: "close"
            bot: 'cr_curtain_lt'
        stop_cover:
          service: script.enable_cr_curtains_tpl
          data:
            modus: "pause"
            bot: 'cr_curtain_lt'
        set_cover_position:
          service: script.enable_cr_curtains_tpl
          data:
            modus: "{{ position }}"
            bot: 'cr_curtain_lt'
        icon_template: >-
          {% if is_state('cover.computer_room_curtain', 'open') %}
            mdi:blinds-open
          {% else %}
             mdi:blinds
          {% endif %}

sensor.yaml

  - platform: mqtt
    name: "CR Curtain Left Position"
    state_topic: 'switchbotmqtt/curtain/cr_curtain_lt'
    value_template: '{{ value_json.serviceData.pos }}'
    unit_of_measurement: "%"
    icon: mdi:format-list-numbered
3 Likes

thanks for that

Thank you for the updates. I found a small bug with regard to OTA.
If you go to ip/serverIndex you can bypass login/password

That makes sense. I copy pasted from the simple OTA arduino example online and just added the choice to change the default username/password

I don’t think many people would be concerned with that, but I could be wrong. Easiest solution would be to remove/disable OTA to make it secure. I added OTA simply because I realized people were probably tucking the ESP32 away somewhere

@ devWaves
Has the bot always trigger for your automation or has it missed a few times?

my coffee has always worked. i think my printer missed once. it is far though. using default 60/30

My presser and the curtains are in the same room and noticed the presser has not missed an automation, but the curtain has missed a few times.

you can increase the attempts to connect and see if that helps. if you notice the missed attempt are caused by a crash then that would be something that I would need to look into. I havent noticed it crashing. watch for a “boot” status on switchbotmqtt/esp32, that means it crashed

I am not monitoring via mqtt so I don’t know the status when the automation failed trigger. When it crashes, does it auto reboot or the esp will not work until you manually unplug/plug back in to the power?

If that is the case, than I don’t think it crashed, because the next set automation works.

if it crashes, it auto reboots and does the normal initial rescan

did you notice failed sends before OTA v0.14? hopefully not cause by the ota implementation

I can’t say for sure prior the .16, it may have failed at that time as well would be my guess.

try 100 connect attempts. see if that helps

I havent used the HA integration, but with the switchbot app I have been standing right next to it and it wouldnt work sometimes. So it could just be switchbots.

I do publish to mqtt when the command was sent to tge bot (after connect), that basically means it should have worked. you can check for that msg. if u dont get it, send again

but with the switchbot app I have been standing right next to it and it wouldnt work sometimes.

My experience is the opposite, but I have only tested the curtain with the app. It works all the time with quite long delays sometimes for me.

with the app, once the initial connection works, then I can call it non stop and it works instantly if i push it over and over. but ive had that initial connection timeout ( the circle runs out).

I have it attempting to connect 60 times by default. a higher value should technically help (if it isnt crashing)

ill do a couple tests to see if i can reproduce a failure

Hi,

Thanks for this, few teething issues with getting it compiled and uploaded to the board, but can now communicate with the switchbot switch. What I am finding is that when I send an ‘on’ command, it does press the rocker switch on, but it returns back to its normal position which pulls the switch to the off position. What should I be using to ensure that the switch does not try and return back to it’s starting position?