Random rgb_color in scene

Hey,
I am trying to get a random color when activating the scene “Disco”, I tried those solutions:

  • name: “Disco”
    entities:
    light.flur:
    state: on
    brightness: 255
    rgb_color: [ ‘{{ (range(0, 255)|random) }}’, ‘255’, ‘0’]

  • name: “Disco”
    entities:
    light.flur:
    state: on
    brightness: 255
    rgb_color: [ ‘{{ (range(0, 255)|random)|int }}’, ‘255’, ‘0’]

  • name: “Disco”
    entities:
    light.flur:
    state: on
    brightness: 255
    rgb_color: [ states.sensor.random_sensor, ‘255’, ‘0’]

and various combinations like ‘{{ states.sensor.random_sensor | int }}’ (and about 10 more) but nothing is working. Always the same error:

Invalid service data for light.turn_on: expected int for dictionary value @ data[‘rgb_color’]. Got [’{{ (range(0, 255)|random)|int }}’, ‘255’, ‘0’]

or the variants

Can someone pleas help me?

Your MA5K

Use something like rgb_color: ['{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}']. You can see a few examples here.

Like this:

  - name: example
    entities:
      light.example:
        state: on
        effect: random 
        brightness: 200
1 Like

EDIT: Look at @anon43302295’s post above. It’s much better. :slight_smile:

Because you’re asking home assistant to evaluate a template, you need to use data_template when calling the light.turn_on service.

Take a look at this example.

I’m not sure how all of this works with scenes as opposed to scripts, but I’m quite confident it would work with a script!

I would use your second example, where you format it as an int. Not sure if it’s necessary, but it just makes sense to me.

You can evaluate if the template itself is rending how you expect by testing it out under “Templates” under Developer Tools on the bottom left of the front end. This really opened up templating for me!

You don’t need to template this, HA has random colours built in to the light component, see my example above.

I am using limitlessLED bulbs and they won´t work with the random effect

tryed this befor same Error:

2017-11-21 14:40:23 ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: expected int for dictionary value @ data[‘rgb_color’]. Got [’{{ (range(0, 255)|random) }}’, ‘{{ (range(0, 255)|random) }}’, ‘{{ (range(0, 255)|random) }}’]

Per @anon43302295’s example, it should be effect: random .

I’m guessing that you entered rgb_color: random .

Could you post your full snippet again? As I’m only guessing.

nope effect: random is not working with limitlessLED bulbs. color_loop is also buggy. But i got it working.
I made an automation, with scenes it seems not to work at all:

- id: disco
  alias: 'Disco'
  initial_state: 'on'
  condition:
    - condition: state
      entity_id: switch.disco
      state: 'on'
  trigger:
   - platform: time
     seconds: '/1'
  action:
     service: light.turn_on
     entity_id: light.flur
     data_template:
       rgb_color: ['{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}','{{ (range(0, 255)|random) }}']
2 Likes

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).