Finally figured out how to handle groups too
{% set exclude_light = 'light.oprit_lichten' %}
{% set exclude_groups = states.light
| selectattr('attributes.entity_id', 'defined')
| selectattr('attributes.entity_id', 'contains', exclude_light)
| map(attribute='entity_id')
| list %}
{% set all_exclude = [exclude_light] + exclude_groups %}
{{ states.light
| selectattr('state', 'eq', 'on')
| rejectattr('entity_id', 'in', all_exclude)
| map(attribute='entity_id')
| join(', ') }}