Skip to content

Instantly share code, notes, and snippets.

@dglaude
Last active April 28, 2024 12:40
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dglaude/619a9e415e1e34019a6b1f5f274e0d14 to your computer and use it in GitHub Desktop.
Save dglaude/619a9e415e1e34019a6b1f5f274e0d14 to your computer and use it in GitHub Desktop.
Home Assistant Rainbow Loop with Ikea Tradfri RGB light
Please find below the various piece of code that together control my RGB light to loop in Rainbow.
Every two seconds, it change from one colour to another based on the value of the second.
So it compute 30 differents RGB value in a "circle", all with the same Saturation and Brightness both forced to 1.0
The transition from one colour to another is done in one seconds.
The name of my Tradfri RGB light bulb is "light.couleur"
A link to my video on Twitter:
https://twitter.com/DavidGlaude/status/1059596285991366657
Source code for the Hue based RGB computation from last post of this:
https://community.home-assistant.io/t/hue-random-color-autiomation-help/52122/10
PS: I am totally not a Home Assistant expert, so there might be better way to implement this.
Fell free to comment or propose better way to control that.
Such as verifying if the light is on (as a condition) before changing the RGB colour.
Anything really, today I just want it to work, but I am ready to learn.
### ###
### LightsHue - Rotating Hue Every Two Seconds ###
### ###
- alias: LightsHue - Rotating Hue Every Two Seconds
initial_state: True
trigger:
- platform: time
seconds: '/2'
action:
- service: script.change_exterior
### Possibly unrelated and not required piece of code... ###
### This is checking every minute the availability of the light bulb. ###
### By setting the brightness to current brightness it force the gateway to report the unavailable status. ###
### This code was found here: https://github.com/home-assistant/home-assistant/issues/17675 ###
- alias: "unavailable_couleur"
initial_state: 'on'
trigger:
platform: time
minutes: '/1'
seconds: 10
condition:
condition: template
value_template: >
{{ states('light.couleur') != 'unavailable'}}
action:
service: light.turn_on
data_template:
entity_id: light.couleur
brightness: >
{{ state_attr('light.couleur','brightness') | int }}
# Your "configuration.yaml" need to countain a reference to your automations.yaml and your scripts.yaml.
# In my case, I don't use "automations.yaml" but my config is split in various files in "automation" folder.
# YMMV
[...]
automation: !include_dir_merge_list automation/
#automation: !include automations.yaml
script: !include scripts.yaml
change_exterior:
alias: Change Exterior Light
sequence:
- service: script.preprocess_color
data:
id: light.couleur
# this script choses a random color for the lights
preprocess_color:
sequence:
- service: script.set_color
data_template:
id: '{{ id }}'
colors: >-
{%- set h = (now().second|int)/60 %}
{%- set s = 1.0 %}
{%- set v = 1.0 %}
{%- set i = (h * 6)|int %}
{%- set f = h * 6 - i %}
{%- set p = v * (1 - s) %}
{%- set q = v * (1 - f * s) %}
{%- set t = v * (1 - (1 - f) * s) %}
{%- if i % 6 == 0 %}
{% set r = v %}
{% set g = t %}
{% set b = p %}
{%- elif i % 6 == 1 %}
{% set r = q %}
{% set g = v %}
{% set b = p %}
{%- elif i % 6 == 2 %}
{% set r = p %}
{% set g = v %}
{% set b = t %}
{%- elif i % 6 == 3 %}
{% set r = p %}
{% set g = q %}
{% set b = v %}
{%- elif i % 6 == 4 %}
{% set r = t %}
{% set g = p %}
{% set b = v %}
{%- elif i % 6 == 5 %}
{% set r = v %}
{% set g = p %}
{% set b = q %}
{%- endif %}
{{ (255*r)|round(0) }}, {{ (255*g)|round(0) }}, {{ (255*b)|round(0) }}, {{ (360*h)|int }}
transition: 1
set_color:
sequence:
- service: light.turn_on
data_template:
entity_id: '{{ id }}'
rgb_color: [ "{{ colors.split(',')[0]|int }}", "{{ colors.split(',')[1]|int }}", "{{ colors.split(',')[2]|int }}" ]
transition: "{{ transition }}"
@loic-moriame
Copy link

loic-moriame commented Nov 25, 2021

Hi all is working nice. Is there a way to use much more colours (like 6 of them) and make transitions smoother?

Sure!
For transition, just change the value of transition parameter

If you want more colors, just change the modulo value (3 to 6 for example) and add more conditions
Let me know if you want some help

P.S: I try to use an array for RGB colors but without success, that why I switch to multiple conditions inside the script

@Gavergracht
Copy link

My son can come out of bed when the light is green. when the light is red or orange, it is to early to wake up. How do I make such an automation ? And is it possible to enter a "time" when it has to be green ?
so setting the time at 07.00 ( not hard coded) ; starting at 06:40 with red for 10 minutes, 06:50 orange for 10 minutes and than at 07:00 half an our or so green ?

@willyfog78
Copy link

willyfog78 commented Nov 26, 2021

Hi I'm trying to achieve change of colour every 5 minutes with smooth transition of colours.

I cannot figure out function if with modulo. I got this but it only works every 5 seconds set in automation. Any other automation timing results in skipping colours.

`alias: Christmas strip led
description: Rotating color every second
trigger:

  • hours: ''
    platform: time_pattern
    minutes: '
    '
    seconds: /5
    condition: []
    action:
  • service: script.1637668450615
    mode: single
    automation:alias: Christmas strip led
    description: Rotating color every second
    trigger:
  • hours: ''
    platform: time_pattern
    minutes: '
    '
    seconds: /5
    condition: []
    action:
  • service: script.1637668450615
    mode: single
    `

@loic-moriame
Copy link

@Gavergracht : your question is out of the scope of this gist. I suggest you to ask directly on the forum of home-assistant :)
@willyfog78 : you should change the timer to execute it every 5min and in the script, change the condition by something like now().minute % 5 == 1

@hugalafutro
Copy link

@loic-moriame I took your work and created a Home Assistant Blueprint from it (I credited you), I hope it's ok.
https://community.home-assistant.io/t/color-loop-effect-for-any-light-with-color-wheel-selector/389343

@loic-moriame
Copy link

@hugalafutro: this is definitely in the spirit of open source and knowledge sharing.
I more than agree with that 👍

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