Adding a SwitchBot Bot to HomeKit (with Home Assistant)
We will explore the way to add a switchbot to homekit using home assistant
this does not require a SwitchBot Hub (Mini), it uses direct bluetooth communication
On this page
Adding the SwitchBot to home assistant
Make sure you have set up Bluetooth on home assistant (so its configured)
‘securing’ the SwitchBot with a password
before trying to add the SwitchBot I strongly suggest putting a password on the SwitchBot
Pairing with Home Assistant
For me, it showed up ‘automatically’ in home assistant notifications
If not head over to integrations and add SwitchBot
Adding SwitchBot to HomeKit
Now, there are a couple of ways to get the SwitchBot into HomeKit, in my case I use the SwitchBot to buzz myself into my apartment since I have not found time to reverse engineer the proprietary protocols used by our intercom (just to keep myself busy) but then the other expression comes up in my head “if it works it’s not stupid”
anyhow, if your SwitchBot would be turning on a coffee machine I would not worry too much about directly exposing it to HomeKit as a misunderstood command by Siri or an accidental “Hey Siri turn everything on” won’t leave your front door open 😅
currently, in my case, the SwitchBot works a bit funny in Home Assistant, as the toggle buttons don’t nicely swap from “turn on to turn off” since a press on “turn on” does the full motion on the SwitchBot to lower the lever press the button and retract it (I have it configured that way)
so in my case, if I press to turn off it will trigger the SwitchBot and buzz the door, and the same with a turn on.
So what I propose in this situation is to make a “virtual” switch within Home Assistant, expose that to HomeKit and power it with automation to deal with the actual SwitchBot itself
Adding a virtual switch (input boolean) to Home Assistant
You can do this via YAML or via the Helpers (⬅️ suggested way)
Head over to settings > devices & services > (on top of the screen) Helper > (right down corner) > + create helper
Now we have a helper toggle we can use!
Exposing our input_boolean / virtual switch to HomeKit
Now that we have our ‘virtual switch’ lets expose it to HomeKit
As you will see I run my HomeKit configs in inclusion mode, meaning only what I chose will be exposed to homekit (allowing me to keep it clean in HomeKit)
Via YAML
homekit: - name: "HA Utility Bridge" filter: include_entities: - input_boolean.switchbot
Via the UI
Go to your Home Assistant integrations and configure your HomeKit integration
Give Home Assistant a quick restart (since I’m using YAML) and it should show up in your Home
Adding the automation logic to trigger the actual SwitchBot
So there are a couple of things we have to do
- Check if the virtual (HomeKit) switch was turned on
- Toggle the SwitchBot itself (in our case a turn off will trigger the SwitchBot, so will a turn on, so we are OK with a toggle)
- turn the virtual switch back off (so its state is reflected in HomeKit)
- [OPTIONAL] check if the source of the event was HomeKit
alias: SwitchBot description: "" trigger: - platform: state entity_id: - input_boolean.switchbot to: "on" from: "off" condition: [] action: - service: switch.toggle data: {} target: entity_id: switch.bot_a1ac - service: input_boolean.turn_off data: {} target: entity_id: input_boolean.switchbot mode: single
there we have it! when you toggle the switch in HomeKit it will toggle the SwitchBot.
‘Securing’ (🤦🏻♀️) the SwitchBot toggle [advanced]
Since Siri does so well at understanding what we ask of it, I want to avoid triggering our door buzzer, or prevent a ‘Hey Siri turn everything on’ would also trigger the switchbot.
So I have come up with some dumb ‘anti mistake’ solution and it works as follows.
2 virtual switches:
- Start door sequence
- Confirm door sequence
The way I’ve implemented this is as follows:
- If you switch [start door] it will turn on [confirm door]
- after 3 seconds they both turn off again (making it hard for voice usage/voice assistant hacking)
- within those 3 seconds you have to turn OFF [confirm door]
- if [confirmed door] gets turned OFF and [start door] is still on trigger the actual SwitchBot
- turn all switches off
it looks like this in HomeKit
Implementation
You will need 2 input_boolean’s
and I’ve made 3 automations, and I’m sure they can be improved, do leave feedback! 🧡
- Launch opening sequence: toggle confirm button on
- Confirm button: the logic to toggle the SwitchBot
- stop homekit turn on: just turns off the confirm button if you turn it on yourself (by pressing it or voice commands)
[BUZZER LOGIC] Launch opening sequence
The idea is as follows:
If the door buzzer switch (step 1) is turned on for atleast 1 second
and the source was HomeKit and the confirm (step 2) button is off, then turn on the confirm button (step 2)
also turn everything back off after 3 seconds
alias: "[BUZZER LOGIC] Launch opening sequence" description: "" trigger: - platform: state entity_id: - input_boolean.door_buzzer from: "off" to: "on" for: hours: 0 minutes: 0 seconds: 1 condition: - condition: template value_template: "{{ trigger.to_state.context.parent_id == none }}" alias: only run when SOURCE is HOMEKIT (parent == none) action: - if: - condition: and conditions: - condition: state entity_id: input_boolean.door_buzzer state: "on" alias: buzzer ON - condition: state entity_id: input_boolean.door_buzzer_confirm state: "off" alias: confirm OFF alias: check if buzzer is ON and confirm is OFF then: - service: input_boolean.turn_on data: {} target: entity_id: input_boolean.door_buzzer_confirm alias: turn on confirm - delay: hours: 0 minutes: 0 seconds: 5 milliseconds: 0 - service: input_boolean.turn_off data: {} target: entity_id: - input_boolean.door_buzzer - input_boolean.door_buzzer_confirm alias: turn both back off else: - service: input_boolean.turn_off data: {} target: entity_id: - input_boolean.door_buzzer - input_boolean.door_buzzer_confirm mode: single
[BUZZER LOGIC] Confirm button
The idea is as follows: if the confirm switch is turned off and stays of for 1 seconds, check if the source was HomeKit (since we turn off our switch with automations) check if the launch switch (step 1) is still turned on and confirm is turned off (step 2) then we trigger the SwitchBot and turn off all the switches again
alias: "[BUZZER LOGIC] Confirm button" description: "" trigger: - platform: state entity_id: - input_boolean.door_buzzer_confirm from: "on" to: "off" for: hours: 0 minutes: 0 seconds: 1 condition: - condition: template value_template: "{{ trigger.to_state.context.parent_id == none }}" alias: only run when SOURCE is HOMEKIT (parent == none) action: - if: - condition: and conditions: - condition: state entity_id: input_boolean.door_buzzer state: "on" for: hours: 0 minutes: 0 seconds: 0 alias: buzzer ON - condition: state entity_id: input_boolean.door_buzzer_confirm state: "off" alias: Confirm OFF then: - service: input_boolean.turn_off data: {} target: entity_id: - input_boolean.door_buzzer - input_boolean.door_buzzer_confirm - type: toggle device_id: 0fb91ad50d8c35dc82db35d667e6977c entity_id: switch.bot_a1ac domain: switch else: - service: input_boolean.turn_off data: {} target: entity_id: - input_boolean.door_buzzer - input_boolean.door_buzzer_confirm alias: check for BUZZER == on and CONFIRM == off mode: single
[BUZZER LOGIC] stop homekit turn on
This just turns off the confirm button when its turned on by HomeKit (so this way only our automations can turn it on) its not foolproof due delays and stuff, but its sufficient for me
alias: "[BUZZER LOGIC] stop homekit turn on" description: "" trigger: - platform: state entity_id: - input_boolean.door_buzzer_confirm to: "on" condition: - condition: template value_template: "{{ trigger.to_state.context.parent_id == none }}" alias: only run when SOURCE is HOMEKIT (parent == none) action: - service: input_boolean.turn_off data: {} target: entity_id: - input_boolean.door_buzzer_confirm - input_boolean.door_buzzer mode: single