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.