[Z-Wave][Zigbee] Set temporary guest code for door lock

I recently purchases a Zigbee smart door lock with programmable codes, and wanted to make a script that can set a temporary “guest” code for a set duration (automatically delete the code after an amount of time)

I made two blueprints, one for Zigbee devices (which I’ve personally confirmed works with my lock) and one for Z-Wave devices (untested). They both do the same thing:

  • Sets a code
  • Optionally triggers another action, such as notify
  • Waits for an amount of time to pass
  • Deletes the code
  • Optional triggers another action, such as notify

Scripts

Zigbee (requires ZHA Toolkit)

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

# This script uses ZHA Toolkit (https://github.com/mdeweerd/zha-toolkit) to manage smart door lock codes, and therefore ZHA Toolkit needs to be installed.
# There is no gaurantee that this script will work as expected with your particular ZigBee smart door lock.
# This has been tested against a Kwikset SmartCode 916 (https://www.kwikset.com/products/detail/916-smartcode-traditional-electronic-deadbolt-with-zigbee-technology)

blueprint:
  name: Set Guest Door Code (ZigBee)
  description: >-
    A script that sets a temporary code for a ZigBee smart door lock (via ZHA Toolkit). 
    This script adds and removes a new code, rather than enabling/disabling an existing code.
  source_url: https://raw.githubusercontent.com/nwithan8/configs/main/home_assistant/blueprints/scripts/set_guest_door_code_zigbee.yaml
  domain: script
  input:
    lock:
      description: (Required) Which door lock to program
      name: Lock
      selector:
        target:
          entity:
            domain: lock
    slot:
      description: (Required) Which code slot to use
      name: Slot
      selector:
        text:
    on_start:
      description: (Optional) Action to execute when code is enabled
      
        The lock entity is available via the "lock" variable.
        The set code is available via the "code" variable.
        The set code slot is available via the "slot" variable.
        The validity time is available via the "duration" variable.
      name: When code enabled
      default: []
      selector:
        action:
    on_end:
      description: (Optional) Action to execute when code is disabled
      name: When code disabled
      default: []
      selector:
        action:
fields:
  code:
    description: The code to set
    example: "1234"
    name: Code
    required: true
    selector:
      text: null
  duration:
    description: How long the code is valid for
    name: Valid for
    required: true
    selector:
      duration:
        enable_day: true

variables:
  lock: !input lock
  slot: !input slot

sequence:
  - service: zha.set_lock_user_code
    data:
      code_slot: "{{ slot }}"
      user_code: "{{ code }}"
    target:
      entity_id: "{{ lock.entity_id }}"
  - choose:
      - conditions: "{{ true }}"
        sequence: !input on_start
  - delay: "{{ duration }}"
  - service: zha.clear_lock_user_code
    data:
      code_slot: "{{ slot }}"
    target:
      entity_id: "{{ lock.entity_id }}"
  - choose:
      - conditions: "{{ true }}"
        sequence: !input on_end

mode: single

Z-Wave (requires Keymaster)

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

# This script uses Keymaster (https://github.com/FutureTense/keymaster) to manage smart door lock codes, and therefore Keymaster needs to be installed.
# There is no gaurantee that this script will work as expected with your particular Z-Wave smart door lock.

blueprint:
  name: Set Guest Door Code (Z-Wave)
  description: >-
    A script that sets a temporary code for a Z-Wave smart door lock (via Keymaster). 
    This script adds and removes a new code, rather than enabling/disabling an existing code.
  source_url: https://raw.githubusercontent.com/nwithan8/configs/main/home_assistant/blueprints/scripts/set_guest_door_code_zwave.yaml
  domain: script
  input:
    lock:
      description: (Required) Which door lock to program
      name: Lock
      selector:
        target:
          entity:
            domain: lock
    slot:
      description: (Required) Which code slot to use
      name: Slot
      selector:
        text:
    on_start:
      description: (Optional) Action to execute when code is enabled.
        The lock entity is available via the "lock" variable.
        The set code is available via the "code" variable.
        The set code slot is available via the "slot" variable.
        The validity time is available via the "duration" variable.
      name: When code enabled
      default: []
      selector:
        action:
    on_end:
      description: (Optional) Action to execute when code is disabled.
      name: When code disabled
      default: []
      selector:
        action:
