Automation & templating regarding light brightness & colour derived from input sliders

I’ve been fighting with this for the last hour or so. I really couldn’t figure it out, something is apparently not passing it properly as a list but I just don’t understand how or why. For those who might have given up (like me), you can always pass color_name, as it accepts that value properly.

Hi, i struggled with this with xy_color instead of rgb color Using template for xy_color

the problem was that the values in the dictionary should be floats, not strings.
rgb_color accept integers, so try this as your template

        {% if now().hour | int > 8 and now().hour | int < 20 %}
          ['{{180|int}}', '{{180|int}}', '{{255|int}}']
        {% else %}
          ['{{255|int}}', '{{180|int}}', '{{180|int}}']
        {% endif %}

I am running into the same problem. As soon as there is something in front of the list in the template, e.g. as the value of rgb_color,

rgb_color: "{% set choice = range(0, 6) | random | int %} [ '{{200|int}}' , '{{100|int}}', '{{100|int}}' ]"
```
the parsed template is a string and you get something like this in the log:

```yaml
None for dictionary value @ data['rgb_color']. Got "[ '200' , '100', '100' ]"
```
(expecting a dict but got this string). To be complete, the following works fine, because nothing comes before the [ character:
````yaml
rgb_color: "[ '{{200|int}}' , '{{100|int}}', '{{100|int}}' ]"
```


My templates work fine in the dev tools, like others who posted here.

Is there a FR for this? If not, how/where can I create one?

Thank you.

I created the following workaround, maybe it helps someone else:
Instead of setting the light color, do all calculations and call a script with a variable (colors) in my example. Than use that variable in the script and set the color.

    - service: script.set_color
      data_template:
        id: group.living_room_colored_lights
        colors: >-
          {%- set delta = 80 %}
          {%- set low = 80 %}
          {%- set high = 256 - delta %}
          {%- set choice = range(0, 6) | random | int %}
          {% set red = range(low, high) | random | int %}
          {% if choice == 0 or choice == 3 or choice == 4 %}
            {% set red = delta + red %}
          {% endif %}
          {% set green = range(low, high) | random | int %}
          {% if choice == 1 or choice == 3 or choice == 5 %}
            {% set green = delta + green %}
          {% endif %}
          {% set blue = range(low, high) | random | int %}
          {% if choice == 2 or choice == 4 or choice == 5 %}
            {% set blue = delta + blue %}
          {%- endif %}
          {{ red|int }},{{ green|int }},{{ blue|int }},{{ choice }}
        brightness: 150
        transition: 5

and here is the script that actually sets the colors:

set_color:
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: '{{ id }}'
        rgb_color: [ "{{ colors.split(',')[0]|int }}", "{{ colors.split(',')[1]|int }}", "{{ colors.split(',')[2]|int }}" ]
        brightness: "{{ brightness }}"
        transition: "{{ transition }}"
4 Likes

@pbavanik. I think this is pretty close to what I’m looking for. I need to parse values from an MQTT topic. what I’m doing so far is sending the 3 values on different topics and using them to compose the rgb_value like this:
rgb_color: ['{{ states.sensor.red_channel.state | int }}','{{ states.green_channel.state | int }}','{{ states.blue_channel.state | int }}']

I think this is a pretty stupid way to do that but I don’t get how to transform R,G,B which is actually a string in an integer values that can go along with “rgb_color” format.
I think the solution is there in your code but I don’t get exactly what “colors” and “split” are in your “colors.split” templating.

thanks

I noticed you use single quotes as opposed to double quotes. Does that matter?
The .split function is used to split a string like “50,100,150” into [50,100,150], so string to array. I don’t think you need that in your case.

Thanks for posting this solution. Worked for me when parsing trigger.payload data coming from MQTT. I just format it properly before I publish the message to the broker.

The only way I could make this working was by using a single line command like this:

data_template:
  address: 2/6/13
  payload: ['{% if is_state("input_select.parent_heating_mode", "Komfort") %} 1 {% elif is_state("input_select.parent_heating_mode", "Nacht") %} 3 {% elif is_state("input_select.parent_heating_mode", "Standby") %} 2 {% endif %}']

I was not able to split this into multiple lines.

Hey all, I think this is still a problem?
I am trying to write a script that allows for RGB control of the bulb. The script publishes to a HASS HTTP sensor, where all the info I need is stored.

The automation gets the info like this:

- alias: Yeelight Control - Bedroom
  id: yeelightbedctrl
  trigger:
  - entity_id: sensor.yeelightcontrol
    platform: state
    to: 'on'
  action:
  - service: light.turn_on
    data_template:
      entity_id: light.bedroom_yeelight
      transition: '{{ states.sensor.yeelightcontrol.attributes.transition }}'
      brightness: '{{ states.sensor.yeelightcontrol.attributes.bright }}'
      rgb_color: >-
        {{ states.sensor.yeelightcontrol.attributes.colour }}

…but no matter how I present the RGB variable within the script/HTTP sensor, or the automation (for exampe moving the square brackets into the template), I get the same useless response from the Home Assistant log:

Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']. Got '[255,10,100]'

Does anyone know what I need to do? None of the suggestions I see work, and those that suggest ommitting the ’ marks, result in an error like:

Error when calling <function YeelightLight.set_default at 0x7fe0b96187b8>: {'code': -5000, 'message': 'general error'}

EDIT: I just figured out that the {‘code’: -5000, ‘message’: ‘general error’}` response is what I get when things are working. So things are actually working fine… but this error lead me to think otherwise.

Maybe I am leaving out a required parameter?

Thank you so much, your “setting RGB value based on sliders” workes perfect for me.

- service: light.turn_on
  data_template:
    entity_id: light.ledzaun
    rgb_color: ['{{ states.input_number.led_color_r.state | int }}','{{ states.input_number.led_color_g.state | int }}','{{ states.input_number.led_color_b.state | int }}']
    brightness: '{{ states.input_number.led_color_bright.state | int }}'

Yeah, so pretty much non of these solutions worked for me. Is this still a bug? Is there a better way to use sliders to control RGB colours?