Generating comma-separated list from multiple binary sensors

I have a robot vacuum flashed with Valetudo and integrated into Home Assistant. This exposes a sensor, sensor.valetudo_dreame_d9_map_segments, which contains as attributes several segments and their friendly names, like so:

"1": Bathroom
"2": Kitchen
"3": Living Room
"4": Kid's Room
"6": Main Bedroom
"7": Hallway

I would like to be able to mark the rooms I wish to be vacuumed via binary switches, set the number of passes via a number helper, and press a button to start my vacuum cleaning with those parameters.

I’ve created the helper entities for this purpose: one binary switch for each room (input_boolean.vacuum_bathroom, input_boolean.vacuum_kitchen, etc.), which I have grouped under group.vacuum_rooms, and a number helper for the iterations, input_number.vacuum_iterations.

I have downloaded this excellent blueprint package to help with sending commands to the vacuum.

The script takes, as input, a comma-separated list of segment names (their ‘friendly names’, not their ID numbers).

How can I generate such a comma-separated list from the binary sensors within the group that are in the state true?

I take it you want a comma separated list of friendly names where their state is on.

{{ expand('group.vacuum_rooms')
  | selectattr('state', 'eq', 'on')
  | map(attribute='attributes.friendly_name') | join(',') }}

Thank you, this was a big help. Because I didn’t have a friendly_name attribute on these binary sensors, I went back into customize.yaml and added a quick room_name attribute to each. In the “segments” section of the vacuum script, I used the following:

segments: >-
    {{ expand('group.vacuum_rooms') | selectattr('state', 'eq', 'on') |
    map(attribute='attributes.room_name') | join(',') }}

It works!