Add or remove entities from group with automation

I’m looking for a way to control what devices get notificiations. I have created input_booleans for all devices and my idea is to add or remove the device from a group and send the notifications to that group. Is it possible to create an automation that triggers on changes in input_boolean and add or removes from a group?

No. There is no way to change the home assistant configuration with an automation. A group definition is part of the configuration.

You could create a notification service for each recipient and choose which ones get used by implementing a service template dependant on your input booleans.

I see. I have never used service template before. This is the code i used in automations:

  - service: notify.all_iphones
    data:
      message: xxx xxxx xxxx

Can someone guide me how to split this into two different devices and check input_boolean?

Something like this:

configuration.yaml

notify:
- platform: group
  name: all_iphones
  services:
  - service: mobile_app_iphone_1
  - service: mobile_app_iphone_2
  - service: mobile_app_ipad_1
  - service: mobile_app_ipad_2

- platform: group
  name: some_iphones
  services:
  - service: mobile_app_iphone_1
  - service: mobile_app_ipad_1

automation:

action:
- service_template: >
    {% if is_state('input_boolean.your_input_boolean_here', 'on') %}
      notify.all_iphones
    {% else %}
      notify.some_iphones
    {% endif %}
  data:
    message: "xxx xxxx xxxx"

Is it possible to do this without the groups. Like:

action:
- service_template: >
    {% if is_state('input_boolean.iphone_1', 'on') %}
      notify.iphone_1
    {% if is_state('input_boolean.iphone_2', 'on') %}
      notify.iphone_2
    {% else %}
    {% endif %}
  data:
    message: "xxx xxxx xxxx"

Sure but you need to supply an ‘else’ case or it will generate errors if none of the input booleans are on.

action:
- service_template: >
    {% if is_state('input_boolean.iphone_1', 'on') %}
      notify.iphone_1
    {% if is_state('input_boolean.iphone_2', 'on') %}
      notify.iphone_2
    {% else %}
### YOU NEED SOMETHING HERE ###
    {% endif %}
  data:
    message: "xxx xxxx xxxx"

Or you need to ensure that at lease one of the input booleans is on in the automation conditions.

condition:
- condition: or
  conditions:
  - condition: state
    entity_id: input_boolean.iphone_1
    state: 'on'
  - condition: state
    entity_id: input_boolean.iphone_2
    state: 'on'
- service_template: >
    {% if is_state('input_boolean.iphone_1', 'on') %}
      notify.iphone_1
    {% if is_state('input_boolean.iphone_2', 'on') %}
      notify.iphone_2
    {% endif %}
  data:
    message: "xxx xxxx xxxx"

Hey @backisen you might want to have a look at this post, it seems there is a group.set service: