Randomize List

Hey guys, I’m trying to figure out why this script that I had working quit working recently. I’m using it to randomize a color for a couple RGB bulbs I have. This was working great up until a week or so ago…It seems to randomize when I restart HASS, but calling the script anytime after that results in the same item being returned every time. Any ideas?

sequence:
- service: light.turn_on
  data_template:
    entity_id: light.xxxxx
    brightness: 255
    color_name: >
      {{['aqua','seagreen','mediumslateblue','teal','blue','darkgreen','deepskyblue','green','indigo','magenta','midnightblue','orangered','hotpink','purple','red','violet','yellow']|random}}

I have a similar script where I randomize media_content_id to load a random playlist to a media player. I’m still seeing randomness. I’m on 0.35.3

Wonder if it’s an issue with how color_name handles templating?

I didn’t think you could set colors programmatically. The template itself is sound; testing it in the editor produced a random value each time I updated it.

I think the main problem is that the value will only get set when the templates are all rendered in HA on initialization. There’s nothing there to reinitialize the template. Something like this would probably work better in AppDaemon.

That’s whats odd, it tests fine in the editor, and it worked fine like this for a couple months. The only change I made was upgrading to 0.36, and when I noticed it wasn’t randomizing anymore I reverted back to 34.5 and that didn’t help.

Maybe because webcolors was removed in June? You’re sure you were only at 0.34.5? Since removing webcolors these are the only supported colors.

Yea in fact that’s what my list is based on. The one color that it picks from the list works fine, just doesn’t randomize anymore.

Anything in your logs when you send the turn_on command?

Nope. Everything appears normal. I guess the next thing I’ll try is using random numbers for RGB values and see if I have the same issue or not.

I’ll try to replicate this issue later tonight and see if I can find any problems. In the mean time, some users were in our Gitter earlier today also trying to generate random RGB colors. Instead of having to fight with Jinja for a while like they did here’s what they came up with. Just use this to generate a tuple of RGB colors.

{{ "{:02x}{:02x}{:02x}".format(range(0, 255) | random, range(0, 255) | random, range(0, 255) | random) }}

rgb_color supports templates now? Yay for that.

Really. How is that not breaking news?

I tried this in my template tester and got a string like this though:

5f55f5

I’m seeing the same thing as @rpitera.

b649b8

Whoops, that generates a hex not a tuple. My b

1 Like