Switch-Bot API integration

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.

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

1 Like

Thanks ! Up and running.

He wout,

Your curtain tutorial worked great !

i have made some scene in my switchbot hub for IR use en I want to trigger it from HA , i’m a newbie in HA and i can’t get it to work . do you ha a tutorial for senes aswel ?

kind regards
Ivan

I’m afraid I don’t. I switched over to an ESP32 based solution to control the curtains without having to use the cloud

Im finding the curtains are not reporting their status. They are always reporting closed, has anyone else seen this? Due to the scan interval? Curtains still work though.

  - platform: rest
    name: 'Living Room Curtain Position'
    resource: !secret switchbot_living_room_curtain_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

Also not sure if I missed this somewhere, but the switches also work similar to the curtains.

sample code, adjust to fir your own config.yaml

  - platform: template
    switches:
      aircon_switch:
        friendly_name: "AirCon Switch"
        unique_id: aircon_switch
        turn_on:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_aircon_deviceId
            command: "turnOn"
        turn_off:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_aircon_deviceId
            command: "turnOff"

Has anyone had luck using the IR remote feature to make a dumb Dyson Fan swing or set a timer? I have managed to get it working using the on and off functionality but that is it.

2 Likes

nice! could be used to turn on/off a tv? how did you do it?

I used the API as above and took out the Device ID’s and added them into the secrets.yaml

switchbot_dyson_status_url: "https://api.switch-bot.com/v1.0/devices/<Device ID>/status"

switchbot_dyson_deviceId: "<Device ID>"

I then added that as a switch in the configuration.yaml

switch:
  - platform: template
    switches:
      dyson_switch:
        friendly_name: Dyson Fan
        turn_on:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_dyson_deviceId
            command: "turnOn"
        turn_off:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_dyson_deviceId
            command: "turnOff"

I have tested this to turn my TV on and off as well and it does work!

The only reason i have not used it so far is because i can’t get the additional commands to work. Such as swing on the Dyson Fan or change channel etc on the TV.

Does anyone have any suggestions?

2 Likes

I am finding the same issue with the reporting back closed. Has anyone found a fix for this yet?

Not sure if this will help, but check the API doco. They may pass though the custom commands, other then just on and off.

Hi, after playing with the switchbot API documentation and just adapting what we know it works, I was able to call custom IR commands doing something like this:

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 }}"}'
  switchbot_irdevice_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": "default", "commandType": "customize"}'

I add a second res_command function that pretty much only change the payload for custom IR commands, they need “commandType” set as “customize”…probably the first function can be improve to deal with this…

So, as a simple test I add a “fake switch” to trigger the custom IR command…

switch:
  - platform: template
    switches:
...other switches...
      bedroom_tv_qmenu:
        friendly_name: Bedroom TV QMenu
        turn_on:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_bedroomtv_deviceId
            command: "Q.Menu"
        turn_off:
          service: rest_command.switchbot_irdevice_command
          data:
            deviceId: !secret switchbot_bedroomtv_deviceId
            command: "Q.Menu"

and it works!..TV shows the quick menu, which is a custom command of my bedroom tv IR device …Of course this is not the proper way since each custom button will be an “On/Off” switch, …but from here some additional templates-entities can be write to add all the custom IR commands that you want to call…

…hope it helps…

…like following some androidtv integration tutorials for HA, they show how to build custom scripts that later can be added as buttons to produce a sort of remote control…adapting that is posible to do:

switchbot_bedroom_tv_qmenu:
  sequence:
  - service: rest_command.switchbot_irdevice_command
    data:
      deviceId: !secret switchbot_bedroomtv_deviceId
      command: "Q.Menu"
  mode: single
  alias: Bedroom TV Quick Menu

Did anyone ever figure out why one arrow is greyed out and how to fix? It seems the only way to fully control the curtains is by dragging the “position” slider from 0 to 100. I have automations controlling the slider this way, but it would still be nice for the buttons to work

I was never able to get it to work properly through the integration. However, my smartthings hub was able to integrate it that way. It’s not the cleanest way but it worked for me.