I habe the same question with controlling the lights which are currently ON.
I understand that there is an existing solution as described above. But this was all written before the changes of group behaviour in HA release 2022.4. So I tried to find something easier. I added the template mentioned above which gives me a list of all lights which are currently on in the script to „set group“. Unfortunately template seems to be not accepted here.
Does anyone have a more simple Solution than described above by using the new Features implemented in R2022.4?