Switch-Bot API integration

Charging and/or solar panel connection would be awesome too :smiley:

I’m getting this error in the log when trying to use your mod:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1286
First occurred: 14:24:53 (4 occurrences)
Last logged: 14:24:53

  • Template warning: ‘round’ got invalid input ‘unknown’ when rendering template ‘{% if (states(‘sensor.livingroom_curtain_position’) | round | int) == 0 %} 100 {% else %} {{ 100 % (states(‘sensor.livingroom_curtain_position’) | round | int) }} {% endif %}’ but no default was specified. Currently ‘round’ will return ‘unknown’, however this template will fail to render in Home Assistant core 2022.1
  • Template warning: ‘int’ got invalid input ‘unknown’ when rendering template ‘{% if (states(‘sensor.livingroom_curtain_position’) | round | int) == 0 %} 100 {% else %} {{ 100 % (states(‘sensor.livingroom_curtain_position’) | round | int) }} {% endif %}’ but no default was specified. Currently ‘int’ will return ‘0’, however this template will fail to render in Home Assistant core 2022.1

Any ideas of how to fix?

How did you name your sensor?

I have this working with my switchbot switch, but has anyone gotten the status to work with the switch? I’m still pretty new to home assistant and I’m not sure where to add the api status check.

Edit:

I think I have this working, someone please let me know if there is a better way to do this. Here is my config.yaml.

Looks like they updated their API to allow 10,000 polls a day so I have my single switch checking every 10 seconds. I’m also leaving out my secrets.yaml because it’s the same as the examples above.

@BlueWave this might solve your question as well.

#Switchbot
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 }}"}'
    
sensor:
  - platform: rest
    name: 'switch_status'
    resource: !secret switchbot_status_url
    method: GET
    scan_interval: 10
    headers:
      Authorization: !secret switchbot_api
      content_type: "application/json"
    json_attributes_path: "$.body"
    value_template: '{{ value_json.body.power }}'
    json_attributes:
      - deviceId
      - deviceType
      - hubDeviceId
      - power


switch:
  - platform: template
    switches:
      buttkicker_power:
        friendly_name: Buttkicker Power
        value_template: "{{ is_state('sensor.switch_status', 'on') }}"
        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"

I saw some questions of people who are having another Switchbot curtain in another room, as I have too… I did not see any reponses. Can someone help with this code? Thanks a lot!

Something like this?

sensor:
  - platform: rest
    name: 'Positie Woonkamer Gras Gordijn'
    resource: !secret switchbot_woonkamer_gras_gordijn_status_url
    method: GET
    scan_interval: 30
    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    
  - platform: rest
    name: 'Positie Woonkamer Water Gordijn'
    resource: !secret switchbot_woonkamer_water_gordijn_status_url
    method: GET
    scan_interval: 30
    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  
cover:
  - platform: template
    covers:
      woonkamer_gras_gordijn:
        device_class: curtain
        friendly_name: "Woonkamer gras gordijn"
        #position_template: "{{ states('sensor.positie_woonkamer_gordijn') }}"
        position_template:  "{% if  (states('sensor.positie_woonkamer_gras_gordijn') | round | int) == 0 %}
                             100
                             {% else %}
                             {{  100 % (states('sensor.positie_woonkamer_gras_gordijn') | 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_woonkamer_gras_gordijn_deviceId
            command: "setPosition"
            parameter: "0,ff,0"
        close_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_woonkamer_gras_gordijn_deviceId
            command: "setPosition"
            parameter: "0,ff,100"
        stop_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_woonkamer_gras_gordijn_deviceId
            command: "turnOff"
        set_cover_position:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_woonkamer_gras_gordijn_deviceId
            command: "setPosition"
            parameter: "0,ff,{{position}}"
  - platform: template
    covers:
      woonkamer_water_gordijn:
        device_class: curtain
        friendly_name: "Woonkamer water gordijn"
#        position_template: "{{ states('sensor.positie_woonkamer_water_gordijn') }}"
        position_template:  "{% if  (states('sensor.positie_woonkamer_water_gordijn') | round | int) == 0 %}
                             100
                             {% else %}
                             {{  100 % (states('sensor.positie_woonkamer_water_gordijn') | 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_woonkamer_water_gordijn_deviceId
            command: "setPosition"
            parameter: "0,ff,0"
        close_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_woonkamer_water_gordijn_deviceId
            command: "setPosition"
            parameter: "0,ff,100"
        stop_cover:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_woonkamer_water_gordijn_deviceId
            command: "turnOff"
        set_cover_position:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_woonkamer_water_gordijn_deviceId
            command: "setPosition"
            parameter: "0,ff,{{position}}"
1 Like

perfect! thanks!

I bought a new Switchbot LED Lightstrip and created an entry for the REST sensor.

  - platform: rest
    name: "Switchbot Lightstrip"
    resource: !secret switchbot_Lightstrip_status_url
    method: GET
    scan_interval: 5
    headers:
      Authorization: !secret switchbot_api
      Content-Type: "application/json"
    value_template: "{{ value_json.body }}"
    json_attributes_path: "$.body"
    json_attributes:
      - deviceId 
      - deviceType
      - hubDeviceId
      - power
      - brightness
      - color

Can somebody please support me in creating a template in order to control the light especially the color?

I’ve further worked on a template for the lighstrip.
Turn on/off is now working

Only two things are still not working. Can somebody please give a hint why i can not adjust brightness and color?

light:
  - platform: template
    lights:
      switchbot_lightstrip:
        friendly_name: Lichtstreifen Signe Büro
        unique_id: Lichtstreifen Signe Büro
        turn_on:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "turnOn"
        turn_off:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "turnOff"
        set_level:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "setBrightness"
            parameter: "{1-100}"
        set_color:
          service: rest_command.switchbot_device_command
          data:
            deviceId: !secret switchbot_lightstrip_deviceId
            command: "setColor"
            parameter: "{0-255}:{0-255}:{0-255}"

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