HOW TO: random Christmas lights [HUE]

I have 5 HUE bulbs in the front of my house. I wanted to make the bulbs randomly change between red/green/white. Using some templating that’s possible!

I basically copyedited from Hue Random Color autiomation help and Using HSV / HSB to set colored lights - so thanks :slight_smile:

Step 1: Set up the Scripts (2 of them)

select_hue_light:
  sequence:
  - service: script.set_hue_christmas_color
    data_template:
      light_id: >-
        {% set i = (range(1,6)|random) %}
        {% if i == 1 %}
          {% set chosen_light = "light.porch_west" %}
        {% elif i == 2 %}
          {% set chosen_light = "light.porch_east" %}
        {% elif i == 3 %}
          {% set chosen_light = "light.garage_west" %}
        {% elif i == 4 %}
          {% set chosen_light = "light.garage_east" %}
        {% elif i == 5 %}
          {% set chosen_light = "light.lamp_post" %}
        {% endif %}
        {{ chosen_light }}
      color_name: >-  
        {%- set i = (range(1,6)|random) %}  
        {%- if i == 1 %}  
          {% set color = "red" %}  
        {%- elif i == 2 %}  
          {% set color = "red" %}  
        {%- elif i == 3 %}  
          {% set color = "green" %}  
        {%- elif i == 4 %}  
          {% set color = "green" %}  
        {%- elif i == 5 %}  
          {% set color = "white" %}  
        {%- endif %}  
        {{ color }}  

and

set_hue_christmas_color:
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: '{{ light_id }}'
        color_name: '{{ color_name }}'
        transition: 10

The first script chooses between one of five lights, and chooses between red, red, green, green, and white (more likely to be red or green than white) and then calls the second script which triggers the color change.

Step 2: Now set up the automation. In my automation I have made it trigger the script every 5 seconds “/5” and only after sunset (because I don’t want them on during the day, derrr).

- id: '1544837457650'
  alias: Christmas Hue Light Randomness
  trigger:
  - seconds: /5
    platform: time
  condition:
  - after: sunset
    condition: sun
  action:
  - service: script.select_hue_light

EDIT: It appears that condition: after sunset stops firing after midnight. Open to ideas! In the meantime I changed this to only fire when one of the lights is on.

There are a couple flaws which I don’t care to spend time fixing:

  1. the script will (fairly often) randomly select a light that’s either JUST finished changing, or is still in the process of changing (since it’s running every 5 sec and we set the transition to 10 sec). This could be addressed by re-selecting the light if it’s changed within the last 5 seconds. Don’t care enough to figure out how to do that.
  2. the script will (again, fairly often) select the same color (i.e. red -> red or green -> green). Again, I don’t care enough to fix this but it could be prevented. :slight_smile: (just wanted to point it out before someone else does :stuck_out_tongue:! )
7 Likes

Thanks for the share!

If you want to have this automation run only during the night (so basically from “after dusk” to “beginning of dawn”) you could make use of my period-of-day identification. Simply build your condition like below:

condition:
  - condition: template
    value_template: "{{ states.sensor.period_of_day.state == 'night' }}"
1 Like

or just use:

- condition: sun
  after: sunset
- condition: sun
  before: sunrise

all time and sun conditions are referenced to midnight.

after sunset is sunset till midnight, before sunrise is after midnight till sunrise.

Tried that (from the UI editor). Didn’t work - I presume the editor takes all conditions and then ANDs them together. In this case, it cannot be both conditions at once because it seems they are mutually exclusive, so I’d need to OR them but guess that’s not possible from the UI currently. shrug

Anding them together is fine but if you really want to or them then you can do that by manually writing the script if necessary. i do it all the time and it works fine. They aren’t mutually exclusive either. see my explanation above.

Thank you very much :slight_smile:
I made a variation on your post.

#automation
    - alias: 'random color on lights'
      trigger:
      - seconds: /600
        platform: time
      condition:
      - after: sunset
        condition: sun
      - condition: state
        entity_id: switch.party
        state: 'on'
      action:
        - service: script.toggle
          entity_id: 
            - script.party_random
#script
        party_random:
            sequence:
              - service: light.turn_on
                data_template:
                  entity_id: light.living_room_window_light, light.hue_color_candle_1_3
                  brightness: 180
                  color_name: >
                    {% set colors = ['white', 'yellow', 'blue', 'green', 'purple'] %}
                    {{ colors|random }}
                  transition: 10
1 Like

I made a variation of this to “simplify” it and try out coding a script.

It is my first HA script, and I must say I have been struggling with syntax, and even what language we are really using here…

But here goes:

christmas_random_pot_lights:
  alias: Christmas_Random_Pot_lights
  variables:
    t_entities: ['light.backyard_inside_potlight_2', 'light.backyard_inside_potlight','light.underdeck',
    'light.outside_pot_by_shed','light.outside_pot_over_deck',
    'light.01401742cc50e3e1b6fd','light.01401742cc50e3e1b954','light.014017422cf43280c9ac','light.driveway_3',
    'light.driveway_2','light.driveway_front_walk']
    t_colours: ['red','green','blue']
  sequence:
  
  - service: script.set_hue_christmas_color
    data_template:
      light_id: >-
        {% set i = (range(1,(t_entities|length)+1)|random) %}
        {% set chosen_light = (t_entities[i-1]|replace("'", "")) %}
        {{ chosen_light }}        
      color_name: >-  
        {% set i = (range(1,(t_colours|length)+1)|random) %}
        {% set colour = (t_colours[i-1]|replace("'", "")) %}
        {{ colour }}
  icon: mdi:pine-tree


set_hue_christmas_color:
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: '{{ light_id }}'
        color_name: '{{ color_name }}'
        transition: 1

This is a great share! Same setup as me where I have about 5-6 bulbs in the front of my house too.

I know the post is quite old but I am a new HA user (HA Green on the way) and was wondering about this type of automation. I have a LOT of Hue bulbs that I will be pairing with the hub. Was hoping to remove the Phillips Hue hub after but the wife likes to do halloween and Christmas light themes.

Is this still valid and functional? Is there more of these automations for HA out there?