While there are multiple topics that deal with controlling lights that are on by creating a group of lights inside Home Assistant, they ultimately work by sending individual commands to each light in the external platform. Although lights can be grouped outside Home Assistant by creating a super group that controls all the lights in the house and since sending commands to change lights behaviour also turns them on, it also creates unexpected situations where only wanting to change a single light would turn the whole house on.
Also problems: when using automated control of lights (ie. Circadian Lighting [Custom Component]) this floods the logbook quite a lot. More, when controlling adjacent indvidual lights, there might be cases when there are delays between commands (not necesarily an issue).
There are lights that can natively create and control groups outside Home Assistant (I only used Philips Hue and MiLight but there are other brands that are capable of doing this) which don’t result in flickering when sending commands to larger groups of lights.
As I’m not a software developer to be able to write a custom component, I can still use REST commands to interact with the lights.
Requirements: use Hue app to set a group that includes all the light needed to be controlled (in my case “81”), change ip of the Hue hub and the API.
Package
input_number:
hue_virtual_light_brightness:
name: "Hue Virtual Light Brightness"
initial: 255
min: 1
max: 255
step: 1
hue_virtual_light_color_temp:
name: "Hue Virtual Light Color Temperature"
initial: 370
min: 153
max: 500
step: 1
hue_virtual_light_hue:
name: "Hue Virtual Light Hue"
initial: 0
min: 0
max: 360
step: 1
hue_virtual_light_saturation:
name: "Hue Virtual Light Saturation"
initial: 100
min: 0
max: 100
step: 1
light:
- platform: template
lights:
hue_virtual_light:
friendly_name: "Hue Virtual Light"
level_template: "{{ states('input_number.hue_virtual_light_brightness') | int }}"
value_template: "{{ states('input_number.hue_virtual_light_brightness') | int > 0 }}"
temperature_template: "{{ states('input_number.hue_virtual_light_color_temp') | int }}"
hs_template: "[{{ states('input_number.hue_virtual_light_hue') | float }}, {{ states('input_number.hue_virtual_light_saturation') | float }}]"
turn_on:
service: input_number.set_value
data:
entity_id: input_number.hue_virtual_light_brightness
value: 255
turn_off:
service: input_number.set_value
data:
entity_id: input_number.hue_virtual_light_brightness
value: 1
set_level:
service: input_number.set_value
data:
entity_id: input_number.hue_virtual_light_brightness
value: "{{ brightness }}"
set_temperature:
service: input_number.set_value
data:
entity_id: input_number.hue_virtual_light_color_temp
value: "{{ color_temp }}"
set_hs:
- service: input_number.set_value
data:
entity_id: input_number.hue_virtual_light_hue
value: "{{ h | float }}"
- service: input_number.set_value
data:
entity_id: input_number.hue_virtual_light_saturation
value: "{{ s | float }}"
script:
# Set the brightness of the actual Hue lights using the REST command
set_hue_brightness:
alias: "Set Hue Brightness"
sequence:
- service: rest_command.set_hue_light_brightness
data_template:
light_id: "81"
brightness: "{{ brightness }}"
# Set the color temperature of the actual Hue lights using the REST command
set_hue_color_temp:
alias: "Set Hue Color Temperature"
sequence:
- service: rest_command.set_hue_light_color_temp
data_template:
light_id: "81"
color_temp: "{{ color_temp }}"
# Set the xy color of the actual Hue lights using the REST command
set_hue_xy_color:
alias: "Set Hue XY Color"
sequence:
- variables:
hue: "{{ states('input_number.hue_virtual_light_hue') | float }}"
saturation: "{{ states('input_number.hue_virtual_light_saturation') | float }}"
xy: >
{% set h = hue / 360 %}
{% set s = saturation / 100 %}
{% set v = 1 %}
{% set r, g, b = (0, 0, 0) %}
{% if s == 0 %}
{% set r, g, b = (v, v, v) %}
{% else %}
{% set i = (h * 6) | int %}
{% set f = (h * 6) - i %}
{% set p = v * (1 - s) %}
{% set q = v * (1 - s * f) %}
{% set t = v * (1 - s * (1 - f)) %}
{% if i == 0 %}
{% set r, g, b = (v, t, p) %}
{% elif i == 1 %}
{% set r, g, b = (q, v, p) %}
{% elif i == 2 %}
{% set r, g, b = (p, v, t) %}
{% elif i == 3 %}
{% set r, g, b = (p, q, v) %}
{% elif i == 4 %}
{% set r, g, b = (t, p, v) %}
{% else %}
{% set r, g, b = (v, p, q) %}
{% endif %}
{% endif %}
{% set x = r * 0.664511 + g * 0.154324 + b * 0.162028 %}
{% set y = r * 0.283881 + g * 0.668433 + b * 0.047685 %}
{% set z = r * 0.000088 + g * 0.072310 + b * 0.986039 %}
{% set x = x / (x + y + z) %}
{% set y = y / (x + y + z) %}
[{{ x | round(5) }}, {{ y | round(5) }}]
- service: rest_command.set_hue_light_xy_color
data_template:
light_id: "81"
xy_color: "{{ xy }}"
automation:
# Sync the virtual light brightness with the actual Hue lights
- alias: 'Sync Hue Virtual Light Brightness'
initial_state: true
trigger:
- platform: state
entity_id: input_number.hue_virtual_light_brightness
condition:
- condition: template
value_template: >
{{ trigger.from_state.state != trigger.to_state.state }}
action:
- service: script.set_hue_brightness
data_template:
brightness: "{{ states('input_number.hue_virtual_light_brightness') | int }}"
light_id: "81"
# Sync the virtual light color temperature with the actual Hue lights
- alias: 'Sync Hue Virtual Light Color Temperature'
initial_state: true
trigger:
- platform: state
entity_id: input_number.hue_virtual_light_color_temp
condition:
- condition: template
value_template: >
{{ trigger.from_state.state != trigger.to_state.state }}
action:
- service: script.set_hue_color_temp
data_template:
color_temp: "{{ states('input_number.hue_virtual_light_color_temp') | int }}"
light_id: "81"
# Sync the virtual light hs color with the actual Hue lights
- alias: 'Sync Hue Virtual Light HS Color'
initial_state: true
trigger:
- platform: state
entity_id: input_number.hue_virtual_light_hue
- platform: state
entity_id: input_number.hue_virtual_light_saturation
condition:
- condition: template
value_template: >
{{ trigger.from_state.state != trigger.to_state.state }}
action:
- service: script.turn_off
target:
entity_id: script.set_hue_xy_color
- service: script.turn_on
target:
entity_id: script.set_hue_xy_color
rest_command:
# Use Philips Hue API to set the brightness of the lights
set_hue_light_brightness:
url: 'http://192.168.x.y/api/aaaaaaabbbbbbbbccccccccddddddddeeeeeeeefffffffffffff/groups/81/action'
method: 'PUT'
headers:
content-type: 'application/json'
payload: >
{
"bri": {{ brightness }}
}
# Use Philips Hue API to set the color temperature of the lights
set_hue_light_color_temp:
url: 'http://192.168.x.y/api/aaaaaaabbbbbbbbccccccccddddddddeeeeeeeefffffffffffff/groups/81/action'
method: 'PUT'
headers:
content-type: 'application/json'
payload: >
{
"ct": {{ color_temp }}
}
# Use Philips Hue API to set the xy color of the lights
set_hue_light_xy_color:
url: 'http://192.168.x.y/api/aaaaaaabbbbbbbbccccccccddddddddeeeeeeeefffffffffffff/groups/81/action'
method: 'PUT'
headers:
content-type: 'application/json'
payload: >
{
"xy": [
{{ state_attr('light.hue_virtual_light', 'xy_color')[0] | float }},
{{ state_attr('light.hue_virtual_light', 'xy_color')[1] | float }}
]
}