Automation to turn on group light members of an area

Hello everyone! I am trying to create an automation to turn on a specific area of ​​lights. I found an example on the forum, but I am not being able to get it to work.

alias: automatic lights garden
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '+2:00:00'
  - platform: sun
    event: sunrise
condition: []
action:
  - service_template: |
      {% if trigger.event == 'sunset' %}
        light.turn_on
      {% elif trigger.event == 'sunrise' %}
        light.turn_off
      {% endif %} entity_id: 'group.automatic_lights_garden'
mode: single

I always get the message “ERROR (MainThread) [homeassistant.components.automation.automatic_lights_garden] Error while executing automation automation.automatic_lights_garden: Template rendered invalid service: light.turn_on” in the logs
I, try switch.turn_on service but with same result. And test it on Developer Tools and there works with the service light turn on. the group automatic lightsgarden exist and work if i turn on the switch on lovelace panel
Thanks in advance.

A couple things:

  • entity_id in the service call is indented incorrectly.
  • No need to quote the group entity ID (although it doesn’t really hurt.)
  • No need for elif. Just use else.

Try:

alias: automatic lights garden
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '+2:00:00'
  - platform: sun
    event: sunrise
condition: []
action:
  - service: |
      {% if trigger.event == 'sunset' %}
        light.turn_on
      {% else %}
        light.turn_off
      {% endif %}
    entity_id: group.automatic_lights_garden
mode: single

Now its working perfectly!
Thanks pnbruckner !!

FWIW, it can be simplified a bit further:

alias: automatic lights garden
description: ''
trigger:
  - platform: sun
    event: sunset
    offset: '+2:00:00'
  - platform: sun
    event: sunrise
condition: []
action:
  - service: "light.turn_{{ 'on' if trigger.event == 'sunset' else 'off' }}"
    entity_id: group.automatic_lights_garden
mode: single

Nice! simpler its better :blush:

That’s a great start for me - thanks! :slight_smile:

However, how about a random pattern in this automation.
I would like to switch of the members of the group with a random delay like this to simulate manual switching when going to bed :wink:

delay:
  hours: 0
  minutes: 0
  seconds: "{{ range(60, 330)|random }}"
  milliseconds: 0

The automation above will cycle through the members AFAIK, so the delay must be somewhere in this “loop”, but I don’t k now how.

By the way:
Does this also work if the main group contains groups with multipl lights (so through the subgroup structruee)?

So, groups have changed significantly since the posts above. Still, when you turn off a group, how/when the individual entities in the group get turned off is kind of beyond your control. If you want to control the sequence and timing, you’ll have to write an automation and/or script to do so, at least as far as I know.

I have a similar automation, but it’s a bit more complicated than that. (E.g., it takes into account which lights are actually on, and simulates going to the “furthest” light, possibly turning lights on in the process, and then turning them off along a “path” to the bedroom, with random delays between all those steps.)

Anyway, possibly the simplest way to do what you want may be something like this:

action:
  - repeat:
      for_each:
        - light.light_1
        - light.light_2
        - switch.light_3
      sequence:
        - service: homeassistant.turn_off
          target:
            entity_id: "{{ repeat.item }}"
        - if: "{{ not repeat.last }}"
          then:
            - delay: "{{ range(300) | random }}"

UPDATE: Fixed typo in service call.

that makes sense, I thought that it cannot be randomized within the group action.

Thank ou very much for this alternative, Phil.
I will give it a try :slight_smile:

I am trying to make use of this with an if before that:

action:
  - service: telegram_bot.send_message
    data:
      message: "Automation (Außen Automatik) triggered by triggerid: {{ trigger.id }}"
  - service: |
      {% if trigger.id == 'nightmode' %}
        action:
        - repeat:
            for_each:
              - light.graslicht_tasmota
              - light.bambuslicht
              - light.hausturlicht
              - light.doppelschalter_terrassenlicht
            sequence:
              - service: home_assistant.turn_off
                target:
                  entity_id: "{{ repeat.item }}"
              - if: "{{ not repeat.last }}"
                then:
                  - delay: "{{ range(30, 300) | random }}"

But I get the error:

Error: Error rendering service name template: UndefinedError: 'repeat' is undefined

I assume that It’s related to the service before the repeat, but I don’t know how to integrate the if AND the repeat properly

This needs the service:

- service: |
      {% if trigger.id == 'nightmode' %}

but this obviously not (created by UI) - not YAML)

`  - if:
      - condition: trigger
        id: nightmode
    then:
      - repeat:
          count: 2
          sequence: []`

EDIT:

This was not accepted on my lights, but light.turn_off works

This works now:

  - if:
      - condition: trigger
        id: debug
    then:
      - repeat:
          for_each:
            - light.graslicht_tasmota
            - light.bambuslicht
            - light.hausturlicht
            - light.doppelschalter_terrassenlicht
          sequence:
            - service: light.turn_on
              target:
                entity_id: "{{ repeat.item }}"
            - if:
                - condition: template
                  value_template: "{{ not repeat.last }}"
              then:
                - delay: "{{ range(3, 10) | random }}"

It’s not with

{% if trigger.id == 'nightmode' %}

but that’s ok.
Thank you again @pnbruckner for your help! :smiley:

1 Like

That was a typo I fixed right after I posted the suggestion. Per the update of my post, it should be homeassistant.turn_off. I only used that service because I didn’t know if all the entities you wanted to turn off were of the same type (i.e., light.) If they are, then yeah, light.turn_off is probably better anyway.

right I did not recognize this - sorry.
Thanks again.