Automatically unlock your door when getting in bluetooth range

My new Switchbot Lock Pro doesn’t have an auto-unlock (yet) like for example Nukis have, which was quite a bummer for me. So I thought about creating my own version of the auto-unlock, including Telegram notifications and bluetooth presence detection with Bluetooth-Proxies. The automation is set up, so that you can easily add all users of your Home Assistant instance to it, by adding more automations with more persons.

Prerequisites

The automation works like this:

  1. You enter a specified geozone (or optionally, you are marked as “Home” by e.g. a wifi-tracker)
  2. A Telegram notification is sent out, that the unlocking automation started. If this is a misfire, you can cancel it directly from the notification
  3. A specific bluetooth proxy notifies that you are close to it
  4. The door gets unlocked

To make this work, there is some configuration needed:

  • The bluetooth proxy should have the correct Area attached
  • Set up the companion app to transmit bluetooth beacons See here. I think this is only necessary for Android, since iOS always transmits iBeacons
  • The blueprint uses Bermuda to get the Area of your phone.

Telegram Notifications

Only if you want telegram notifications (highly recommended):

Blueprint

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

The blueprints code is here:

blueprint:
  name: Automatic door unlock
  description: >-
    Automatically unlock the door, when the selected person first enters the specified
    zone and afterwards gets close to the bluetooth range of a bluetooth proxy.

    Prerequisites:
      - The automation uses the [Bermuda Triangulation](https://github.com/agittins/bermuda) integration
      to get the Area of your bluetooth device
      - Your phone has to be set to transmit bluetooth beacons
      - (Only if Telegram is enabled) Set up `person.xxx.telegram_id` using the `customize.yaml`
      - You need another automation to trigger the cancellation of your door
      with the event_type `telegram_callback` and command `/cdu` as trigger
  domain: automation
  input:
    person:
      name: Person
      description: The person, who has to enter the zone
      selector:
        entity:
          domain: person
    enable_telegram:
      name: Enable Telegram?
      default: false
      description: >-
        If enabled, will sent out the Telegram notifications. Make sure that you set up the
        telegram_bot integration first: https://www.home-assistant.io/integrations/telegram_bot/
      selector:
        boolean:
    zone:
      name: Zone
      default: zone.home
      description: The zone which has to be entered by the person
      selector:
        entity:
          domain: zone
    coming_home:
      name: Activate when person is marked as "home"?
      default: false
      description: >-
        If set to true, the automation will also trigger, when the person is marked as "home".
        This makes sense, when you want to use your wifi as device_tracker
      selector:
        boolean:
    beacon:
      name: Bluetooth Beacon
      description: >-
        The bluetooth beacons that have to be connected to the bluetooth proxy

        Notes:  
        - When using another integration than bermuda, change the "integration"
        part of this selector to your integration name in the blueprint's .yaml file  
        - The entity has to be in reach when creating this automation,
        otherwise it will not show up
      selector:
        entity:
          integration: bermuda
          multiple: true
          domain: sensor
          device_class: bermuda__custom_deviformusce_class
    area:
      name: Area
      description: >-
        The area the phone has to be considered as.
        Use the area that the bluetooth proxy at your door is in.

        Note: All areas with ESPHome devices are shown. You might need to double-check
        that there actually is a bluetooth proxy in the area you selected, otherwise
        it will not work.
      selector:
        area:
          entity:
            integration: esphome
    timeout:
      name: Zone-Entering Timeout
      default:
        hours: 0
        minutes: 10
        seconds: 0
      description: >-
        When you enter the zone and don't come close to the door in this timeframe,
        the automation will cancel. This is so that the door does not open automatically,
        when the zone you entered is triggered accidentally
      selector:
        duration:
    minimum_beacon_time:
      name: Minimal connection time
      default:
        hours: 0
        minutes: 0
        seconds: 5
      description: >-
        The minimum time the bluetooth beacon has to be connected to
        the proxy, to open the door.
      selector:
        duration:
    lock:
      name: Lock
      selector:
        entity:
          domain: lock

mode: restart

variables:
  timeout: !input timeout
  person: !input person
  lock: !input lock

trigger:
  - platform: zone
    entity_id: !input person
    zone: !input zone
    event: enter
  - platform: state
    entity_id:
      - !input person
    to: home
    enabled: !input coming_home

action:
  - service: telegram_bot.send_message
    metadata: {}
    enabled: !input enable_telegram
    data:
      target: '{{state_attr(person,"telegram_id")}}'
      message: >-
        You have been detected close to home. If you near {{ area }} in the next
        {{ timeout.hours*60 + timeout.minutes }} minutes{% if timeout.seconds != 0 %} and {{timeout.seconds}} seconds{%endif%},
        the door will be opened for you.
      one_time_keyboard: true
      inline_keyboard:
        - Cancel:/cdu {{this.entity_id}}
  - wait_for_trigger:
      - platform: state
        entity_id: !input beacon
        attribute: area_id
        to: !input area
        for: !input minimum_beacon_time
    continue_on_timeout: false
    timeout: !input timeout
  - service: lock.unlock
    target:
      entity_id: !input lock
    data: {}
  - service: telegram_bot.edit_message
    enabled: !input enable_telegram
    metadata: {}
    data:
      chat_id: '{{state_attr(person,"telegram_id")}}'
      message_id: last
      message: The door has been unlocked.
1 Like

Telegram automations

To enable telegram automations, add the following to your customize.yaml:

customize.yaml

person.<<person-name>>:
  telegram_id: <<persons-telegram-chat-id>>

Also add the following automation to enable the cancellation:

Automation: Telegram Cancellation

To cancel automations using Telegram (in case of misfires), set up an automation that listens for the /cdu command (cdu is for “cancel door unlock”, but thanks to message size limits for telegram callbacks, this has to be short)

The automation could look like this:

alias: "Lock: Cancel unlocking"
description: ""
trigger:
  - platform: event
    event_type: telegram_callback
    event_data:
      command: /cdu
condition: []
action:
  - service: automation.turn_off
    data:
      entity_id: "{{ trigger.event.data.args[0] }}"
      stop_actions: true
  - service: automation.turn_on
    data:
      entity_id: "{{ trigger.event.data.args[0] }}"
  - service: telegram_bot.edit_message
    metadata: {}
    data:
      message_id: "{{ trigger.event.data.message.message_id }}"
      chat_id: "{{ trigger.event.data.chat_id }}"
      inline_keyboard: []
      message: |
        Unlocking cancelled.

Thanks a lot for your suggestions, I added the MyHA Button as you suggested. I am new to sharing blueprints as you have seen and am very glad that people like you exist and help make posts for newbies better. <3

1 Like