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

Hello.

Firstly I would like to say this is my first post to these forums, g’day from down under :wink:

I have been quite happily using HASS for a few months now & would like to say thank you so very much to both the devs & community for such a great project even at the 0.23 stage it is at now. I have high hopes for the future of HASS & hope I will in some way be able to help it & fellow users along the way.

Along with a heap of DIY ESP8266 & Arduino devices, milights, chromecasts, 433MHz alarm sensors & PIRs, etc & plenty of time I have been able to start down the path of automating my home.

For instance it is fantastic that as I move throughout my home the lights turn on & off as I go. They dim after 9PM on weekdays or turn off when I turn on my home theatre & watch something.
I can schedule my washing machine to start an hour before I wake up on the weekend (when it is loaded the night before) & have it notify me when it is finished, making sure the clothes get the maximum sun exposure to dry & saving me time in the morning.
When I leave home the WiFi MQTT alarm is automatically armed if no-one is home. When I return home the alarm can be disarmed & the garage door can be opened for me so I can just drive in.
Occupancy sensors under the bed stop the bedroom light coming on due to PIR sensors detecting movement in the night.
My solar/battery powered letterbox notifies me when I have letters & how many so I remember to check it.
HASS audio alerts & text to speech from automations or music I’m listening to is sent to the bluetooth speaker system in the room I am currently located in.
The list goes on & on, but I digress. Thank you again.

I am currently attempting to control the brightness & colour of my lights when used by automations, with values derived from input sliders. This is to avoid hard coding values in to the automations.yaml I have.

This snippet currently works & allows me to turn on the light at the brightness I have set with an input slider:

service: light.turn_on
entity_id: light.bedroom
data:
  rgb_color: [255, 255, 255]
data_template:
  brightness: '{{ states.input_slider.light_brightness.state }}'

When I attempt to template the rgb_color as follows (which looks ok in the template editor):

service: light.turn_on
entity_id: light.bedroom
data_template:
  brightness: '{{ states.input_slider.light_brightness.state }}'
  rgb_color: '[{{ states.input_slider.light_red.state }}, {{states.input_slider.light_green.state }}, {{states.input_slider.light_blue.state }}]'

I get the following error message:

  homeassistant.core: Invalid service data for light.turn_on: None for dictionary value @ data['rgb_color']

If I remove the single quotes at the beginning & end of the rgb_colour line, HASS fails to load. I have tried:

  data:
    rgb_color: '[255, 255, 255]'

To test if the quotes are able to be used, but this also causes HASS to fail to load.

So it seems with or without those quotes, neither will allow HASS to load. Does anyone have any insight in to what might be a work around or fix for this? I have done a lot of research of these forums & Google. https://community.home-assistant.io/t/issues-with-input-slider-together-with-in-automation/1152/3 is how I got the brightness working.

Any help would be appreciated, thanks for your time.

1 Like

For your information, I submitted an issue about a week ago after noticing similar behaviour. Check it out here.

In my example, I’m only trying to make the brightness variable, so I’ll use your example (using both data and data_template) as a workaround and see if that works for me. Thanks! :slight_smile:

EDIT: seems to work for me! But obviously, the issue still stands for people (like you) wanting to make the rgb_color value variable.

Hi David, I am so glad that my post was able to help you. Templating is really powerful, I cannot wait until its functionality is extended further.

1 Like

I ran into this same issue this evening. I have a lamp that I’d like to change color based on the weather forecast. Tried:

 scene:
   - name: Weather Lamp
     entities:
       light.office_rgbw:
         data:
           state: on
           transition: 2
           brightness: 153
         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 %}

But I get an error in the logs:

homeassistant.helpers.state: reproduce_state: Unable to reproduce state

I don’t think you can use templates in scenes. The scene documentation doesn’t mention data or data_template at all. Google can’t find me any examples either.

You’ll have to use a script instead of a scene. I’ve actually converted all my scenes into scripts, as scripts are alot more flexible.

@Rodney and also @ih8gates : according to @balloob in issue #2985 opened by me, data_template should accept lists since version 0.28.

Thanks for the update. I’m not sure that this helps in my situation above. When I try to do this with in a script:

weatherlighttest:
    alias: Weather Light Test
    sequence:
      - service: light.turn_on
        entity_id: light.office_rgbw
        data:
          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 %}

My error is:

homeassistant.core: Invalid service data for light.turn_on: None for dictionary value @ data[‘rgb_color’]. Got ‘[229,255,193]’

I think that templates (and when I can use them) is one of the things that I struggle with the most. Can I not use a template for rgb_color? Is that the prob with my example?

(updated to include full script)

Is my problem there that I’m returning a string instead of a list?

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 -%}']