Tracking state of all locks in a scene

First of all, I’ve been googling around and reading for a while tryign to figure out the best way to do this but am struggling to understand exactly how to execute.

I’d like to have a button that turns its icon orange if any of the locks in the house are not locked. I could prob do this with a long template statement and 'and’s but would love to have a script (i believe this is the best?) to return a true or false to set the condition of the icon color. (unless there is a better way of course)

If I hit the button, I want it to lock any of the locks that are unlocked.

Any help or direction would be greatly appreciated!

Edit: (more info on what I’ve found/done)
This is currently what I have controlling the icon color:

{% if lock.back_porch|selectattr(‘state’,‘equalto’,‘unlocked’)|list|length > 0 %}
orange
{% else %}
white
{% endif %}

I found that in a template I can do this:

{{ states.lock.back_porch.state == ‘locked’ and states.lock.basement_back.state == ‘locked’ }}

I considered something like this

if states.all_locks|selectattr(‘state’,‘equalto’,‘unlocked’)|list|length > 0

And also was trying to reference code from the template here: Toggle Scene on/off script

{% for entity_ids in scene_entities if is_state(entity_ids, ‘on’)%}
{% if loop.index == 1 %}
true
{% endif %}
{% else %}
false
{% endfor %}

Your topic’s title is “locks in a scene”. Which one do you want check, all locks or just the ones in a scene?

List entity_ids of all unlocked locks:

{{ states.lock | selectattr('state', 'eq', 'unlocked') | map(attribute='entity_id') | list }}

Report true if any lock is unlocked:

{{ states.lock | selectattr('state', 'eq', 'unlocked') | list | count > 0 }}

For locks in a scene, replace states.lock with:

expand(state_attr('scene.your_scene', 'entity_id'))

Well, I guess in this case I would like to check all locks but would like to know how to set this up for a scene as I will want to split up the locks into different floors and do something similar for lights as well.

I posted examples for both cases, all locks and just the ones in scene.

Thanks a bunch! I’m trying this out. I was pretty close with some of my fiddling around.