Switch-Bot API integration

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

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?

6 Likes

That’s spot-on. Thanks for taking the time to post this.

Would this work with the temp and humidity meter and the humidifier?

According to the API documentation, the limit is 1000 times per day, not 1200.
That is the biggest issue of this API and there is no answer from Switch-Bot about that, same about a local API.
But for sure, the problem is the same with 1000 or 1200.

I have build a solution using Azure Service Bus and an app function.
I wanted to build a central cloud solution so i’m able to control multiple curtains from multiple HA installations at different locations.

From HA a message is send to the queue, and the app functions behind the queue, sends the message to the switch bot API. The message contains the same data as the API would.

The only thing i have to do if a new curtain is installed on a location, is register an token and configure the queue in the new HA installation.
The cost for Azure is allmost nothing. Around 10 eurocents per month using one curtain twice a day.

an example of my solution

alias: Gordijnen - Woonkamer
description: ''
trigger:
  - platform: state
    entity_id: input_number.curtain_position
condition:
  - condition: state
    entity_id: input_boolean.curtain_automatic
    state: 'on'
action:
  - service: notify.azure_curtain_queue
    data:
      message: CurtainService
      title: CurtainService
      data:
        custom_object:
          authorization: '{{ states(''input_text.switchbottoken'') }}'
          device_id: '{{ states(''input_text.switchbotcurtain1_deviceid'') }}'
          index: '0'
          mode: 0
          position: '{{states(''input_number.curtain_position'')  |int }}'
mode: single

In the future I want to integrate the other devices like switches.

1 Like

Hello there, I was wondering… does this needs and switchbot Hub or not? I guess it does… I don’t have a Bluetooth on my HA since its a VM, not sure what are my solutions here without the hub.

Is there a way to use a esp32 Bluetooth to control this?

Thanks!

Thanks in advance.

Swichbot using ESP32 and MQTT

3 Likes