Automation turn off a light if its whole group is not turned on

Hello there,

I have a lot of spotlight (hue) they are in groups (~10 in each groups), sometimes i have one spot that turn itself on (voltage drop i guess), i want to automate this check :

if only one bulb of the group is turned on, then turn it off.

What would be the simplest way to do this ? do i have to make one automation rule for each bulb ? (it would take me ages)

thanks :slight_smile:

i think you can adjust the code from here to do what you are looking for:

You should be able to just use the group name for your lights in place if the all doors one and update the length check to 1 instead of > 1.

Neat, i will check this tommorow, thanks :slight_smile:

Woudn’t this mean, if you turn on the first light, it would turn itself off again right away?

Yeah i have to add a delay, like if a light is on for a minute alone

1 Like

Hello again,

I tried this :slight_smile:

alias: "Turn off single light bulb in group cuisine"
  trigger:
    - platform: time_pattern
      minutes: "/5"
  condition:
    condition: template
    value_template: > 
      # this counts the light bulb that are on and if less than 2 are open, it will turn off the whole group
      {{ states | selectattr('entity_id', 'in', state_attr('light.cuisine_2','entity_id')) | selectattr('state','in',['on','open']) | list | length < 2 }}
   action:
    - service: light.turn_off
      target:
        entity_id:
          - light.cuisine_2

I tried to add this code into a new automation > go into YALM mode and paste it.

But when i have to save it says :

Message malformed: Integration ‘’ not found

I tried to mess around and change the format, but i can’t get it to work.

What have i done wrong ?

I found the issue, it was just space indent problem.

YALM is kinda strict about this :slight_smile:

Sorry for spamming,

So i got this code working :slight_smile:

alias: Turn off single light bulb in group cuisine
trigger:
  - platform: time_pattern
    minutes: /5
condition:
  - condition: template
    value_template: >
      # this counts the doors that are open and if we have any that are open,
      the notify will occur {{ states | selectattr('entity_id', 'in',
      state_attr('group.cuisine','entity_id')) |
      selectattr('state','in',['on','open']) | list | length < 2 }}
action:
  - service: light.turn_off
    target:
      entity_id:
        - light.cuisine_2
mode: single

It seems to work as intended, but there is one last issue.

While testing it i found out that my light.turn_off service is acting weird when the bulb is already off, its turning the bulb on then off.

which is quite annoying.

So i have to add a condition in my action so it turns off only the bulb that is on.

how should i do that? :slight_smile:

I was sort of thinking that from your original post that you never just have one light one from the group…so then you would just be looking for a count = 1 to know to turn it off…

Instead of using hard coded light.cuisine_2 you should be able to use trigger.entity_id:

action:
  - service: light.turn_off
      target:
      entity_id: "{{ trigger.entity_id }}"

The trigger shouldn’t be time based, you should be able to have it trigger when the group or when a light goes on. I think you might have to use a comma separated list of the lights though instead of the group if you only want to have it turn it off if it is on.

trigger:
  platform: state
  entity_id: light.cuisine_1,light.cuisine_2,light.cuisine_3,light.cuisine_4,etc
  to: 'on'

so your final would look like:

alias: Turn off single light bulb in group cuisine
trigger:
  platform: state
  entity_id: light.cuisine_1, light.cuisine_2, light.cuisine_3, light.cuisine_4
  to: 'on'
condition:
  - condition: template
    value_template: >
      {{ states | selectattr('entity_id', 'in',state_attr('group.cuisine','entity_id')) | selectattr('state','==','on') | list | length == 1 }}
action:
  - service: light.turn_off
    target:
      entity_id: "{{ trigger.entity_id }}"
mode: single
1 Like

Hi there.

Your are so right, it is much more efficient, thank you ! :+1: