Skip to content

Instantly share code, notes, and snippets.

@nilsreiter
Created October 8, 2023 09:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilsreiter/eac1a2dea7e920011793a1901e468e00 to your computer and use it in GitHub Desktop.
Save nilsreiter/eac1a2dea7e920011793a1901e468e00 to your computer and use it in GitHub Desktop.
This is a script I used in Home Assistant to distribute color palettes on participating lights.
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
@nilsreiter
Copy link
Author

An updated version of this can be found here: https://github.com/nilsreiter/home-assistant-scenes/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment