Skip to content

Instantly share code, notes, and snippets.

@jerkovicl
Forked from dglaude/1st_readme.txt
Created March 17, 2021 17:48
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 jerkovicl/3b65e23d57894c5e21d3d4eda6ec013c to your computer and use it in GitHub Desktop.
Save jerkovicl/3b65e23d57894c5e21d3d4eda6ec013c 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 }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment