Help With Automation/Script For Changing State of Entity

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:”.

script:
  lock_unlocked_locks:
    sequence:
      - variables:
          locks:
            - lock.front_door_2
            - lock.basement_entry_2
            - lock.yard_entry_2
            - lock.garage_entry_2
          unlocked_locks: >
            {{ expand(locks) | selectattr('state', 'eq', 'unlocked')
              | map(attribute='entity_id') | list }}
      - condition: "{{ unlocked_locks | count > 0 }}"
      - service: lock.lock
        target:
          entity_id: "{{ unlocked_locks }}"

Thanks for the suggestion. I’m now getting the following error when the automation kicks off…

Thoughts?

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:

automation ui: !include automations.yaml
automation manual: !include_dir_merge_list automations/
script: !include scripts.yaml

In the automations directory, the file where the automation is stored is called locks.yaml:

- id: "2586665546233"
  alias: bed time
  trigger:
    - platform: time
      at: "22:00:00"
  action:
    - service: script.lock_unlocked_locks
  mode: single

And then in the scripts.yaml:

lock_unlocked_locks:
  sequence:
    - variables:
      locks:
        - lock.front_door_2
        - lock.basement_entry_2
        - lock.yard_entry_2
        - lock.garage_entry_2
      unlocked_locks: >
        {{ expand(locks) | selectattr('state', 'eq', 'unlocked')
          | map(attribute='entity_id') | list }}
    - condition: "{{ unlocked_locks | count > 0 }}"
    - service: lock.lock
      target:
        entity_id: "{{ unlocked_locks }}"

I think I have a syntax error somewhere, but can’t figure out where.

Your version of my example contains an indentation error (compare with my example posted above).

The entire block of variables marked in red needs to be indented by two spaces.

You’re the best. That did the trick. Guess I need to go back to YAML for Dummies. Thanks again!

1 Like