Hue Random Color autiomation help

I want an automation to change my hue lamp color to a random color every 1 minute. But I only want to turn on after sunset. Can someone help me with this automation. I am new and just getting into automations.

Use something like:

- alias: Lights - Random Color Every Minute
  trigger:
    - platform: time
      minutes: '/1'
      seconds: 00
  condition:
    - condition: state
      entity_id: sun.sun
      state: 'below_horizon'
  action:
    - service: light.turn_on
      entity_id:
        - light.media_center_lighting
      data:
        effect: random

Thanks for the response.

Is there a way to make it so the light is only on for 3 hours after sunset?

Add another automation to turn_off lights after 3 hours.

That code turns on the automation but when you turn the light off it turns back on.

Yeah…that automation was a very simple example of how to go about it. It does what you mentioned in the original post. You should use that as a base to handle other conditions like the one you added.

I wrote this a while back.

Trigger the script execution using a time trigger and a condition to only fire after a certain time.

1 Like

Thanks for the information. While I got these items to show on the front end. I am unable to get it to work. I can’t get the script to trigger my light.sink_lamp

I got the light to turn on but I get this in my error log.

ERROR (MainThread) [homeassistant.helpers.service] Error rendering data template: UndefinedError: ‘colors’ is undefined

My code has evolved a bit. This is what I am currently running for 3 different lights (only one in code below = kitchen_high, but you can easily copy that script, rename it and call the script.preprocessor with another id). An important thing to keep in mind is that you should not call the script multiple times at the same time, as it will produce an error that the script was still running.
To configure the brightness and saturation (these are not randomly selected, only the hue is), I am using two sliders in my UI. They are called input_number.movie_night_saturation and input_number.movie_night_brightness.

SCRIPTS

change_kitchen_high:
  alias: Change Kitchen Top Light
  sequence:
    - service: script.preprocess_color
      data:
        id: light.kitchen_high

# this script choses a random color for the lights
preprocess_color:
  sequence:
    - service: script.set_color
      data_template:
        id: '{{ id }}'
        colors: >-
          {%- set h = (range(0, 360)|random)/360 %}
          {%- set s = states.input_number.movie_night_saturation.state|float %}
          {%- set v = states.input_number.movie_night_brightness.state|float %}

          {%- 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: 10

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 }}"

AUTOMATION
The following automation changes the kitchen colored light after 8pm, every 20 minutes, but only if they are on

- alias: Kitchen colored lights change after 20:00
  trigger:
    platform: time
    minutes: /20
    seconds: 30
  condition:
    condition: and
    conditions:
    - condition: state
      entity_id: light.kitchen_high
      state: 'on'
    - condition: time
      after: '20:00:00'
  action:
    - service: script.change_kitchen_high

INPUT NUMBERS

movie_night_saturation:
  name: Color Saturation
  initial: 0.4
  min: 0
  max: 1
  step: 0.05
  mode: slider

movie_night_brightness:
  name: Color Brightness
  initial: 0.8
  min: 0
  max: 1
  step: 0.05
  mode: slider

I hope that helps.

2 Likes

Thank you. I got it working.

Any upddates on this, how to make it run on 0.118.3? The simple part, the automation, works fine. The script does not seem to change the colour of my Trådfri bulb. Note, I hade to change the platform to time_pattern and not use “/x” for the “every”-part of the story.

Ditto what @Marcus_Jansson said - my code like this has recently stopped working, but I’m not sure why. I can see the automation and all scripts firing, so I’m not sure what the problem is. Anyone have any thoughts or advice? Not sure where to look next…

EDIT: Found another example online that fixed things, and condensed it all down to one automation without any extra scripts:

- alias: LightsHue - Rotating Hue Colors Every Two Seconds
  initial_state: True
  trigger:
    - platform: time_pattern
      seconds: '/2'
  condition:
    - condition: state
      entity_id: light.office
      state: 'on'
  action:
    - service: light.turn_on
      entity_id: light.office
      data_template:
        hs_color:
          - "{{ (30 + (state_attr('light.office', 'hs_color')[0] or 0)) % 360 }}"
          - 100
        brightness_pct: 100
        transition: 2

Obviously update your light names as needed. Hope this helps someone else!