Switch-Bot API integration

Thanks!! I was bummed that I couldn’t use the BT integration since I’m running Hyper-V but this works perfectly. Using “press” mode:

configuration.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 }}","parameter": "{{ parameter }}","commandType": "{{ commandType }}"}'

scripts.yaml

switchbot_aircon:
  alias: Switchbot Aircon
  sequence:
  - service: rest_command.switchbot_device_command
    data:
      deviceId: !secret switchbot_aircon_deviceId
      command: press
      parameter: default
      commandType: command

Hey guys,

I’ve been trying to get the temperature working for my IR aircon.
Besides creating the rest_command and switch in the config I created a climate/generic_thermostat like this:

climate:
  - platform: generic_thermostat
    name: Airco Slaapkamer
    heater: switch.airco_slaapkamer
    target_sensor: sensor.airpurifier_mc1_temperature
    min_temp: 17
    max_temp: 23
    ac_mode: true
    target_temp: 19
    initial_hvac_mode: "off"
    precision: 1

In this particular case I used our air purifier as temperature sensor, but it’s probably best to use a zigbee or BLE temperature sensor instead. I’m still testing it, but so far it seems to work.

Yeah, I’m having the same issue. Right now, I have a card on a dashboard to control the curtains but no way of hooking it into automation. I’m trying to figure out a way of detecting motion outside the window and using that to close the curtain … (beginner here btw)

[quote=“shogun168, post:88, topic:270550, full:true”] I’m trying to figure out a way of detecting motion outside the window and using that to close the curtain … (beginner here btw)
[/quote]

I am using a Homematic Window sensor and call a cover service to open or close the curtains once motion is detected or not. Works pretty well.

1 Like

Limit is now 10.000 calls acording to the documentation

I ended up using this cover template for the Curtain

cover:
  - platform: template
    covers:
      bedroom_curtain:
        device_class: curtain
        friendly_name: "Bedroom Curtain"
        #position_template: "{{ states('sensor.positie_woonkamer_gordijn') }}"
        position_template:  "{% if  (states('sensor.positie_bedroom_curtain') | round | int) == 0 %}
                             100
                             {% else %}
                             {{  100 % (states('sensor.positie_bedroom_curtain') | round | int) }}
                             {% endif %}" # my change using % (mod) to convert the value. if value is 0 it throws error in log about using mod on a value of 0. so place in the static value instead.
        open_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_bedroom_deviceId
            command: "setPosition"
            parameter: "0,ff,0"
        close_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_bedroom_deviceId
            command: "setPosition"
            parameter: "0,ff,100"
        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}}"```

Now I would like to add a webhook trigger to open or close the curtains via an automation. (receive webhook → open/close Curtains) But below code isn’t working, any idea what I’ve done wrong?

service: rest_command.switchbot_device_command
data: 
 deviceId: !secret switchbot_bedroom_deviceId
 command: "setPosition"
 parameter: "0,ff,0"

Any ideas would be welcome!

Fresh pair of eyes and energy on an other day can help…

service: rest_command.switchbot_device_command
data:
  deviceId: REAL DEVICE ID NOT USING !secret
  command: setPosition
  parameter: 0,ff,0

this seems to work, the automation can’t handle !secret it seems

1 Like

Thanks (dank je! :p), I noticed it showed closed when open and the other way around implementing the original solutions (made into an article by a youtuber, who refered back to this topic). Figured Switchbot and HA had a direct reverse interpetation of 0 and 100. This saved me having to do the math myself. :+1: :smiley:

Hey All,

For those looking to integrate the Switchbot Lock, figured I’d share my code (shamelessly stolen and adapted from others). Note that Switchbot API Limit is 10,000/day so this will eat up over a quarter of that:

sensor:
  - platform: rest
    name: "Door Status"
    resource: https://api.switch-bot.com/v1.0/devices/<DeviceID here>/status/
    method: GET
    scan_interval: 30
    headers:
      Authorization: <Paste Authorization Here>
      Content-Type: "application/json"
    value_template: "{{ value_json.body.lockState }}"
    json_attributes_path: "$.body"
    json_attributes:
      - deviceId
      - deviceType
      - hubDeviceId
      - lockState
      - doorState
      - calibrate

rest_command:
  switchbot_device_command:
    url: "https://api.switch-bot.com/v1.0/devices/{{ deviceId }}/commands"
    method: post
    content_type: "application/json"
    headers:
      Authorization: <Paste Authorization Here>
    payload: '{"command": "{{ command }}","parameter": "{{ parameter }}"}'

