Switch-Bot API integration

Hello. I didn’t find sample codes/ automations for the Switchbot humidifier in this thread. Here is my setup if anyone is interested. I have used some of the codes here to call API and for the automations, helpers and sensors, I used @ tom_l lounge heating and cooling automations.

Paste these codes and adjust to your HA environment. Best to restart HA since there are quite a few of codes. Hope I didn’t missed anything.

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 }}" }'

# humidifier sensors
rest:
  - resource: !secret switchbot_wohumi_waterlevel_url
    method: GET
    headers:
      Authorization: !secret switchbot_api
      Content-Type: 'application/json'
    scan_interval: 600 # adjust this to suite your need. There is api limit of 10,000 /per day.
    sensor:
      - name: SB Humidifier Lack Water
        value_template: "{{ value_json.body.lackWater }}"
        device_class: carbon_dioxide
      - name: SB Humidifier Nebulization Efficiency
        value_template: "{{ value_json.body.nebulizationEfficiency }}"
        device_class: ozone 
      - name: SB Humidifier Temperature
        value_template: "{{ value_json.body.temperature }}"
        device_class: temperature 
      - name: SB Humidifier Humidity
        value_template: "{{ value_json.body.humidity }}"
        device_class: humidity

secret.yaml

switchbot_api: '2ac4f4dxxxxxxxx6a0718ec'
switchbot_wohumi_waterlevel_url: 'https://api.switch-bot.com/v1.0/devices/<devicID>/status'
switchbot_wohumi_id: '<deviceID>'

input_select.yaml

  sb_humidifier_mode:
    name: SB Humidifier Mode
    icon: mdi:air-humidifier
    options:
      - 'Off'
      - Auto
      - Low
      - Med
      - High

input_number.yaml

  lrh_humidity_low_set:
    name: Humidity Low Set Point
    icon: mdi:thermometer
    initial: 30
    min: 0
    max: 100
    step: 1

  lrh_humidity_high_set:
    name: Humidity High Set Point
    icon: mdi:thermometer
    initial: 50
    min: 0
    max: 100
    step: 1

  lrh_humidity_intensity_set:
    name: Humidifier Intensity
    icon: mdi:water-plus
    min: 34
    max: 100
    step: 1

input_datetime.yaml

  lrh_ac_am_on_time:
    name: Humidifier AM On Time
    has_time: true
    initial: '05:00'
  lrh_ac_am_off_time:
    name: Humidifier AM Off Time
    has_time: true
    initial: '06:00'
  lrh_ac_pm_on_time:
    name: Humidifier PM On Time
    has_time: true
    initial: '18:30'
  lrh_ac_pm_off_time:
    name: Humidifier PM Off Time
    has_time: true
    initial: '19:00'

input_boolean.yaml

  lrh_humidifier_automation_enable:
    name: Humidifier Automation
    icon: mdi:av-timer
    initial: off

binary_sensor.yaml

      lrh_humidifier_am_automation_time_active:
        friendly_name: "LRH Humidifier AM Automation Time Active"
        value_template: >-
          {% set d = now().strftime("%Y-%m-%dT") %}
          {% set tz = as_timestamp(now()) | timestamp_custom('%z') %}
          {% set t = now().timestamp() %}
          {% set am_start = as_timestamp(d ~ states('input_datetime.lrh_ac_am_on_time') ~ tz) %}
          {% set am_end = as_timestamp(d ~ states('input_datetime.lrh_ac_am_off_time') ~ tz) %}
          {{ am_start <= t <= am_end }}

      lrh_humidifier_pm_automation_time_active:
        friendly_name: "LRH Humidifier PM Automation Time Active"
        value_template: >-
          {% set d = now().strftime("%Y-%m-%dT") %}
          {% set tz = as_timestamp(now()) | timestamp_custom('%z') %}
          {% set t = now().timestamp() %}
          {% set am_start = as_timestamp(d ~ states('input_datetime.lrh_ac_pm_on_time') ~ tz) %}
          {% set am_end = as_timestamp(d ~ states('input_datetime.lrh_ac_pm_off_time') ~ tz) %}
          {{ am_start <= t <= am_end }}

script.yaml

  humidifier_low:
    sequence:
      - service: rest_command.switchbot_device_command
        data:
          deviceId: !secret switchbot_wohumi_id
          command: 'setMode'
          parameter: 101
        
  humidifier_med:
    sequence:
      - service: rest_command.switchbot_device_command
        data:
          deviceId: !secret switchbot_wohumi_id
          command: 'setMode'
          parameter: 102

  humidifier_high:
    sequence:
      - service: rest_command.switchbot_device_command
        data:
          deviceId: !secret switchbot_wohumi_id
          command: 'setMode'
          parameter: 103

  humidifier_auto:
    sequence:
      - service: rest_command.switchbot_device_command
        data:
          deviceId: !secret switchbot_wohumi_id
          command: 'setMode'
          parameter: auto
          
  humidifier_off:
    sequence:
      - service: rest_command.switchbot_device_command
        data:
          deviceId: !secret switchbot_wohumi_id
          command: 'turnOff'
          parameter: default

  lrh_humidity_intensity_set:
    sequence:
      - service: rest_command.switchbot_device_command
        data:
          deviceId: !secret switchbot_wohumi_id
          command: 'setMode'
          parameter: "{{ states('input_number.lrh_humidity_intensity_set') }}"

automation.yaml