fields:
  code:
    description: The code to set
    example: "1234"
    name: Code
    required: true
    selector:
      text: null
  duration:
    description: How long the code is valid for
    name: Valid for
    required: true
    selector:
      duration:
        enable_day: true

variables:
  lock: !input lock
  slot: !input slot

sequence:
  - service: keymaster.add_code
    data:
      code_slot: "{{ slot }}"
      usercode: "{{ code }}"
      entity_id: "{{ lock.entity_id }}"
  - choose:
      - conditions: "{{ true }}"
        sequence: !input on_start
  - delay: "{{ duration }}"
  - service: keymaster.clear_code
    data:
      code_slot: "{{ slot }}"
      entity_id: "{{ lock.entity_id }}"
  - choose:
      - conditions: "{{ true }}"
        sequence: !input on_end

mode: single

Usage

Setting up the script

This script will set code slot 30 on the Front Door Lock to whatever code is provided.
After enabling the code, a notification will be sent to my phone, telling me what code was just set.
When the provided time is lapsed, the code will be deleted, and a notification will be sent to my phone to confirm.

Although they are optional, I would recommend using the “When code enabled” and “When code disabled” actions to trigger some sort of notification or other action to confirm that the code has been added and removed. Alternatively, you could use these actions to trigger other automations or scripts, such as disabling a house alarm or preparing for a guest’s arrival.

Calling the script

This will set the guest code to “1234” for 12 hours, after which it will be deleted.

1 Like

Hi Nate, how could I adapt this for the Aqara U100? Note for that particular lock currently you need to lair it over zigbee to matter enabled m2 hub which can then be used to bring in the lock via matter to HA.

Hmm, I would imagine the pin code setting capabalities probably got lost in the Zigbee-to-Matter translation. I assume you’ve already sanity-check tried to use this script (or just the zha.set_locker_user_code service directly) to modify the code on your door lock?

Hi @nwithan8. Can I adapt this blueprint for the Z-Wave Allegion SBE469ZP without Keymaster? I’ll share the results here.

Go for it.

Thank you. It works on Allegion SBE469ZP.

# This script uses ZWaveJS HA Integration to manage smart door lock codes.
# There is no gaurantee that this script will work as expected with your particular Z-Wave smart door lock.
# This has been tested on Allegion SBE469ZP (https://www.schlage.com/en/home/products/BE469ZPCAMFFF.html)

blueprint:
  name: Set Guest Door Code (Z-Wave)
  description: >-
    A script that sets a temporary code for a Z-Wave smart door lock (via ZWaveJS). 
    This script adds and removes a new code, rather than enabling/disabling an existing code.
  source_url: https://community.home-assistant.io/c/blueprints-exchange
  domain: script
  input:
    lock:
      description: (Required) Which door lock to program
      name: Lock
      selector:
        target:
          entity:
            domain: lock
    slot:
      description: (Required) Which code slot to use
      name: Slot
      selector:
        text:
    on_start:
      description: (Optional) Action to execute when code is enabled
      
        The lock entity is available via the "lock" variable.
        The set code is available via the "code" variable.
        The set code slot is available via the "slot" variable.
        The validity time is available via the "duration" variable.
      name: When code enabled
      default: []
      selector:
        action:
    on_end:
      description: (Optional) Action to execute when code is disabled
      name: When code disabled
      default: []
      selector:
        action:
fields:
  code:
    description: The code to set
    example: "123456"
    name: Code
    required: true
    selector:
      text: null
  duration:
    description: How long the code is valid for
    name: Valid for
    required: true
    selector:
      duration:
        enable_day: true

variables:
  lock: !input lock
  slot: !input slot

sequence:
  - service: zwave_js.set_lock_usercode
    data:
      code_slot: "{{ slot }}"
      usercode: "{{ code }}"
    target:
      entity_id: "{{ lock.entity_id }}"
  - choose:
      - conditions: "{{ true }}"
        sequence: !input on_start
  - delay: "{{ duration }}"
  - service: zwave_js.clear_lock_usercode
    data:
      code_slot: "{{ slot }}"
    target:
      entity_id: "{{ lock.entity_id }}"
  - choose:
      - conditions: "{{ true }}"
        sequence: !input on_end

mode: single
1 Like