Skip to content

Instantly share code, notes, and snippets.

@cmsj
Created September 26, 2023 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmsj/17707a60d08bee94e2d5b05eb7c973f9 to your computer and use it in GitHub Desktop.
Save cmsj/17707a60d08bee94e2d5b05eb7c973f9 to your computer and use it in GitHub Desktop.
A Home Assistant custom script for turning on zigbee lightbulbs in a less awful way than default.
hue_nicely:
alias: A Hue Nicely
mode: queued
fields:
bulbs:
name: Bulbs
description: Turn zigbee-connected Hue bulbs on smoothly
required: true
selector:
entity:
domain: light
multiple: true
color_temp:
name: Color Temperature
description: Color Temperature
selector:
color_temp: {}
color_rgb:
name: RGB Color
description: RGB Color
selector:
color_rgb: {}
brightness:
name: Brightness
description: Brightness percentage
selector:
number:
min: 0
max: 100
transition:
name: Transition
description: Time to transition to the new state
selector:
number:
min: 0
max: 10000
variables:
color_temp: '{{ color_temp|default("") }}'
color_rgb: '{{ color_rgb|default("") }}'
brightness: '{{ brightness|default("") }}'
transition: '{{ transition|default(0) }}'
light_pre_options: >
{% macro add_string(key, value) -%}
"{{ key }}":"{{ value }}",
{%- endmacro %} {% macro add_list(key, value) -%}
"{{ key }}": [{{ value|join(",") }}],
{%- endmacro %}
{% set option_dict = namespace(value="") %}
{% if color_rgb != "" %}
{% set option_dict.value = option_dict.value ~ add_list("rgb_color", color_rgb) %}
{% elif color_temp != "" %}
{% set option_dict.value = option_dict.value ~ add_string("color_temp", color_temp) %}
{% endif %}
{{ option_dict.value[:-1] }}
light_on_options: >
{% macro add_string(key, value) -%}
"{{ key }}":"{{ value }}",
{%- endmacro %} {% macro add_list(key, value) -%}
"{{ key }}": [{{ value|join(",") }}],
{%- endmacro %}
{% set option_dict = namespace(value="") %}
{% if brightness != "" %}
{% set option_dict.value = option_dict.value ~ add_string("brightness_pct", brightness) %}
{% endif %}
{% if color_rgb != "" %}
{% set option_dict.value = option_dict.value ~ add_list("rgb_color", color_rgb) %}
{% elif color_temp != "" %}
{% set option_dict.value = option_dict.value ~ add_string("color_temp", color_temp) %}
{% set option_dict.value = option_dict.value ~ add_string("transition", transition) %}
{% endif %}
{{ option_dict.value[:-1] }}
sequence:
- repeat:
for_each: '{{bulbs}}'
sequence:
- if:
- condition: template
value_template: '{{ is_state(repeat.item, "off") }}'
then:
- service: light.turn_on
data:
brightness: 2
transition: 0
target:
entity_id: '{{repeat.item}}'
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 200
- service: light.turn_on
data: '{ {{ light_pre_options }}, "transition":0 }'
target:
entity_id: '{{repeat.item}}'
- delay:
hours: 0
minutes: 0
seconds: 0
milliseconds: 200
- service: light.turn_on
data: '{ {{ light_on_options }}, "transition":0.2 }'
target:
entity_id: '{{repeat.item}}'
@cmsj
Copy link
Author

cmsj commented Sep 26, 2023

Save that yaml file into your Home Assistant's config directory and then include it into configuration.yaml like this:

script hue_nicely: !include hue_nicely.yaml

Because the script specifies its input fields, it works with the visual editor:
Screenshot 2023-09-26 at 11 10 22

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