If statement in a for each loop?

I am attempting to run a for each loop for a set of locks in my house. Detect if they are unlocked and if they are, lock them.
Can anyone tell me what I’m doing wrong?

I am getting the following error:
template value is None for dictionary value @ data[0][‘repeat’][‘sequence’][0][‘then’][0][‘data’]

    - device_id: aaa
      entity_id: lock.bbb
    - device_id: yyy
      entity_id: lock.zzz   
        - condition: device
          device_id: "{{ repeat.item.device_id }}"
          domain: lock
          entity_id: "{{ repeat.item.entity_id }}"
          type: is_unlocked
        - service: lock.lock
            device_id: "{{ repeat.item.device_id }}"

Is this an automation? should you be using the choose functionality?

It’s complaining about that empty data block under the then. Remove it.

BTW, it’s probably harmless to just lock all locks (you want to) anyways and avoid the complexity of the loop.

I’m still new to HA, and haven’t yet wrapped my head around the syntax and intent for choose.

Thank you! I’ll give it a go and post my findings. Agreed on the locking command. Though I found myself ending up wanting to run the same “if” statement on a list of devices for all sorts of needs and could never get it to work. This was the first example I tried.