lock:
  - platform: template
    name: Apartment Door
    value_template: "{{ is_state('sensor.door_status', 'locked') }}"
    lock:
      service: rest_command.switchbot_device_command
      data:
        deviceId: <device ID here>
        command: "lock"
    unlock:
      service: rest_command.switchbot_device_command
      data:
        deviceId: <device ID here>
        command: "unlock"
1 Like

I’d like to add that you can also set up a Webhook to reduce the API calls, if you have a HA Cloud subscription:

alias: SwitchBot Webhook
description: ''
trigger:
  - platform: webhook
    webhook_id: <webhook-id-generated-by-hacloud>
action:
  - service: python_script.hass_entities
    data:
      action: set_state
      entity_id: >
        {{ 'lock.front_door_lock' if (trigger.json.context.deviceMac ==
        '<deviceID>') }}
      state: >
        {{ trigger.json.context.lockState | lower if
        (trigger.json.context.deviceMac == '<deviceID>') }}

I used the hass_entities python script to set the entity states: https://github.com/pmazz/ps_hassio_entities

Switchbot Webhook documentation:

2 Likes

sup guys
love this community, and this discussion is just another good example of it :slight_smile:

need some assistance. for some reason my “open” and “close” buttons are not working properly if the blinds are completely “close” or “open”.
let me elaborate.
buttons, sensor, and even the slider to control the switchbot - works great. however, if the blinds are 100% “open”, for some reason the “open” button on the dashboard is the only one that stays “pressable” (yet pressing it does nothing) while the “close” button is grayed out, and cant be pressed.
the “stop” button is useable as “close” button.
for now, i “reversed” the actions of the buttons - (close button actually open the blinds, and vice versa) but that cant be the “right” way to go…

i’d love some help, if anyone have any experience with this, or knows whats need to be done to fix it…
cheers

The API and HA are reversed how they see 100 and 0. There is a post in this topic how to adapt your code for this. I’m not behind a computer else I would look it up. Scroll through the topic and you will find it.

thanks for the pointer, i did try it, but it didnt work for me. not sure what i did wrong. so for now, im back to my “reverse definitions”, as the “open” button closes the blinds and vice versa. not ideal, but its fine. ill add a costume card with some buttons for better UI experience, and for the automations it doesn’t really matter anyway.

With the new HA release and renewed Bluetooth support I can now find my curtainbots without having to the use the API route. With me they don’t respond yet (but show status correctly), probably due to the distance. It may be worth investigating for you as if the distance isn’t to big. Will prevent a lot of tinkering with the API.

When you say they don’t respond yet, are you seeing errors? I can add them but cannot control them even if the Pi is very close to them. I opened a ticket but not sure if this is an isolated issue to my Pi.

There is a full topic about this now. It’s not just you (or me). And it also seems the password is not needed, anyone can just add a curtainbot.

Hi Guys,

I can’t get my device ID… Someone can help ?

I use this command in powershell:
curl.exe -H “Authorization: xxxxxxx” https://api.switch-bot.com/v1.0/devices/
(where xxxxxxx is my api i got in the app)

but i get this error:

“curl: (35) schannel: next InitializeSecurityContext failed: Unknown error (0x80092012) - La fonction de révocation n’a pas pu vérifier la révocation du certificat.”

Can anyone tell me what i’m doing wrong ?

thanks

EDIT:
Finally found out after hours of searching and trying (and failing).
Just needed to add “–ssl-no-revoke” at the end of the powershell command.

So it must look like this:
curl.exe -H "Authorization: YOUR_API" https://api.switch-bot.com/v1.0/devices/ --ssl-no-revoke

Just want to add my2cent, maybe helpful for others.
I use a switchbot hub mini sto remote control my air condition. As there is no integration that uses the api to control the hub, I found a workaround, that works good for me.
I set up different scenes in the hub mini for the air condition, then I set up webhook actions in IFTTT to start these scenarios.
With the IFTTT integration in HA it’s pretty easy to initiate these Szenarios now from inside HA.
I defined an input_select with all Szenarios as well as an automation which sends IFTTT triggers, based on the input_select.
I now can even select the input_select from within HomeKit incl. Siri and control my air condition.
Perfect for me.

Are you able to lock and unlock using that code? I received the state but cannot operate the motor…

Thanks!

I found the answer by myself… The lock/unlock function have been remove from the API by switchbot for “security reason”. On github they mentionned they work on a new version to reopen the function…