- alias: 'SB Humidifier'
  initial_state: true
  trigger:
    platform: state
    entity_id: input_select.sb_humidifier_mode
  action:
  - service_template: >
      {% if is_state('input_select.sb_humidifier_mode', 'Low') %} script.humidifier_low
      {% elif is_state('input_select.sb_humidifier_mode', 'Med') %} script.humidifier_med
      {% elif is_state('input_select.sb_humidifier_mode', 'High') %} script.humidifier_high
      {% elif is_state('input_select.sb_humidifier_mode', 'Auto') %} script.humidifier_auto
      {% elif is_state('input_select.sb_humidifier_mode', 'Off') %} script.humidifier_off
      {% endif %}


- alias: 'SB Humidifier Intensity'
  initial_state: true
  trigger:
    platform: state
    entity_id: input_number.lrh_humidity_intensity_set
  condition:
  - condition: template # Only if currently On
    value_template: "{{ is_state('input_boolean.sb_humidifier', 'on') }}"
  action:
  - service: script.lrh_humidity_intensity_set


- alias: 'SB Humidifier AM Schedule'
  trigger:
    - platform: state
      entity_id: binary_sensor.lrh_humidifier_am_automation_time_active
      to: 'on'
      for:
        seconds: 10
  condition:
    - condition: template # Only if automation is enabled
      value_template: "{{ is_state('input_boolean.lrh_humidifier_automation_enable', 'on') }}"
    - condition: template # Only if currently Off
      value_template: "{{ is_state('input_select.sb_humidifier_mode', 'Off') }}"
    - condition: numeric_state 
      entity_id: sensor.sb_humidifier_humidity
      above: input_number.lrh_humidity_low_set
      below: input_number.lrh_humidity_high_set
    - condition: template # Water tank not empty
      value_template: "{{ is_state('sensor.sb_humidifier_lack_water', 'False') }}"
    - condition: template # Someone is home
      value_template: "{{ is_state('input_select.home_mode', 'Home') }}"
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.sb_humidifier_mode
      option: High


- alias: 'SB Humidifier PM Schedule'
  trigger:
    - platform: state
      entity_id: binary_sensor.lrh_humidifier_pm_automation_time_active
      to: 'on'
      for:
        seconds: 10      
  condition:
    - condition: template # Only if automation is enabled
      value_template: "{{ is_state('input_boolean.lrh_humidifier_automation_enable', 'on') }}"
    - condition: template # Only if currently Off
      value_template: "{{ is_state('input_select.sb_humidifier_mode', 'Off') }}"
    - condition: numeric_state 
      entity_id: sensor.sb_humidifier_humidity
      above: input_number.lrh_humidity_low_set
      below: input_number.lrh_humidity_high_set
    - condition: template # Water tank not empty
      value_template: "{{ is_state('sensor.sb_humidifier_lack_water', 'False') }}"
    - condition: template # Someone is home
      value_template: "{{ is_state('input_select.home_mode', 'Home') }}"
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.sb_humidifier_mode
      option: High


- alias: 'SB Humidifier AM Off'
  trigger:
    - platform: state
      entity_id: binary_sensor.lrh_humidifier_am_automation_time_active
      to: 'off'
  condition:
    - condition: state # Only if AM automation is enabled
      entity_id: automation.sb_humidifier_am_schedule
      state: 'on'
    - condition: template # And only if humidifier is on
      value_template: '{{ states("input_select.sb_humidifier_mode") != "Off" }}'
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.sb_humidifier_mode
      option: 'Off'

- alias: 'SB Humidifier PM Off'
  trigger:
    - platform: state
      entity_id: binary_sensor.lrh_humidifier_pm_automation_time_active
      to: 'off'
  condition:
    - condition: state # Only if PM automation is enabled
      entity_id: automation.sb_humidifier_pm_schedule
      state: 'on'
    - condition: template # And only if humidifier is on
      value_template: '{{ states("input_select.sb_humidifier_mode") != "Off" }}'
  action:
    service: input_select.select_option
    data_template:
      entity_id: input_select.sb_humidifier_mode
      option: 'Off'

This is how my lovelace looks like. Manual controls and automations are here. This is also taken from @ tom_l. I used decluttering-card. Make sure you have this installed and setup. You can do it without it, just follow what is in ui-lovelace.yaml and input the entities in lovelace.yaml below. The Humidifier Auotmation switch is what will turn on your am/pm automations. Make sure it is in the on position.

ui-lovelace.yaml

  humidifier_tpl:
    card:
      type: 'custom:vertical-stack-in-card'
      cards:
        - type: glance
          title: '[[title]]'
          state_color: false
          show_name: false
          entities:
            - entity: '[[entity_1]]'
              name: Humidifier
            - entity: '[[entity_2]]'
              name: Temperature
            - entity: '[[entity_3]]'
              name: Humidity
            - entity: '[[entity_4]]'
              name: Efficiency
        - entities:
            - head:
                label: Automation
                type: section
              items:
                - input_boolean.[[room]]_humidifier_automation_enable                                              
                - input_datetime.[[room]]_ac_am_on_time
                - input_datetime.[[room]]_ac_am_off_time
                - input_datetime.[[room]]_ac_pm_on_time
                - input_datetime.[[room]]_ac_pm_off_time
                - input_number.[[room]]_humidity_low_set                
                - input_number.[[room]]_humidity_high_set              
                - input_number.[[room]]_humidity_intensity_set
              type: 'custom:fold-entity-row'
          show_header_toggle: false
          type: entities

lovelace.yaml (file inside of lovelace folder)

  - type: custom:decluttering-card
    template: humidifier_tpl
    variables:
      - title: Humidifier Controls
      - room: lrh
      - entity_1: input_select.sb_humidifier_mode
      - entity_2: sensor.sb_humidifier_temperature
      - entity_3: sensor.sb_humidifier_humidity
      - entity_4: sensor.sb_humidifier_nebulization_efficiency
1 Like