jprates
(JoĂŁo Prates)
June 23, 2021, 10:39am
1
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
jprates
(JoĂŁo Prates)
June 23, 2021, 11:04am
4
@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?
jprates
(JoĂŁo Prates)
June 23, 2021, 11:27am
5
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.
jprates
(JoĂŁo Prates)
June 23, 2021, 11:30am
6
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
1 Like