Random Color doesn't work

“Random” has been broken in template rendering for a while now. The first time your automation runs, a random color will be chosen, but then it will never update again. There’s an issue on GitHub discussing. Add your notes to the discussion:

This got me thinking … would it be possible to randomly pick a scene from a predetermined list of scenes?
I currently have my hue lights turn on at night for TV Viewing … I created 7 entries and trigger one every day of the week. But it’s predictable of which color on which day and I am limited to 7 different scenes.

Would it be possible to use random and pick any scene from a list?
What would be needed for this? Creating a group and picking a scene from a group? I want to keep it for TV Viewing only and not interfere with my Kitchen Lights. A code example would be of great assistance to this newbie.

Can you templatize color values at all? I’m still not sure this is doable.

Honestly it’s likely going to be far easier to just add the random effect to the hyperion component. It’s fairly easy, I just did it, but I need someone who is willing/able to test it.

I am willing to test it, just tell me what to do.

I just saw it here: Change color of Hue lamp every hour

Did you read the whole thread, including the other thread that was linked in? :wink:

I don’t believe this can be done within the constraints of automation in HA. I don’t think you can use templates to produce color values in lights; rgb, color names, etc. I think that to do this, you’d have to look at AppDaemon and have an app that constructs the whole JSON string for the light (with the color values rendered and included) and send that to HA.

I think if you follow the thread to the end and also look at the connected thread, you’ll see that no one seems to have got it to work. But if this CAN be done solely in HA via templating, I would totally love to be proven wrong as I have some use cases for it. :smiley:

I couldn’t test this yet, but at github someone seems to have a solution: https://github.com/home-assistant/home-assistant/issues/5678

I’m pretty sure you can use a template to specify the entity_id for scene.turn_on. If not, you can definitely use service_template to call a script.

I solved it :slight_smile:

1 Like

I saw your report on GitHub. Curious - was it the update to .39 (with an updated version of Jinja) that did the trick?

Even with your simplified code for the rgb_color, it’s still using the random filter in Jinja (which was not working for me/others).

I’m pretty sure it did this with 0.38.3 as I skipped 0.38.4.

I’m still on 0.38.3 and I tried using that code in my automation and it did seem to work for me. I just tried triggering it a couple of times just now and each time I got a different color just to make sure it wasn’t a fluke.

That’s interesting. How about your other randoms? Like your Alexa replies? I’ve got randoms that fail for my Alexa skills and also for my scripts (so it’s not just an Alexa configuration thing).

Alexa is still giving me the same reply over and over again; no change there… which is curious.

My landscape lights automation just ran as scheduled and the color is indeed random! So that part is solved; why it works there and not in my Alexa scripts is beyond me.

I made some tests and got the same result.

In my script.yaml I wrote the following lines:

 alias: Jarvis Test
  - service: tts.google_say
      entity_id: media_player.speaker_hall
      message: "{{ (['horse','dog','cat','mouse','tiger','rabbit','snail']|random) }}"

I’m getting the same reply, too, until I restart. Seems to me, this random filter is just executed once.

Another test with this lines:

 alias: Jarvis Test Number
  - service: tts.google_say
      entity_id: media_player.speaker_hall
      message: '{{ (range(0, 255)|random) }}'

gave me random numbers everytime I triggered the script, without restarting HAss.

Maybe there is something wrong with the implementation of “random for lists”.

With the assumption that range(1,8) delivers the following list [1,2,3,4,5,6,7] I also tested:

 alias: Jarvis Test Number 2
  - service: tts.google_say
      entity_id: media_player.speaker_hall
      message: '{{ [1,2,3,4,5,6,7]|random) }}'

and got the same result as in the last test, always the same number, until restart.
So even range(1,8) and [1,2,3,4,5,6,7] are handled differently.

1 Like

Thanks. This is a good point of data in figuring out the underlying issue. Please post this to the issue.

1 Like

BTW, a PR fixing this issue has been submitted and should show up in the next release.


1 Like

Yeah, I noticed that too when checking the PRs. Should be in 45.1.