Random rgb_color in scene

Hi,

Just to say this was exactly what I needed and has worked for me using my aqara switches, changing colours, using the aqara both clicks, on both kids milight gateways and aqara gateways. If it helps anyone else here are my yaml files.

  • alias: Eva_light_change_colour
    initial_state: True
    hide_entity: True
    trigger:
    - platform: event
    event_type: click
    event_data:
    entity_id: binary_sensor.wall_switch_both_158d0001639dcb
    click_type: both
    action:
    - service: light.turn_on
    entity_id: light.eva_lamp
    data_template:
    rgb_color: [‘{{ (range(0, 255)|random) }}’,‘{{ (range(0, 255)|random) }}’,‘{{ (range(0, 255)|random) }}’]
    brightness: 159
  • alias: Eliz_light_change_colour
    initial_state: True
    hide_entity: True
    trigger:
    • platform: event
      event_type: click
      event_data:
      entity_id: binary_sensor.wall_switch_both_158d000128cd07
      click_type: both
      action:
    • service: light.turn_on
      entity_id: light.gateway_light_286c0785f460
      data_template:
      rgb_color: [‘{{ (range(0, 255)|random) }}’,‘{{ (range(0, 255)|random) }}’,‘{{ (range(0, 255)|random) }}’]
      brightness: 255

Thanks

Trux

1 Like

Trying my best with the “blockquote”!

Hi!

I use this automation right now and it’s works nicely BUT, almost never coming “clear” colours , can I change this automation somehow or I must maid brand new automation for that ? something like that – every some minute coming specific colour ( green, red, blue, yellow, purple - etc.)

  • alias: I korrus esik Disco
    trigger:
    • platform: time
      seconds: ‘/30’
      action:
      service: light.turn_on
      entity_id:
      • light.i_korrus_esikkook
      • light.led_test
        data_template:
        rgb_color: [‘{{ (range(0, 255)|random) }}’,‘{{ (range(0, 255)|random) }}’,‘{{ (range(0, 255)|random) }}’]

