Switch-Bot API integration

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.

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?

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:
3 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}}"
3 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.

1 Like

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”?

It would be great if someone could explain how to add one of the switches…it’s the only thing that’s keeping me from turning off my Smartthings hub!

2 Likes

I stumbled upon this thread while looking for a solution to my SwitchBot Switch.

Re-configuring @gdeboos work, I modified it to handle a single SwitchBot Switch that operates my coffee machine:

secrets.yaml

switchbot_api: "<YOUR_API_KEY>"
switchbot_deviceId: "<YOUR_DEVICE_ID>"

configration.yaml

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 }}"}'

switch:
  - platform: template
    switches:
      ki_coffee_machine:
        friendly_name: Coffee Machine
        turn_on:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_deviceId
            command: "turnOn"
        turn_off:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_deviceId
            command: "turnOff"

Note that in the above example ki_coffee_machine is what I named my SwitchBot entity.

Basically, I just cleaned the code for all the curtain specific stuff. I’m not smart enough to tell whether or not a template is even needed at this point but I’m satisfied that it works well for now.

Maybe this is what you’re looking for, @t34wrj?

4 Likes