Automation to rotate user code for Schlage lock

I have a user code for guests/babysitters/petsitters for my Schlage lock. I’d like to rotate it on a weekly basis to a new random code, and would love for it to be automated/have it send out the code on TG. I see the “lock.set_usercode” service and figure I’d do it that way, but I can’t figure out the automated bit/how to get it sent out via TG. Anybody do this/have ideas?

I don’t have such a lock so have no idea how that service works, but for the random code i’d use this component:


this will give you a random number. If you need more than one, for like a 4 digit code, use 4 sensors, one per digit.
Hope that helps.

I guess you’ll have to use something like:

  - service: lock.set_usercode
    data:
      node_id: 44
      code_slot: 4
    data_template:
      usercode: "{{states.sensor.pin_code.state}}"

Figured it out. Combination of random sensor and input_number run in a script. Thanks all! If anybody wants the code, just let me know.

I’d be interested in taking a look at the code.

Note: this is for a 6 digit combination. If you have 4 or 8, you’d need to tweak the number of random sensors and the input_number.

First, add random sensors:

##### Random for Locks
  - platform: random
    name: "First Set"
    minimum: 10
    maximum: 99
  - platform: random
    name: "Second Set"
    minimum: 10
    maximum: 99
  - platform: random
    name: "Third Set"
    minimum: 10
    maximum: 99

Next, add an input_number to your configuration.yaml.

input_number:
  guest_code:
    name: Guest Code
    initial: 123456
    min: 100000
    max: 999999
    step: 1

Once you do that, add a script to run the change (insert your values for XXXX):

  # Set Guest Code with random numbers
  guest_lock_code:
    alias: Generate and Set Guest Code
    sequence:
      - 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 }}{{ states.sensor.third_set.state | int }}"
      - service: lock.set_usercode
        data_template: {
          "node_id": XXXX,
          "code_slot": XXXX,
          "usercode": "{{ states.input_number.guest_code.state | int }}"
          }
      - service: notify.XXXX
        data_template:
          message: "*New Guest User Code Generated* \n The new code for the week is: {{ states.input_number.guest_code.state | int }}"

Finally, add an automation to run the script however often you want. My is once a week on Sunday.

- alias: "Security - Weekly Door Code Rotate"
  hide_entity: True
  trigger:
    platform: time
    at: '09:00:00'
  condition:
    condition: time
    weekday:
    - sun
  action:
    - service: script.guest_lock_code
2 Likes