I try’d the color: [‘{{ (range(0, 12)|random) }}’], this one not work ;( - from commandline can in tasmotta send color 1 = red, color 3 Blue etc.

effect: random if supported by your platform is the easiest way.

@anon43302295 my device is tasmotad Magic Home LED controller in ARilux LC01 Mode and seems that not supported “effect:” command - when I wrote to automatsion

action:
service: light.turn_on
entity_id:
- light.i_korrus_esikkook
- light.led_test
data_template:
effect: random

in web console shows only that the power on command is send

Shame :slight_smile:

The only thing I can think of is to come up with a list of predefined colours, and then randomly select from that list, in a similar fashion to this…

Thanx @anon43302295 -from this post I get some help
I made my automation little easy way, but for me it’s ok and working.

  • alias: I korrus esik Disco
    trigger:
    • platform: time
      minutes: ‘/2’
      seconds: 00
      action:
    • service: light.turn_on
      entity_id:
      • light.i_korrus_esikkook
      • light.led_test
        data_template:
        brightness: 250
        color_name: >
        {% set colors = [‘red’, ‘yellow’, ‘blue’, ‘green’, ‘purple’] %}
        {{ colors|random }}
1 Like

Could you format it differently i get an error:

  - alias: Police
    trigger:
      - platform: state
        entity_id: alarm_control_panel.house
        to: 'triggered'
    action:
      service: light.turn_on
      entity_id: group.all_lights
      data_template:
        brightness: 250
        color_name: >
        {% set colors = [‘red’, ‘yellow’, ‘blue’, ‘green’, ‘purple’] %}
        {{ colors|random }}



Error loading /config/configuration.yaml: while scanning for the next token found character '%' that cannot start any token in "/config/automations/test.yaml", line 12, column 10

The last two lines of yours need indenting 2 spaces more.

  - alias: Police
    trigger:
      - platform: state
        entity_id: alarm_control_panel.house
        to: 'triggered'
    action:
      service: light.turn_on
      entity_id: group.all_lights
      data_template:
        brightness: 250
        color_name: >
          {% set colors = [‘red’, ‘yellow’, ‘blue’, ‘green’, ‘purple’] %}
          {{ colors|random }}

He mf, thanks for the reply, now it says there’s a coloring issue?
I am using a magichome RGB LED strips flashed with tasmota.
Best thing is to just set an effect on a light in an automation, but i cant figure out how to.

Invalid config for [automation]: invalid template (TemplateSyntaxError: unexpected char '‘' at 17) for dictionary value @ data['action'][0]['data_template']['color_name']. Got None. (See /config/configuration.yaml, line 169). Please check the docs at https://home-assistant.io/components/automation/

1 Like

You’re using the wrong type of quotes around the colour names you need ' but you have ’ (they’re angled).

You’re such a bad ass! Thank you so much, couldn’t have spotted that in a hundred years!

Edit: This is a workaround for setting an effect on Tasmota Magic Home LED. By change you know how to let this loop for about a minute, it only changes once now… Cant seem to find any guides, saw another post of yours but not really apliccable in my case.

You want it to change colour every few seconds for a minute? If so, how random do you want it to be? Like does it always go red, yellow, blue, green, purple but just the start color is different each time? Or is the first colour based on something? Or do you just want a completely random colour every time?

Well it really doesnt matter, id like to use it in my alarm setup to draw attention so it just has to switch fast until the state of the alarm is disarmed, or as lomg as its triggered.

I tried the snippit from above followed by turn off an then copied that like 100 times, didnt work and ofcourse a nasty workaround.

Thanks for your help!
Ezra

maybe my party lights script helps:

  lights_partying_on:
    alias: Lights partying on
    sequence:
      service: light.turn_on
      data_template:
        entity_id: light.{{['dining_corner','cabinet','kist','home_theater']|random}}
        rgb_color: ['{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}',
                    '{{ (range(0, 255)|random) }}']
        brightness: '{{ (range(50, 250)|random) }}'
        transition: '{{ (range(1, 3)|random) }}'

call this with the following automation each 4 seconds:

automation:
  - alias: 'Lights partying'
    id: 'Lights partying'
    initial_state: 'off'
    trigger:
      platform: time
      seconds: '/4'   # {{ "/" ~ ((range(2, 5) | random) | int) }}
    condition:
      condition: template
      value_template: >
        {{is_state('automation.sense_lights_change','off') }}
    action: 
      service: script.lights_partying_on

and this input_boolean for setting the mode:

  - alias: 'Lights Party Mode'
    id: 'Lights Party Mode'
    initial_state: 'on'
    trigger:
      platform: state
      entity_id: input_boolean.home_mode_party
    action:
      service_template: >
        script.lights_mode_party_{{ trigger.to_state.state }}
4 Likes

OK, off the top of my head I would probably do it like this:

automation:
  - alias: Police
    trigger:
      - platform: state
        entity_id: alarm_control_panel.house
        to: 'triggered'
    action:
      - service: script.light_cycle
      - delay: '00:01:00'
      - service: homeassistant.turn_off 
        entity_id:
          - script.light_cycle
          - script.loop_lights

script:
  light_cycle:
    sequence:
      - service: homeassistant.turn_on 
        entity_id: group.all_lights
        data_template:
          brightness: 250
          color_name: >
            {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
            {{ colors|random }}
      - service: script.loop_lights

  loop_lights:
    sequence:
      - delay: '00:00:02'
      - service: script.light_cycle

Fairly simple, but does the business.

3 Likes

cool this loop. you’re a magician!
—> Cookbook this goes.

1 Like

Super!! thanks mate!

1 Like

is there a way to use mili seconds instead of seconds? To have it change the color faster perhaphs?
Thanks again!

Edit: got the miliseconds now, but it still doesnt loop, it turns on and does not change just once.

- alias: "[Alarm] Police on"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'triggered'
  action:
    - service: script.light_cycle
    
- alias: "[Alarm] Police off"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'disarmed'
  action:
    - service: homeassistant.turn_off 
      entity_id:
        - script.light_cycle
        - script.loop_lights

  light_cycle:
    sequence:
      - service: homeassistant.turn_on
        entity_id: group.all_lights
        data_template:
          brightness: 100
          color_name: >
            {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
            {{ colors|random }}
      - service: script.loop_lights

  loop_lights:
    sequence:
      - delay:
          milliseconds: 10
      - service: script.light_cycle

Ok i got it down to about 3 changes right now with this config:

scripts.yaml:

  light_cycle:
    sequence:
      - service: light.turn_on
        entity_id: group.all_lights
        data_template:
          brightness: 250
          color_name: >
            {% set colors = ['red', 'yellow' , 'blue', 'green' , 'purple'] %}
            {{ colors|random }}
      - service: script.loop_lights

  loop_lights:
    sequence:
      - delay:
          milliseconds: 5
      - service: script.light_cycle

automations.yaml:

- alias: "[Alarm] Police on"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'triggered'
  action:
    - service: light.turn_off
      entity_id: group.all_lights
    - service: script.light_cycle

- alias: "[Alarm] Police off"
  trigger:
    - platform: state
      entity_id: alarm_control_panel.house
      to: 'disarmed'
  action:
    - service: homeassistant.turn_off
      entity_id:
        - script.light_cycle
        - script.loop_lights

Any advice?