Populate Helper binary group based on sensor status from the moment of changing the Alarm status

Hello,
I have created a binary group of door sensors in helpers and I have populated manually this group with all the door sensors of my home.

What I want is to populate this group based on the following rule:

Rule
In the moment when the alarm change the status to ¨Arm Away¨ or ¨Arm Home¨ populate this group only with the door sensors which have the status ¨Closed¨

Thank you!!

{
        "created_at": "1970-01-01T00:00:00+00:00",
        "data": {},
        "disabled_by": null,
        "domain": "group",
        "entry_id": "6206fbafb8b5b24883ee626c72bbc48d",
        "minor_version": 1,
        "modified_at": "2024-09-27T20:27:15.358330+00:00",
        "options": {
          "group_type": "binary_sensor",
          "name": "Alarm Doors sensors",
          "entities": [
            "binary_sensor.0x00158d0007e58bdf_contact",
            "binary_sensor.0x00158d00080ce783_contact",
            "binary_sensor.0x00158d00080cf126_contact",
            "binary_sensor.0x00158d0008094fca_contact",
            "binary_sensor.0x00158d00090d4160_contact",
            "binary_sensor.0x00158d0008f4521e_contact",
            "binary_sensor.0x00158d000ad96f4f_contact",
            "binary_sensor.0x00158d000ae0fbaf_contact",
            "binary_sensor.0x00158d000ae180ce_contact"
          ],
          "hide_members": false,
          "all": false
        },

Group Helpers themselves are not modifiable that way. You can, however, use your existing group as the basis for a Template sensor containing the desired entity IDs.

template:
  - trigger:
      - platform: state
        entity_id: alarm_control_panel.home_alarm
        to: 
          - armed_away 
          - armed_home
        variables:
          obj_list: "{{ expand(state_attr('binary_sensor.alarm_doors_sensors', 'entity_id')) }}"
          closed: "{{ obj_list | selectattr('state', 'eq', 'off') | list }}"
    sensor:
      - name: Closed Doors
        state: "{{ closed | count }}"
        attributes:
          entity_id: "{{ closed | map(attribute = 'entity_id') | list }}"

With the help of the Spook custom integration, it is also possible to create an automation to apply and remove labels from entities using similar templating.

It is also possible to add and remove group members from legacy groups.