Doors Open/Unlocked/People Home Template Help

So I have a “report” that tells me people which are home that works fine, but I’d like to add doors which are open to it, but what I’d really like to do that I’m having trouble with is, including my door locks, so it would say something like:
The side door is open. OR The side door is unlocked…
(obviously if it is open, it is unlocked, so I don’t need it to say both if it is open, but I would like to know if it’s unlocked but closed)
I can’t really figure out how to get it to only choose open/vs unlocked when both are on/unlocked.
This is what I have so far, obviously it’s pretty much two of the same, so ideally I’d like to make it all one continuous response, in this current state it leaves a couple blank lines between the result.

{% set people_home = states | selectattr('entity_id', 'in', state_attr('group.family','entity_id')) | selectattr('state','in',['home']) | map(attribute='name') | list %}
{% if people_home | length == 1 %}
{{ people_home[0] }} is at home
{% elif people_home | length == 0 %}
The house is empty
{% else %}
{{ people_home[:-1] | join(', ') }}{{',' if people_home | length > 2 else ''}} and {{ people_home[-1]}} are home
{% endif %}
{% set doors_open = states | selectattr('entity_id', 'in', state_attr('group.doors','entity_id')) | selectattr('state','in',['on']) | map(attribute='name') | list %}
{% if doors_open | length == 1 %}
and the {{ doors_open[0] }} is open
{% elif doors_open | length == 0 %}
and all doors are secure
{% else %}
and the {{ doors_open[:-1] | join(', ') }}{{',' if doors_open | length > 2 else ''}} and {{ doors_open[-1]}} are open
{% endif %}

Any help would be appreciated… I’m a pretty much a noob when it comes to templates…

I was searching for this recently as well, found another thread that helped me come to this conclusion, but I can’t find it anymore… Here’s a template to show the locks that are locked:

{{ states.lock|selectattr('state', 'equalto', 'locked')|list|map(attribute='name')| list|join(', ') }}

You can also count them with this:

{{ states.lock|selectattr('state', 'equalto', 'locked')|list|length }}

You can adjust these for the different domains
Here’s my config: https://github.com/JonGilmore/ha-personal/blob/5e57e81ca219543962337e76ad7d407ce52b5d3e/sensors.yaml#L144-L167

Do you know how I would get it to choose only one or the other? I have a door lock and door sensor for both my front and side doors, but I would like it to only say open if opened, but locked or unlocked if the door is in the closed position

where is the information stored for locked / unlocked?

the doors are binary sensors, off closed, on open.
the locks are lock entities, with locked/unlocked, but I also have binary sensors for them, off locked, on unlocked…
I think that is what you were asking correct?
Currently everything is in a group called group.doors, but determining wether having the lock entities or the binary sensors for the locks in the group is where I realized I had no idea where to go next.

Yeah, I was trying to see if these lock items had the state of the door built into them. I don’t have locks myself so I really don’t know. The more I know about your setup the better I can help.

So, last question: Can you post all the entity_ids for the locks and door sensors?

entities:
  - binary_sensor.front_door_lock
  - binary_sensor.side_door_lock
  - binary_sensor.garage_door
  - binary_sensor.side_door_opened
  - binary_sensor.front_door_opened
## not currently in group but could be..
#  - lock.lock_front_door_lock
#  - lock.lock_side_door_lock

I’m guessing your binary sensor is a sensor template. If not, I suggest you make a binary sensor, or a sensor that fully represents the door. This is what it would look like:

- platform: template
  sensors:
    front_door:
      value_template: "{{ is_state('binary_sensor.front_door_opened','on') and is_state('lock.lock_front_door_lock', 'unlocked') }}"
      device_class: door
      attrbute_templates:
        door: "{{ 'open' if is_state('binary_sensor.front_door_opened','on') else 'closed' }}"
        lock: "{{ states('lock.lock_front_door_lock') }}"

Now you have a single binary_sensor that is only on when the door is unlocked and open. Closed at all other times. You can change the value template to be whatever you want.

Anyways, in the end, you can simply:

{{ states | selectattr('entity_id', 'in', state_attr('group.doors','entity_id')) | selectattr('state','eq','on') | selectattr('attributes.lock','eq','unlocked') }}

or something to that affect.