Here are two suggestions.
The first one handles any number of doors.
- platform: template
sensors:
open_doors:
value_template: >
{% set doors =
[ states.binary_sensor.back_door_access_control,
states.binary_sensor.front_door_access_control ]
| selectattr('state', 'eq', 'on')
| map(attribute='name') | list %}
{% set qty = doors | count %}
{% if qty == 0 %}
All exterior doors are closed.
{% else %}
The following door{{ 's are' if qty > 1 else ' is' }} open: {{doors | join(', ')}}
{% endif %}
The second one is designed to handle only two doors. It also strips out the word “Door” from the entity’s friendly_name so it can produce a phrase like “The Back and Front doors are open.” as opposed to “The Back Door and Front Door are open.”
- platform: template
sensors:
open_doors:
value_template: >
{% set doors =
[ states.binary_sensor.back_door_access_control,
states.binary_sensor.front_door_access_control ]
| selectattr('state', 'eq', 'on')
| map(attribute='name')
| map('replace', ' Door', '') | list %}
{% set qty = doors | count %}
{% if qty == 0 %}
All exterior doors are closed.
{% elif qty == 1 %}
The {{doors[0]}} door is open.
{% else %}
The {{doors[0]}} and {{doors[1]}} doors are open.
{% endif %}
Ideally, we could use the expand function with a group in this Template Sensor. However, on startup, Home Assistant doesn’t expand the group to monitor each group member’s state but simply monitors the group’s state. I had posted it as a Feature Request.
There was an enhancement introduced in 0.110 to make it monitor each group member’s state but, unfortunately, it failed to work. I reported it as an Issue and the developer continues to work on correcting it. The complication he encountered is that, on startup, groups must be defined before Template Sensors and that sequence is not guaranteed.
EDIT
Correction. Forgot to copy-paste this very important line: value_template: >