Zigbee Rotate Door Lock Code for Home Assistant

I have had a Schlage zigbee lock for the front door for some time now, and I have always wanted to be able to set the user code for the door lock like you could do with zwave. Well today I just noticed that functionality has finally made it into Home Assistant. I wanted to auto-rotate the user code on my door lock, after doing some searching around I found someone that got it to work with a zwave lock, so I thought it couldn’t be that much different to get going with zigbee.

For my scenario, I’m using 4 digit user codes. The first part of the setup in configuring two “random” sensors in your configuration.yaml. This will be used to generate the 4 digit code.

sensor:
  - platform: random
    name: "First Set"
    minimum: 10
    maximum: 99
  - platform: random
    name: "Second Set"
    minimum: 10
    maximum: 99

Next we need a input_number helper, with the minimum value set to 1000 and the maximum set to 9999.

input_number

Now create a script that will set the input_number to the two numbers generated by the random sensors, notify you of the code in a text message, and then change the lock to the code generated.

service: input_number.set_value
data_template:
  entity_id: input_number.guest_code
  value: >-
    {{ states.sensor.first_set.state | int }}{{ states.sensor.second_set.state |
    int }}
service: notify.group
data_template:
  message: |-
    *New Lock Code Generated* 
     The new code is: {{ states.input_number.guest_code.state | int }}
service: zha.set_lock_user_code
data_template:
  code_slot: 1
  user_code: '{{ states.input_number.guest_code.state | int }}'
target:
  entity_id: lock.schlage_front_door
DoorCodeScript

Next create an automation that will trigger the script. I have mine set to run every 18 hours, except on the weekends.

Rotate Door Lock Code automation