It would be nice to have a template group, similar to other template components. It would give more dynamic group creation inclusion and exclusion.
Jinja supports returning a list which is what the regular group definition expects for entities
group:
template_group:
name: A template group
entitties_template: >-
{{ (states.zwave
| selectattr('attributes.battery_level', 'defined')
| map(attribute='entity_id')
| list)
}}
I think as long as the template returns a list, it should be possible.