Iterate through group of light

Hi everyone,

It’s been a long time now I’ve been looking for this and I can’t figure out how to achieve this.

I currently run a script every evening that decreases my lights brightness every 5 mins until they’re all “off” and I like that my living room slowly dims the light off at bedtime.
I’ve already set the bedtime as a parameter using time helper.

The point is I had currently hardcoded the script with all the light concerned, it works but I’m feeling bad about the way I achieve this. I don’t like the idea I would need to modify the script every time I Add/Remove a light in the living room, or that I should do another script for any room I’d want to replicate this behavior.

I’d like to modify this script like receiving a group/room as an input so it goes like :

var group = !input light_group
repeat: 
  Foreach light in group:
    light->decrease_brightness
  end foreach
until group is off

Any idea how I could script this in a elegant way ?

Thanks !

Post the script you are currently using (I assume by “script” you mean the Scripts integration).

Hi Taras,

Thanks for your interest, I actually run this in nested scripts

This one just decrease all the light in my living room

decrease_brightness_livingroom:
  alias: Decrease bulbs brightness
  sequence:
  - type: brightness_decrease
    device_id: f5614aebbf231b024f9ab5faec9f03e9
    entity_id: light.plafonnier_5
    domain: light
  - type: brightness_decrease
    device_id: 866381bc05bbd394efba5c606bcf80a6
    entity_id: light.plafonnier_3
    domain: light
  - type: brightness_decrease
    device_id: 69a1b213be17d1d425bc82d34cdf7779
    entity_id: light.light_desk
    domain: light
  - type: brightness_decrease
    device_id: 022fc14b088841a92bd6707a9593839e
    entity_id: light.lampe_table_a_manger_2
    domain: light
  - type: brightness_decrease
    device_id: c03adab590e55803e29dbe2d3aa7c619
    entity_id: light.bar_4
    domain: light
  - type: brightness_decrease
    device_id: f54afd1b2ae8e6f8f96190d679dfcbe3
    entity_id: light.ruban_led
    domain: light
  - type: brightness_decrease
    device_id: ff90cb46e2c661466f6178367ee9f51d
    entity_id: light.tv_bulb
    domain: light
  - type: brightness_decrease
    device_id: 53c08ccf93a44ed10cb2d477b90c5b11
    entity_id: light.halogene
    domain: light
  mode: queued
  max: 10

It is called every 5 min by this one until my group is off :

slowly_light_off_livingroom:
  alias: Slowly turn off lights
  sequence:
  - repeat:
      until:
      - condition: state
        entity_id: group.livingroom_bulbs
        state: 'off'
      sequence:
      - service: script.decrease_brightness_livingroom
        data: {}
      - delay: 00:05:00
  - type: turn_off
    device_id: b7ca3a091f0672fb1efdedded7cd85e7
    entity_id: switch.guirlande
    domain: switch
  mode: single

Don’t pay attention to the last action, I have a light strip on a connected plug so I just turn it off once every light is off. But this has nothing to do with my purpose of iterate the decrease_brightness through a group of light.

And yep you’re right I made those scripts with the script integration “UI” but I don’t mind going with YAML script. I’m not just very familiar with the YAML script syntax yet, especially when it comes to template.

Thanks for your help

Have been asking the same question. Came up with a python script to manage this. Find it in this thread:

Hi Crashmatt

After moving to a new place I started a clean install of my HA setup.
I also ended up using a python script via AppDaemon to manage this.

For those interested, I forked this script and modify it to meet my needs.

Cheers