I tried to go the templating route for this but I just couldn’t get it working. For any other slow-learners like me there is a quick and dirty solution here and that’s to have an automation that updates the brightness after it’s set. Personally I wanted to set a 10% cap on the brightness. If it helps anyone:
Dig your solution for the brightness problem, really don’t like how it works natively for groups. Did you find a way to reuse it better? My initial though was just to move the set level logic into a script for re-use, but there is still a big template you need to create for each group.
Sorry for the late reply! Looking at my current code, sadly no.
Even with scripts, if I’m not mistaken I’d have to make scripts for each one of the level_template, value_template, turn_on, turn_off, set_level and so on…
I’m thinking perhaps the best way forward would be to put all this logic inside a python script and try to create an even better and improved light group altogether One day perhaps…
I’m currently getting the following error when trying to set a brightness for the group when all lights are off. I can’t see where to add the default value to fix this?
Error rendering service target template: ValueError: Template error: int got invalid input 'None' when rendering template '{% set brightness_from = state_attr('light.living_room_lights_smart', 'brightness')|int %} {% set brightness_change = brightness - brightness_from %} {{ expand("group.living_room_dimmable") | selectattr('attributes.brightness') | selectattr('attributes.brightness', '>', -brightness_change) | map(attribute='entity_id', default=0) | list or expand('group.living_room_dimmable') | selectattr('domain', '==', 'light') | map(attribute='entity_id', default=0) | list if brightness != 255 else [0] }}' but no default was specified
Looking through your error message and comparing your code to mine, I think the only thing that differs that might cause this could be the very last line…
It seems the last thing you’re doing is
list if brightness != 255 else [0]
while I on the other hand am doing
list if brightness != 255 else []
Try changing from [0] to just [] and see if that helps.
I would like to have this solution applied to different media player volumes as I have smart speakers spread within my home. Similar to the lights, I would like to have each room to be set accordingly so that when the master slider is moved, the individual rooms adjust, not equal to the master slider.
I am not versed in YAML. Could someone point me in the right direction for this volume solution? Thanks!
Hello,
I would first like to thank you for this template, it was exactly what I was looking for, you saved me quite a bit of time.
However, I have the exact same problem as neilnose, the same error message happens when using the slider while all lights are off.
I’ve set the “else []”, the problem isn’t coming from this part.
Do you have any idea what “but no default was specified” is refering to ?
Thanks for asking this - I’ve been looking for the same thing.
I wish HA would incorporate a similiar feature.
Google Home will present a group of lights and relative-dim them all similar to how your template does.
‘Areas’ seems a logical place for it - extend what an Area can do by imbuing it with control abilities and offer them as ‘fixed’ or ‘relative’ to achieve what you have here, but without having to do all the coding for each one.
Sorry i thought i had replied to the above post which had the identcal error.
Failed to call service light/turn_on. Error rendering service target template: ValueError: Template error: int got invalid input ‘None’ when rendering template ‘{% set brightness_from = state_attr(‘light.office_lights_smart’, ‘brightness’)|int %} {% set brightness_change = brightness - brightness_from %} {{ expand(“group.office”) | selectattr(‘attributes.brightness’) | selectattr(‘attributes.brightness’, ‘>’, -brightness_change) | map(attribute=‘entity_id’) | list or expand(‘group.office’) | selectattr(‘domain’, ‘==’, ‘light’) | map(attribute=‘entity_id’) | list if brightness != 255 else }}’ but no default was specified
It’s the same I answered above: Give int a default value.
That is the original code with default values for brightness and int, using a UI generated light group:
light:
- platform: template
lights:
test_lights_smart:
friendly_name: "Test lights smart"
# Average of all lights currently on
level_template: >
{{ ( expand('light.test')
| map(attribute='attributes.brightness', default=0)
|map('int', -1)
| select("greaterthan", 0)
| list or [0])
| average
| round }}
# True if any light has a brightness larger than 0
value_template: >
{{ expand('light.test')
| map(attribute='attributes.brightness', default=0)
|map('int', -1)
| sum > 0 }}
turn_on:
service: light.turn_on
target:
entity_id: light.test
turn_off:
service: light.turn_off
target:
entity_id: light.test
set_level:
# Set brightness by amount to all lights (when all are off), OR only the lights that would remain on by the brightness change
- service: light.turn_on
data:
brightness_step: "{{ brightness |int(0) - state_attr('light.test_lights_smart', 'brightness')|int(0) }}"
target:
entity_id: >
{% set brightness_from = state_attr('light.test_lights_smart', 'brightness')|int(0) %}
{% set brightness_change = brightness |int(0) - brightness_from %}
{{ expand('light.test')
| selectattr('attributes.brightness')
| selectattr('attributes.brightness', '>', -brightness_change)
| map(attribute='entity_id')
| list or
expand('light.test')
| selectattr('domain', '==', 'light')
| map(attribute='entity_id')
| list if brightness != 255 else [] }}
# Set brightness to 1 for all lights that would turn off, keeping them on
- service: light.turn_on
data:
brightness_pct: 1
target:
entity_id: >
{% set brightness_from = state_attr('light.test_lights_smart', 'brightness')|int(0) %}
{% set brightness_change = brightness |int(0) - brightness_from %}
{{ expand('light.test')
| selectattr('attributes.brightness')
| selectattr('attributes.brightness', '<=', -brightness_change)
| map(attribute='entity_id')
| list
}}
# Set brightness to 100 for all lights currently on when slider moves to 100%, or all the lights if all are currently off
- service: light.turn_on
data:
brightness_pct: 100
target:
entity_id: >
{{ expand('light.test')
| selectattr('attributes.brightness')
| map(attribute='entity_id')
| list or
expand('light.test')
| selectattr('domain', '==', 'light')
| map(attribute='entity_id')
| list if brightness == 255 else [] }}
So please try it out, as I’ll most likely focus my effort on that one over the very convoluted light template approach I created above
Also, a small heads-up that the custom component acts a little bit different - whenever the group brightness is adjusted the individual lights are adjusted proportionally to the group change while the solution above simply increased/decreased each individual light brightness equally as much as the group brightness changed. This should make the behavior more similar to what the Hue app and others do as well. Give it a spin will ya!
This is brilliant. I’ve just got it working (those having problems, don’t forget to use light.NAME where NAME is what you called the room in the configuration.yaml).
If you look into the custom component that I made it should be fairly easy to set up as many “virtual lights” as you want. Try that and if it doesn’t work, let me know in the other thread