In order to phase out the Hue Bridge, I was looking for a way to have nicely colored scenes in HA, similar to the Hue ones. I am only aware of this discussion, but it relies on the hue bridge.
My solution relies on three parts:
1. The script
It basically supports the following things:
- Select a room or a couple of lights.
- Select a scene by name.
- The scene is associated with a few color, that are distributed randomly to the participating lights.
- It’s possible to only apply to lights that are already on.
- It’s possible to skip light groups.
- We don’t want to accidentally turn on lights that cannot be colored.
Aaaand here it is (also posted as a GitHub Gist here):
alias: Light / Hue Scene
description: >-
This script tries to replicate the Hue scenes. Colors are distributed randomly
on participating lights. Each scene currently has five different colors
represented by XY values. The script is only applied to lights that support XY
color mode.
fields:
target:
name: Target
required: true
description: Select one or more areas or light entities.
selector:
target:
entity:
domain: light
scene:
name: Scene
description: Which scene? Scenes are taken from the Hue app.
required: true
default: Savanna Sunset
selector:
select:
options:
- Savanna Sunset
- Golden Pond
- Horizon
- Frosty Dawn
onlyonlights:
name: Only lights currently on?
description: If enabled, the scene is only applied to the lights currently on.
required: false
default: false
selector:
boolean: null
skipgroups:
name: Skip groups
description: If enabled, group light entities will be skipped.
required: false
default: true
selector:
boolean: null
sequence:
- variables:
colors: |-
{% set scenes = {
"Savanna Sunset": {
"colors": [[0.644, 0.3348],[0.5246,
0.3864],[0.4801, 0.4309],[0.5862, 0.3575],[0.4162, 0.4341]]
},
"Golden Pond": {
"colors": [[0.5695, 0.3999],[0.482,
0.4489],[0.496, 0.4424],[0.5584, 0.4083],[0.5063, 0.4474]]
},
"Horizon": {
"colors": [[0.2779, 0.2188],[0.1811,
0.1979],[0.5247, 0.3877],[0.592, 0.385],[0.1731, 0.1978]]
},
"Frosty Dawn": {
"colors": [[0.4221, 0.386],[0.387,
0.4328],[0.4013, 0.4172],[0.439, 0.3782],[0.4675, 0.3769]]
}
}%}
{{scenes[scene].colors}}
lights: |-
{% set l=[]%}
{% if target.area_id %}
{% if target.area_id is iterable and not target.area_id is string %}
{% for a in target.area_id %}
{% set l = l + area_entities(a)|select('match', 'light.')|list %}
{% endfor %}
{% else %}
{% set l = l + area_entities(target.area_id)|select('match', 'light.')|list %}
{% endif %}
{% endif %}
{% if target.entity_id %}
{% if target.entity_id is iterable and not target.entity_id is string %}
{% set l = l + (target.entity_id|list) %}
{% else %}
{% set l = l + [target.entity_id] %}
{% endif %}
{% endif %}
{% if onlyonlights %}
{% set l = l| select('is_state', 'on')%}
{% endif %}
{% if skipgroups %}
{% set l|from_json %}
[{%- for ll in l -%}
{%- if not state_attr(ll, "entity_id")-%}
"{{ ll }}"
{%- if not loop.last-%},{%-endif-%}
{%-endif-%}
{%- endfor -%}]
{% endset %}
{% endif %}
[{%- for ll in l %}
{%- set colormodes = state_attr(ll, "supported_color_modes") -%}
{%- if "xy" in colormodes -%}
"{{ ll }}"
{%- if not loop.last-%},{%-endif-%}
{%- endif -%}
{%- endfor -%}]
- repeat:
for_each: "{{ lights }}"
sequence:
- service: light.turn_on
data:
xy_color: "{{ colors|random}}"
target:
entity_id: "{{ repeat.item }}"
mode: single
2. The colors
The next question: How to get the colors? The Hue app shows them in small circles, but I thought there needs to be a better way to extract the colors. Since I still have the bridge running, I applied the scene to a number of lights, and extracted the color info via Home Assistant with this template script:
{%- set lightstates -%}
[{%- for l in ["light.example1", "light.example2"] -%}
"{{state_attr(l,"xy_color")|list }}"
{% if not loop.last %},{%endif%}
{%- endfor -%}]
{%- endset -%}
{% for xy in lightstates|from_json|unique -%}
{{xy}}
{% if not loop.last %},{%endif%}
{%- endfor -%}
I used the template tool in the developer section of HA to run this, and then copied the colors into the script.
3. The UI
My dashboards are heavily based on the entities and auto-entities cards, so I wanted to integrated these scenes into that, and came up with the above UI. It uses paper-buttons-row to show the buttons. A short tap activates the scene on all lights currently on, and a long tap activates it on all lights in the area.
- type: custom:paper-buttons-row
base_config:
name: false
styles:
icon:
height: 64px;
width: 64px;
tap_action:
action: call-service
service: script.light_hue_scene
service_data:
onlyonlights: true
skipgroups: true
target:
area_id: wohnzimmer
hold_action:
action: call-service
service: script.light_hue_scene
service_data:
onlyonlights: false
skipgroups: true
target:
area_id: wohnzimmer
buttons:
- entity: script.light_hue_scene
image: /local/hue/savanna-sunset.jpg
tap_action:
service_data:
scene: Savanna Sunset
hold_action:
service_data:
scene: Savanna Sunset
- entity: script.light_hue_scene
image: /local/hue/horizon.jpg
tap_action:
service_data:
scene: Horizon
hold_action:
service_data:
scene: Horizon
- entity: script.light_hue_scene
image: /local/hue/golden-pond.jpg
tap_action:
service_data:
scene: Golden Pond
hold_action:
service_data:
scene: Golden Pond
Final thing, because it took me far too long to notice: The Hue scene images are creative commons images, and are linked within the “About” section of the app. From these links (most of them to flickr) they can easily be installed in HA.
Enjoy!