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
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.
- Start by getting an API key.
- If you have two curtains opposite each other covering one window, itās easier if you link them up so they act as one.
- 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
- 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
- You should now have a working cover entity that controls your Switchbot Curtain(s) through the Hub
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
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}}"
Thatās so much better than what I managed to come up with. Very cool 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.
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
Ok, I missed that before.
Seems to be all working now
Only thing missing before I start deom SmartThings is the button clicker and IR
Anyone using that? can share there config?
Thanks guys
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!
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?
Thatās spot-on. Thanks for taking the time to post this.