Newbie - how to use random values on RGBW color light.turn_on service field

Hi,

This is mostly (I believe) a newbie question concerning yaml language, so apologies if this seems basic and easy for most of you.

I was trying to set the RGBW color of some LED light bulbs on my automations using random values.

After setting the random sensors for red green and blue I tried to use them plain vanilla on the service call like this:

(...)
action:
  - service: light.turn_on
    data:
      brightness_pct: 20
      rgbw_color:
        - random_red
        - random_green
        - random_blue
        - 0
(...)

The result is an error message stating “expected int for dictionary value @ data[‘rgbw_color’]”.
Removing the sensors names to literal integers obviously solve the problem.

Replacing the sensor names on the developer tools sensors works fines, I get the random integer values back.

So simply put: How to use the random values on the service call?

Thanks in advance for your help.

Are you sure you are getting a random integer and not a randon string?

This should do it, if it doesn’t make sure your light allows rgbw. If it doesn’t, remove the last line, the - 0

service: homeassistant.turn_on
data:
  entity_id: light.your_light
  rgb_color:
    - "{{ range(0,255)|random}}"
    - "{{ range(0,255)|random}}"
    - "{{ range(0,255)|random}}"
    - 0

@obaldius how do I check if it’s an integer or a string that gets printed on screen at developer tools?

And most importantly can I change the datatype to INT in case it’s a string? How?

OK, so I tested this fine:

And yet when I do this, it doesn’t work, it still complains it was expecting an INT, what the heck…

service: light.turn_on
data:
  brightness_pct: 20
  rgb_color:
    - {{states.sensor.random_red.state | int}}
    - {{states.sensor.random_green.state | int}}
    - {{states.sensor.random_blue.state | int}}
target:
  entity_id:
    - light.suite_aplique_esquerdo
    - light.suite_aplique_direito
    - light.suite_candeeiro_comoda

Your method of using range hardcoded worked by the way.

argh, missing the quotes… for an int… this is so dumb IMHO…

it works this way:

service: light.turn_on
data:
  brightness_pct: 20
  rgb_color:
    - "{{states.sensor.random_red.state | int}}"
    - "{{states.sensor.random_green.state | int}}"
    - "{{states.sensor.random_blue.state | int}}"
target:
  entity_id:
    - light.suite_aplique_esquerdo
    - light.suite_aplique_direito
    - light.suite_candeeiro_comoda

Thanks for your inputs, I would not have got here so fast without your help.

1 Like

that’s exactly what I was writing ^^, you were missing the quotes.

It’s not recommended tou use the “states.sensor.random_red.state” format when templating. Use “states(‘sensor.random_red’)” instead.

  rgb_color:
    - "{{states('sensor.random_red') | int}}"
    - "{{states('sensor.random_green') | int}}"
    - "{{states('sensor.random_blue') | int}}"

Np, glad to help :wink:

1 Like