Dimming only lights that on

Sorry if this has been covered elsewhere, I have searched but couldn’t find anything.
I have 9 lights in my living room, each are IKEA ZigBee bulbs, each configured and working in Hass via phoscon.
I have scenes set for different activities, like watching movies, relaxing etc.
I want to use my harmony remote to dim or brighten the lights when needed. I have an emulated Roku and am successfully picking up the keypresses, I am able to trigger the scenes from the harmony remote
How would I write an automation or script to determine which lights are currently in use and only brighten or dim them when I press a button. Sometimes I want to brighten just the lights that are on without turning on the ones that are off.
Thanks in advance for your help.

I would create an input_select populated with a name for each Scene (which you could also use as a drop-down selector in the UI if wanted), then use a data_template to define the groups for each Scene name in your Automation. eg:

input_select:

name: Lighting Scenes
  options:
    - Watching Movies
    - Relaxing
    - etc.

automation:
Written as usual (I’m not familiar with the Harmony Remote component or actions) but with
a data_template defining entity_id

...
      data_template:
        entity_id: >
          {% if is_state("input_select.lighting_scenes", "Watching Movies") %}
            light.1,light.2
          {% elif is_state("input_select.lighting_scenes", "Relaxing") %}
            light.2,light.3
          {% elif is_state("input_select.lighting_scenes", "etc.") %}
            light.3,light.4
          {% endif %}
  ...
1 Like

Thanks, so for each scene you would manually configure which lights would be dimmable?

I think so. I don’t know of any way to parse a Scene configuration for entity_ids.

A template can be devised to report all the lights in your house that are currently on or all the lights in a given group that are on.

You have defined lights in a scene, so the first case, checking all lights, may be too broad for your purpose. The second case works with lights in a group so, unless you were to do that, it won’t work with your scenes.

The template can only focus on the lights in a given scene but you will have to list all those lights within the template.


EDIT
While writing my post, petro has supplied you with the first template example, for all lights in your house.

1 Like

if you just want to dim lights that are currently on with a button press via a script, this would be the action.

increase brightness by 5

  service: light.turn_on
  data_template:
    entity_id: >
      {{ states.light | selectattr('state','eq','on') | map(attribute='entity_id') | list | join(', ') }}
    brightnes: >
      {% set current_levels =  states.light | selectattr('state','eq','on') | map(attribute='attributes.brightness') | list %}
      {% set current_avg = current_levels | sum / current_levels | length %}
      {{ 255.0 if current_avg + 5 > 255 else current_avg + 5 }}

decrease brightness by 5

  service: light.turn_on
  data_template:
    entity_id: >
      {{ states.light | selectattr('state','eq','on') | map(attribute='entity_id') | list | join(', ') }}
    brightnes: >
      {% set current_levels =  states.light | selectattr('state','eq','on') | map(attribute='attributes.brightness') | list %}
      {% set current_avg = current_levels | sum / current_levels | length %}
      {{ 0.0 if current_avg - 5 < 0 else current_avg - 5 }}

These get the average brightness across all lights that are on, and adds 5. You can add any number increase or decrease. I just chose 5.

3 Likes

Thanks, very helpful.
All the lights controlled by the scene are in the lounge. Is there a way to filter it down to just the lounge group?

Otherwise I may find i am dimming or brightening lights in other rooms also.

Yes and if you’re using 0.96 you can take advantage of the nifty new expand function.

In petro’s template, replace all instances of:

states.light

with:

expand('group.lounge_lights')

Obviously you will use the actual name of the group containing your lounge lights.

Using petro’s first example:

  service: light.turn_on
  data_template:
    entity_id: >
      {{ expand('group.lounge_lights') | selectattr('state','eq','on') | map(attribute='entity_id') | list | join(', ') }}
    brightness: >
      {% set current_levels =  expand('group.lounge_lights') | selectattr('state','eq','on') | map(attribute='attributes.brightness') | list %}
      {% set current_avg = current_levels | sum / current_levels | length %}
      {{ 255.0 if current_avg + 5 > 255 else current_avg + 5 }}
1 Like

That’s great thanks.
Yep, I’m running 0.96.5
The expand feature will come in handy, nice.
Sometimes it so hard to find things in Hass, I was looking for a service.light_brightness or service.light_dim :joy:
Thanks again

I get the following error from the script from @petro

Error: Error rendering data template: UndefinedError: 'homeassistant.util.read_only_dict.ReadOnlyDict object' has no attribute 'brightness'

1 Like

sorry for my ignorance, that’s exactly what I was looking for but I didn’t understand where to put the script…can you help me please?

You put it in the script section, configuration.yaml or the UI

@Ampe1 I had the same issue until I noticed that there’s a typo in the example code from petro. You just have to add an “s” to “brightnes: >”. Took me forever to figure out. The risks of copy-paste I guess : )

What I couldn’t figure out was how to modify the above exampe: Instead of calculating the average brightness of all lights in the group and then add a value to the average and set that new total value as the brigthness for each light I wanted to e.g. add a value to the brightness of each light in the group individually (capped at 255).

Example brightness before: light1=120, light2=50, light3=252
Petro’s code does this: light1=146, light2=146, light3=146 (based on the average of 141)
But I want to have this outcome: light1=125, light2=55, light3=255

Here’s my (not at all working) script:

alias: 1_bn_l_dim_all_active_up
mode: single
sequence:
  - service: light.turn_on
    data_template:
      entity_id: >
        {{ expand('light.1_bn_l_all') 
        | selectattr('state','eq','on') 
        | map(attribute='entity_id') | list | join(', ') }}
      brightness: >
        {% set current_levels = expand('light.1_bn_l_all') 
        | rejectattr('attributes.brightness', 'undefined') 
        | selectattr('state','eq','on') 
        | map(attribute='attributes.brightness') 
        | list %} 
        {% for level in current_levels %}
          {{ 255 if level + 26 > 255 else level + 26 }}
        {% endfor %}

Somehow I fear the answer is a completely seperate approach as I am setting brightness for a group item. Not sure how to allocate the individual brightness levels to individual lights in the group.

alias: 1_bn_l_dim_all_active_up
mode: single
variables:
  light: light.1_bn_l_all
  step: 5
  lights: >
    {{ expand(light) | map(attrbute='entity_id') | list }}
  next_levels: >
    {% set ns = namespace(items=[]) %}
    {% for l in lights | select('is_state', 'on') %}
      {% set current = state_attr(l, 'brightness') %}
      {% if current is not none %}
        {% set ns.items = ns.items + [{'entity_id': l, 'brightness': current + step if current + step <= 255 else 255}] %}
      {% endif %}
    {% endfor %}
    {{ ns.items }}
sequence:
- repeat:
    for_each: "{{ next_levels }}"
    sequence:
    - service: light.turn_on
      data: "{{ repeat.item }}"

Wow that really looks very different from the other avg-light-approach.
Many thanks it works great!