I’d like to set up an automation that locks all my locks at a set time, but only if the lock is in an unlocked state. I’m still new to scripting, but here’s what I have so far that is failing according to the trace logs.
automation:
- id: "2586665546233"
alias: bed time
trigger:
- platform: time
at: "22:00:00"
action:
- service: script.lock_unlocked_locks
mode: single
script:
lock_unlocked_locks:
sequence:
- service: >
{% set locks = ['lock.front_door_2', 'lock.basement_entry_2', 'lock.yard_entry_2', 'lock.garage_entry_2'] %}
{% set unlocked_locks = states | selectattr('entity_id', 'in', locks) | selectattr('state', 'eq', 'unlocked') | map(attribute='entity_id') | list %}
{% if unlocked_locks %}
lock.lock
{% endif %}
data_template:
entity_id: "{{ unlocked_locks }}"
The reason why I only want to call the lock.lock service is because the lock will go from locked to unlocked and back to locked if I don’t have this condition.
Thoughts on why it’s failing? The trace log ends with “Template rendered invalid service:”.
Your bed_time automation calls a script named script.lock_unlocked_locks but it doesn’t exist.
Check Developer Tools > States for the presence of script.lock_unlocked_locks.
Also check the Log for any errors related to scripts.
Lastly, where are your scripts normally stored? The way you posted your original example, where the first line is the script: key, suggests all your scripts are stored in configuration.yaml. If they’re stored elsewhere, in a separate file, then the first line of that file should not contain the script: key.
State for the script.lock_unlocked_locks is unavailable. From the logs:
Script with object id ‘lock_unlocked_locks’ could not be validated and has been disabled: expected a dictionary for dictionary value @ data[‘sequence’][0][‘variables’]. Got None extra keys not allowed @ data[‘sequence’][0][‘locks’]. Got [‘lock.front_door_2’, ‘lock.basement_entry_2’, ‘lock.yard_entry_2’, ‘lock.garage_entry_2’] extra keys not allowed @ data[‘sequence’][0][‘unlocked_locks’]. Got “{{ expand(locks) | selectattr(‘state’, ‘eq’, ‘unlocked’)\n | map(attribute=‘entity_id’) | list }}\n”
Here’s how I have things coded in configuration.yaml: