How to use groups in scripts?

Hi!
How can I use groups in my scripts?
I have a set of lights in a group and I want to use this group in my script (called ‘Soninho 1’). The idea is to turn off some lamps (this group). I know that I can set multiple actions in my script…but I will have to repeat this proccess in some other scripts, that’s why I would like to turn off just the ‘group’ device.
Also, I have more lights in this zone, so I can’t turn off entire zone.

This is my group (define in YAML mode):

- platform: group
  name: spots_quarto
  unique_id: spots_quarto
  entities:
    - light.spot_q1_light
    - light.spot_q2_light
    - light.spot_q3_light
    - light.spot_q4_light
    - light.spot_q5_light
    - light.spot_q6_light
    - light.spot_q7_light
    - light.spot_q8_light

spots_quarto doesn’t showd up in device selection box in script.

It should be an entity. Search in developer entity tab for group.

Yes, it does show up as an entitie…I’m already using in dashboard…but it doesn’t show while creating an script!

See this last action…spots_quarto doesnt show :frowning: Everything else is correct, including individual lights in that group.

  1. You probably should put up a light group. You can do this with a helper.

Automations & Scenes - Helpers - Add Helper - Group - Light Group

  1. Call that group via 'Call Service '. (Light) Groups are entities not devices.

Groups is already created in YAML.
But tks, the tip that groups are not devices, but entities, did light me up! :slight_smile:
This combined with Call Service solved my issue with script. Tks!

1 Like

The advantages of a light group are the simultaneous control of color, temperature and of course brightness.

But happy that I could help. :blush:

I agree with @jmceara. I just tried to create a test automation and my YAML-based groups do not appear in the entity picker anymore. NOTE: I would think you actually want to use “Call Service” and then the “Light off” service and select the entity but I could not see them their either.

Then I selected to edit in YAML as shown below and “boom” they are there. So it looks like YAML-based groups do not appear in the GUI-based entity picker.

I would note that I like using the old groups better although I would say I have not tried the new groups. This is because it would require a lot for me to redefine many switches as lights and I also do several groups in other groups. This is just a snippet of my YAML groups:

# Art
artlights:
    name: Artwork
    icon: phu:double-spot
    entities:
      - light.switchlinc_dimmer_1e_3f_e2
      - light.switchlinc_dimmer_2b_e4_c5
      - light.switchlinc_dimmer_1e_3e_07
      - light.switchlinc_dimmer_1e_3e_f3
      - light.switchlinc_dimmer_1e_3e_c0
      - light.switchlinc_dimmer_1e_3e_dc
      - light.keypadlinc_dimmer_1c_df_46_main
      - light.switchlinc_dimmer_1e_40_d7
    all: true
# ArriveHome
arrivehomelights:
    name: Arrival
    icon: mdi:car
    entities:
      - group.foyerlights
      - group.frontlights
    all: true
# Panic
paniclights:
    name: Panic
    icon: mdi:run-fast
    entities:
      - group.foyerlights
      - group.frontlights
      - group.backyardlights
      - group.kitchenlights
      - group.masterlights
    all: true

Then I make use of custom:button-card and repetitive YAML to add into Lovelace. Adding this button_card_template:

button_card_templates:
  group_lights:
    group_expand: true
    styles:
      card:
        - height: 100px
    tap_action:
      action: none
    hold_action:
      action: call-service
      service: >-
        [[[ if (entity.state == "on") return "homeassistant.turn_off"; else
        return "homeassistant.turn_on" ]]]
      service_data:
        entity_id: entity

And this in design with three lines of YAML per button and easy to add additional ones:

One additional point, maybe should be reported as a bug. If I try to use the Entity picker as shown below, you can see nothing “pops” when I start to type groups.

But if I enter the full name (in this case “group.artlights”) and click off, it comes back as knowing the group:

But these controls works with YAML defined group too. I’m already using this for a long time. I have a button in dashboard set to my group and after changing color/brightness for this group, all lights are changed. No group in UI at all! :slight_smile:

Well as I said they do work if I edit in YAML or I enter the full name of the group. They just do not appear in my quicklist picker for entities.

The difference is that my YAML uses old style groups. I can combine lights, switches and other groups this way. As I understand it, all members of light group must be in light domain now. So I would need to redefine many switches as lights. Then find out if you can add a light group to another group.

The answer was simple for my “old style” Groups. Of course you cannot use the service light.turn_on because they are not necessarily lights (some are switches and in some cases other things). You use the generic homeassistant.turn_on

Then you can see the groups.

2 Likes