Switch-Bot API integration

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.

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

3 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…

3 Likes

…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

1 Like

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.

same problem for me, anyone have a solution?

This works great, I’ve been able to work around the close button not working as expected by using a custom slider for the entity card (HACS), however when my curtains are closed it mention open in HA and when the curtains are open it mention close in HA. I’ve been searching however I’ve not been able to find a solution to set the cover positions in a cover template.

Thank you.

I have two curtains covering the same window. What is the process to link them as mentioned in step 2?

Is this via the app or changing the code in the cover ?

Has anyone been able to use to API to pull from a contact sensor in real time? i.e. instant (or near to) status updates when a door is open.

1 Like

They have updated the rate limit to 10000 last month

I will receive my curtains devices in a couple of weeks and will be looking to hook them up via the API seen some examples above but also have created custom components before, if it is not already out there i might look in to creating one for Switch-bot curtains.

Wonderfull to integrate the switchbot curtains via API. There a two things i could not solve:

  1. Anyone succeeded in creating an automation with de switchbot? Is it done by Service? Entity’s are not possible with automation (i would like to use the Sun Trigger!)
  2. I have two differtent windows with two bots. How to aggregate them in code?

For those of you struggling with one of the buttons for the curtains being greyed out: I solved it. It has to do with the slider position. In Home Assistant a slide position of 100 means fully opened and a slide position of 0 is fully closed. In the SwitchBot API it’s the exact opposite: a slide position of 100 is fully closed and 0 is fully opened. The trick is quite simple: subtract the SlidePosition value you get with the API from 100. You have to do the same with setting the position: subtract the value from the slider in HA from 100 and pass the result as a parameter to the SwitchBot API.

As I noticed that the curtains do not always report 0 or 100 if they have been fully opened or closed, but e.g. 1 or 99, I also fixed that, by using an if-statement in the sensor template. Furthermore, I left out the stop_cover command, because the API doesn’t support that.

I used the templates from @gdeboos. This is what the SwitchBot part of my configuration.yaml looks like after my modifications:

# Switchbot setup
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 }}"}'

sensor:
  - platform: rest
    name: 'Positie gordijnen balkon'
    resource: !secret switchbot_balkon_status_url
    method: GET
    scan_interval: 600
    headers:
      Authorization: !secret switchbot_api
      Content-Type: 'application/json'
    value_template: >
      {% if value_json.body.slidePosition > 95 %} 0
      {% elif value_json.body.slidePosition < 5 %} 100
      {% else %} {{100-value_json.body.slidePosition}}
      {% endif %}
    json_attributes_path: "$.body"
    json_attributes:
      - deviceId
      - deviceType
      - hubDeviceId
      - calibrate
      - group
      - moving
      - slidePosition

cover:
  - platform: template
    covers:
      gordijnen_balkon:
        device_class: curtain
        friendly_name: "Gordijnen balkon"
        unique_id: curtain.balkon
        position_template: "{{states('sensor.positie_gordijnen_balkon')}}"
        open_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_balkon_deviceId
            command: "turnOn"
        close_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_balkon_deviceId
            command: "turnOff"
        set_cover_position:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_balkon_deviceId
            command: "setPosition"
            parameter: "0,ff,{{100-position}}"


As you can see HA reports the position of the curtain for the balcony as 100 (fully opened), while the sensor read-out via the API for SlidePosition is 0.

7 Likes

image
Curtain fully opened, so slider position in HA at 100 (corresponding with a SwitchBot slide position of 0).

1 Like

image
Curtains partially open (70 percent open), so both arrow buttons can be clicked/tapped upon. BTW: up is open, down is close. :slight_smile:

1 Like

Does anyone have a template for the Color Bulb?
Please

2 Likes

Everything i try, the curtains won’t react to the commands in home assistant. Even when i open/close the curtains with the switchbot app, it is not displayed in the history in Home assistant.

Also the meter isn’t working
afbeelding

don’t know what i’m doing wrong. out of idea’s.

But what i see at your pictures looks great.