Switch-Bot API integration

Hello,

Switch-Bot recently released an API: https://github.com/OpenWonderLabs/SwitchBotAPI

It allows to control all Switch Bot devices: the bots, meters, curtains, Humidifier, plugs and also the virtual appliances and scenes.

Could it be possible to add a new Switch Bot integration based on this API?
It would avoid only using Bluetooth which adds some constraints and moreover would allow to control more Switch-Bot devices natively.

I would second this, I have quite a few of them :slight_smile:

3 Likes

I agree to use the API, but I think it shouldnā€™t replace bluetooth completely.

I have four SwitchBot Meters. Since the number of the API calls is limited to 1000/day, I reach it when I obtain the value for each meter every 5 minutes. This limitation is tight because I might add more meters and need to retain the calls for other SwitchBot devices.

Therefore, in my implementation, I try Bluetooth first, and then call API if Bluetooth fails. By this way, unnecessary API calls are suppressed and API calls are used for only devices that Bluetooth cannot reach.

1 Like

Yes I completely agree with using api. Would be great if an official integration option existed like that.
I currently have to use smarthings integration as a workaround to control Switchbot with HA (Switchbot is integrated sith smarthings)
But Smarthings is only syncing automatically once every 6hours with Switchbot which is annoying especially for thermomters

1 Like

Yes but with Bluetooth we have distance problem. I cannot get all my meters temperature and humidity using bluetooth.
I totally agree the rate limit is bad for meters, looks like Switch-Bot have not taken them in account when deciding it. The best for that is to hop for them to create a local API, with no rate limit or to contact them to suggest to have a higher limit.

If it was possible to use the BLE mesh network Switch-Bot use Bluetooth could be used without problem for me. But it is not the case or I have not found how to do so.

Iā€™m trying to use the API through curl to control my curtains. Bit messy, but should do the trick. This is what Iā€™m sending to close one of the curtains

curl -X POST -H "Content-Type: application/json" -H "Authorization: $TOKEN" -d "{\"command\": \"setPosition\", \"parameter\": \"0,ff,100\", \"commandType\": \"command\"}" https://api.switch-bot.com/v1.0/devices/$DEVICEID/commands

The API responds with

{"statusCode":100,"body":{},"message":"success"}

Butā€¦nothing happens at all. Iā€™ve tried turnOn/turnOff and setPosition. The API sends a ā€˜successā€™ response everytime, without anything happening. Any ideaā€™s on whatā€™s going wrong?

2 Likes

Nevermind. A simple reboot of the hub fixed it

Alright, I managed to get my SwitchBot Curtains hooked up through the API. Hereā€™s is a quick tutorial for anyone thatā€™s interested.

  1. Start by getting an API key.
  2. If you have two curtains opposite each other covering one window, itā€™s easier if you link them up so they act as one.
  3. Find out the deviceID of the curtain you want to control by running the following command on the Windows command line. In the output you should see the identifier of your curtain
curl -H "Authorization: API_KEY_HERE" https://api.switch-bot.com/v1.0/devices
  1. Open up your configuration.xml and add the following
cover:
  - platform: command_line
    covers:
      switchbot_curtains:
        command_open: "/usr/bin/curl -X POST -H 'Content-Type: application/json' -H 'Authorization: API_KEY_HERE' -d '{\"command\": \"turnOn\"}' https://api.switch-bot.com/v1.0/devices/DEVICE_ID_HERE/commands"
        command_close: "/usr/bin/curl -X POST -H 'Content-Type: application/json' -H 'Authorization: API_KEY_HERE' -d '{\"command\": \"turnOff\"}' https://api.switch-bot.com/v1.0/devices/DEVICE_ID_HERE/commands"
        command_stop: "/usr/bin/curl -X POST -H 'Content-Type: application/json' -H 'Authorization: API_KEY_HERE' -d '{\"command\": \"turnOff\"}' https://api.switch-bot.com/v1.0/devices/DEVICE_ID_HERE/commands"
        friendly_name: Switchbot Curtains
  1. You should now have a working cover entity that controls your Switchbot Curtain(s) through the Hub :slight_smile:
5 Likes

Thanks for this! I was struggling with range so using the API has worked a treat.
Although based off your work I was the able to set up using a template cover.

And I have managed to get setPosition working as well :smile:

secrets.yaml

switchbot_api: "<YOUR_API_KEY>"
switchbot_bedroom_status_url: "https://api.switch-bot.com/v1.0/devices/<YOUR_DEVICE_ID>/status"
switchbot_bedroom_deviceId: "<YOUR_DEVICE_ID>"

configuration.yaml

  • rest comand to open / close/ set position
  • re-useable for multiple devices
