Trouble creating a toggle script for groups

Hi,

I’m trying to create a script that toggles groups of entities states from either all on to all off.

My code in scripts.yaml:

toggle_group:
  alias: 'Toggle Group On/Off'
  sequence:
    - service_template: >
        {% if is_state(group_id,'off') %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
      service_data:
        entity_id: "{{ state_attr(group_id,'entity_id') | join(', ') }}" 

My code in lovelace yaml

          - cards:
              - entity: group.all_home_theatre_lights
                icon: 'mdi:lightbulb'
                name: All
                state:
                  - color: 'rgb(253,216,53)'
                    value: 'on'
                tap_action:
                  action: call-service
                  service: script.toggle_group
                  service_data:
                    group_id: 'group.all_home_theatre_lights'
                type: 'custom:button-card'

I’m also using the custom button-card.

The error I’m getting from home assistant is:

[homeassistant.config] Invalid config for [script]: [service_data] is an invalid option for [script]. Check: script->script->toggle_group->sequence->0->service_data. (See /config/configuration.yaml, line 26).

I’m not too sure what this means, I’m fairly new to home assistant.

My Home Assistant is running on Synology inside docker, latest build.

Please let me know if you have any suggestions. Thanks.

Came up with a solution to my own problem.

scripts.yaml

toggle_group:
  alias: 'Toggle Group On/Off'
  sequence:
    - service_template: >
        {% if is_state(group_id,'off') %}
          homeassistant.turn_on
        {% else %}
          homeassistant.turn_off
        {% endif %}
      data_template:
        entity_id: >
          {{ group_id }}

lovelace yaml

- cards:
              - entity: group.all_home_lights
                icon: 'mdi:lightbulb'
                name: All
                state:
                  - color: 'rgb(253,216,53)'
                    value: 'on'
                tap_action:
                  action: call-service
                  service: script.toggle_group
                  service_data:
                    group_id: group.all_home_lights
                type: 'custom:button-card'

Really works great now.

I wouldn’t do this if I were you.
A group is considered ‘on’ if only 1 of it’s members is ‘on’
The results of your script are unpredictable
You would be better defining an entity in the group that you wish the others to emulate and then drive it from that entity to all the other entities and not the group

If the entities are all lights use this:

It is designed for your exact problem. I.e. treating a bunch of lights as one.

Thanks for the feedback. I’ve tested it and I’m okay with this outcome.

I’m planning to use it for switches, lights, and a mix of both. Simple script for the button-card allowing for all scenarios.