Skip to content

Instantly share code, notes, and snippets.

@dglaude
Last active April 28, 2024 12:40
Show Gist options
  • 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 I think this is exactly what im looking for for my Tuya RGB bulbs! Im a newby with HA (as in 2 days ;-) ) and i was searching my ass off for this function to select the "relax" scene. Since i'm not very well informed about getting this to work, could you enlighten me with a bit of info on where to put what to make this work?

Back from holidays!
@bafplus: I will try to check my HA tonight to let you know how to manage this

@kylewilliams321
Copy link

I haven't got this to work as I stumbled upon this from another thread in Home Assistant forum. However, my question for this is I'm trying to set something up for 3 lights on my patio and would like to cycle through colors for certain holidays. For example, Halloween - Purple and Orange... Christmas - Red, Green ....... 4th of July - Red, White, Blue, etc. You get the gist.

A couple modifications: 1) I think I'd prefer RGB color coding & 2) Is it possible for this to run on an endless loop?

THANKS!

@loic-moriame
Copy link

@loic-moriame I think this is exactly what im looking for for my Tuya RGB bulbs! Im a newby with HA (as in 2 days ;-) ) and i was searching my ass off for this function to select the "relax" scene. Since i'm not very well informed about getting this to work, could you enlighten me with a bit of info on where to put what to make this work?

For the script:
image

For the automation:
image

And I have put a switch on/off on my lovelace interface:
image

@bafplus
Copy link

bafplus commented Nov 23, 2021

Thanks!

@loic-moriame
Copy link

I haven't got this to work as I stumbled upon this from another thread in Home Assistant forum. However, my question for this is I'm trying to set something up for 3 lights on my patio and would like to cycle through colors for certain holidays. For example, Halloween - Purple and Orange... Christmas - Red, Green ....... 4th of July - Red, White, Blue, etc. You get the gist.

A couple modifications: 1) I think I'd prefer RGB color coding & 2) Is it possible for this to run on an endless loop?

THANKS!

You can for sure create new scripts with retricted colors and change the frequency in your automation.
To switch between only limited values in the array, I suggest to used HACS custome_component hass-variables

You can find below the details for Christmas light (you can change the frequency in automation if you want)

Script:

alias: christmas light
sequence:
  - service: light.turn_on
    data:
      rgb_color: |
        {% if now().second % 2 == 0 %}
          [255, 0, 0]
        {% else %}
          [0, 255, 0]
        {% endif %}
      transition: 0
    target:
      entity_id: '{{ entity }}'
mode: single
fields:
  entity:
    description: The entity that will be rainbowed
    example: light.bathroom
icon: mdi:pine-tree

Automation:

alias: Christmas strip led
description: Rotating color every second
trigger:
  - hours: '*'
    minutes: '*'
    platform: time_pattern
    seconds: /5
condition: []
action:
  - service: script.1637666266806
    data:
      entity: light.strip_led
mode: single

Lovelace UI:

type: entities
title: bureau
show_header_toggle: false
entities:
  - type: custom:slider-entity-row
    entity: light.strip_led
    toggle: true
    min: 1
    max: 100
  - entity: automation.christmas_strip_led

@loic-moriame
Copy link

and the script for 4th of July (or 14th of July for french people 😉)

Script:

alias: 4th July light
sequence:
  - service: light.turn_on
    data:
      rgb_color: |
        {% if now().second % 3 == 0 %}
          [0, 0, 255]
        {% elif now().second % 3 == 1 %}
          [0, 0, 0]
        {% else %}
          [255, 0, 0]
        {% endif %}
      transition: 0
    target:
      entity_id: '{{ entity }}'
mode: single
fields:
  entity:
    description: The entity that will be rainbowed
    example: light.bathroom
icon: mdi:firework

@willyfog78
Copy link

and the script for 4th of July (or 14th of July for french people 😉)

Script:

alias: 4th July light
sequence:
  - service: light.turn_on
    data:
      rgb_color: |
        {% if now().second % 3 == 0 %}
          [0, 0, 255]
        {% elif now().second % 3 == 1 %}
          [0, 0, 0]
        {% else %}
          [255, 0, 0]
        {% endif %}
      transition: 0
    target:
      entity_id: '{{ entity }}'
mode: single
fields:
  entity:
    description: The entity that will be rainbowed
    example: light.bathroom
icon: mdi:firework

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

@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