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

BTW, @fanaticDavid - the example you describe in the issue on GitHub kinda blew my mind. I didn’t realize that you could call script.turn_on and pass in variables! The documentation for Script Syntax doesn’t mention this. Thanks for that.

1 Like

First of all, your entity_id: needs to be under data:
Also, change rgb_color: > to rgb_color: >- to strip any unwanted spaces and newlines.
And finally, does your template work when you test it in the UI (Developer Tools)?

Calling scripts with parameters is briefly explained here. I’ll consider editing that page to provide a little more information.

No. I get the same error even stripping whitespace. And it does work fine in the template dev (I always try that first).

That error reads like light.turn_on isn’t expecting data > rgb_color. rgb_color would typically be at the same level of indentation as entity_id, etc. I think that it’s just not expecting rgb_color to come from data/data_template.

I’m not sure if you moved your entity_id: as well, but your final script should look like this:

weatherlighttest:
  alias: Weather Light Test
  sequence:
    - service: light.turn_on
      data:
        entity_id: light.office_rgbw
        brightness: 255
      data_template:
        rgb_color: >-
          {% if states.sensor.forecastio_daily_high_temperature.state | float < 32 %}[168,108,255]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 55 %}[192,255,239]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 80 %}[229,255,193]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 95 %}[255,231,205]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 105 %}[185,13,0]{% else %}[255,255,255]{% endif %}

If that doesn’t work, then try this:

weatherlighttest:
  alias: Weather Light Test
  sequence:
    - service: light.turn_on
      data_template:
        entity_id: light.office_rgbw
        brightness: 255
        rgb_color: >-
          {% if states.sensor.forecastio_daily_high_temperature.state | float < 32 %}[168,108,255]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 55 %}[192,255,239]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 80 %}[229,255,193]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 95 %}[255,231,205]{% elif states.sensor.forecastio_daily_high_temperature.state | float < 105 %}[185,13,0]{% else %}[255,255,255]{% endif %}

If that doesn’t work either, then you may have stumbled upon another bug…

Same error for both those variations. I’ll update to 0.29.7 tomorrow and confirm that it wasn’t something they fixed this last go-round. (I’m on 0.29.6)

I came across this thread looking for some info on setting the RGB value based on sliders after not finding anything else and a whole lot of errors i have some code that works for anyone else who stumbles on this thread:

  - alias: CrownMouldColorSet
    trigger:
      - platform: state
        entity_id: input_slider.crowncolor_r
      - platform: state
        entity_id: input_slider.crowncolor_g
      - platform: state
        entity_id: input_slider.crowncolor_b
      - platform: state
        entity_id: input_slider.crowncolor_level
    action:
      - service: light.turn_on
        data_template:
          entity_id: light.crown_moulding
          rgb_color: ['{{ states.input_slider.crowncolor_r.state | int }}','{{ states.input_slider.crowncolor_g.state | int }}','{{ states.input_slider.crowncolor_b.state | int }}']
          brightness: '{{ states.input_slider.crowncolor_level.state | int }}'
3 Likes

Hi all,

I’d like to ask @ih8gates if you managed to succeed with templating rgb_color?
My HASS is 0.31.0 and none of the suggested solutions work.
Maybe there are some other ideas or should it be a ticket or future request?

I never got that to work. I don’t believe that the rgb_color can be templated.

Hi,

I managed to make this working (not exactly what I want but better than nothing):

action:
  service: light.turn_on
  data_template:
    entity_id: light.kitchen
    brightness: 60
    rgb_color: ['70', '70', '{% if states.sensor.dark_sky_precip_probability.state | float < 49 %}0{% else %}255{% endif %}']

However, I still cannot understand why the old code below doesn’t work:

    rgb_color: '{% if states.sensor.dark_sky_precip_probability.state | float < 40 %}[254, 174, 0]{% elif states.sensor.dark_sky_precip_probability.state | float < 80 %}[129, 120, 193]{% else %}[70, 108, 255]{% endif %}'

Maybe it is expecting it to be an integer when it is actually a string or vice versa?
Can someone help?

Did anyone get this figured out?

I think this is the most similar thread to my issue, and it helped a bit.
If I do a normal light change automation with fixed color:

  action:
  - service: light.turn_on
    data_template:
      entity_id: >-
        light.sunflower_{% if trigger.entity_id == 'binary_sensor.motion_living' %}50f6{% else %}6532{% endif %}
      brightness: 100
      rgb_color: [20, 120, 255]

I see this in the logs:

<Event call_service[L] service_data=entity_id=light.sunflower_50f6, brightness=151, rgb_color=['182', '183', '251'], domain=light, service_call_id=3054315984-4, service=turn_on> 

When I replace rgb_color with my template:

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

It looks like it works, and I get the values I want (first line), but error (second line) and it doesn’t run:

<Event call_service[L] service_call_id=3054690704-2, domain=light, service=turn_on, service_data=entity_id=light.sunflower_50f6, brightness=151, rgb_color=['255', '180', '180']> 
ERROR (MainThread) [homeassistant.core] Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color] Got "['255', '180', '180']" 

(I tried it with and without quotes around the R, G, B values. Similar results.)
Should this be able to work somehow?
Thanks!

You can do it like this, even though its ugly and not really looking nice.

rgb_color: >- ['{%- if Farbe == "rot" -%} 255 {%- else -%} 0 {%- endif -%}', '{%- if Farbe == "gruen" -%} 255 {%- else -%} 0 {%- endif -%}', '{%- if Farbe == "blau" -%} 255 {%- else -%} 0 {%- endif -%}']

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.