rest_command:
  switchbot_device_command:
    url: 'https://api.switch-bot.com/v1.0/devices/{{ deviceId }}/commands'
    method: post
    content_type: 'application/json'
    headers:
      Authorization: !secret switchbot_api
    payload: '{"command": "{{ command }}","parameter": "{{ parameter }}"}'
  • rest sensor for curains position
sensor:
  - platform: rest
    name: 'Bedroom Curtain Position'
    resource: !secret switchbot_bedroom_status_url
    method: GET
    scan_interval: 600
    headers:
      Authorization: !secret switchbot_api
      Content-Type: 'application/json'
    value_template: '{{ value_json.body.slidePosition }}'
    json_attributes_path: "$.body"
    json_attributes:
      - deviceId
      - deviceType
      - hubDeviceId
      - calibrate
      - group
      - moving
      - slidePosition
  • template cover using the above
cover:
  - platform: template
    covers:
      bedroom_curtains:
        device_class: curtain
        friendly_name: "Bedroom Curtains"
        position_template: "{{ states('sensor.bedroom_curtain_position') }}"
        open_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_bedroom_deviceId
            command: "turnOn"
        close_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_bedroom_deviceId
            command: "turnOff"
        stop_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_bedroom_deviceId
            command: "turnOff"
        set_cover_position:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_bedroom_deviceId
            command: "setPosition"
            parameter: "0,ff,{{position}}"
7 Likes

Thatā€™s so much better than what I managed to come up with. Very cool :slight_smile: I would increase the scan_interval though, thereā€™s a 1200 requests/day limit. With as 60 sec interval, youā€™ll cross that.

Good shout. Iā€™ll change it to every 10 minutes in that case as I have a few of them as well.

This is awesome guys! thank you! Canā€™t wait to test

Does anyone have a template for the button and temp sensor?

Iā€™m trying to get my devicelist with

curl -H ā€œAuthorization: API_KEY_HEREā€ https://api.switch-bot.com/v1.0/devices

But I keep getting a unauthorized messageā€¦But i am sure the api key is correct. Did they change the version or does it take some time for the api key to be validated?

OK tried this one:

curl ā€œhttps://api.switch-bot.com/v1.0/devicesā€ -H ā€œauthorization: YOUR_TOKEN_KEYā€

and that worked!

For Switchbot Meter I added next lines.

secrets.yaml

switchbot_meter1_status_url: "https://api.switch-bot.com/v1.0/devices/<YOUR_DEVICE_ID>/status"

configuration.yaml

sensor:
#<Curtains Position and other sensors here>
  - platform: rest
    name: 'Meter1 JSON'
    resource: !secret switchbot_meter1_status_url
    method: GET
    scan_interval: 600
    headers:
      Authorization: !secret switchbot_api
      Content-Type: 'application/json'
    value_template: '{{ value_json.body }}'
    json_attributes_path: "$.body"
    json_attributes:
      - deviceId 
      - deviceType
      - hubDeviceId
      - humidity
      - temperature
  - platform: template
    sensors:
      switchbot_meter1_temp:
        friendly_name: "Meter1 Temperature"
        value_template: '{{ states.sensor.meter1_json.attributes["temperature"] }}'
        unit_of_measurement: "Ā°C"
        device_class: "temperature"
      switchbot_meter1_humidity:
        friendly_name: "Meter1 Humidity"
        value_template: '{{ states.sensor.meter1_json.attributes["humidity"] }}'
        unit_of_measurement: "%"
        device_class: "humidity"

If you have several Meters copy lines and change Meter1 to Meter2.

2 Likes

Thanks mate,

I still havenā€™t restarted while Iā€™m waiting on a fix lol

But as soon as it comes though, ill give this a go.

is anyone using this the button presses?

Thanks again :slight_smile:

Ok, I missed that before.

Seems to be all working now :slight_smile:

Only thing missing before I start deom SmartThings is the button clicker and IR

Anyone using that? can share there config?

Thanks guys :slight_smile:

This is great everyone. Iā€™m having a hell of a time trying to get switchbot curtains integrated and this gets me very close. I followed the configuration provided by @gdeboos and i see the integration and can interact with it. However, it only closes the shades and the open is greyed out. Also, when using the slider, the switchbot kinda clicks and then does nothing if setting it to completely closed. Additionally, the Curtain position never changes, even closed its stating 100.

Any help would be much appreciated.

Hey,

I donā€™t know anything about the %, but ignore the grayed out button, click it and it works. Had me stumped for a bit as well LOL.

Are you using the button pressers?

The slider seems to work and the open (up arrow) button but the close (down arrow) doesnt seem to work. Additionally when opening with the arrow button, the position (slider) doesnt change so i have to slide it back and forth for it to be in the right position

I see different configs hereā€¦ which is the one that would work best? Any chances of a structured summary as in ā€œthis goes in secrets.yaml and this in config.yamlā€?