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