Create a temperature changing light from 2 lights and Shelly RGBW2 [SOLVED]

What do you get when you evaluate

in the template section of devtools?

When off it gives an error “Division by zero” (which is something that should probably be managed later), otherwise it gives the temperature in kelvin. I modified to convert in mireds and now the template light has the color_temp attribute and current state of the light. When changing the temperature on the UI the behavior is still not correct, but I think this can be related to the same issue (kelvin vs. mireds).
Will let you know, thanks for the good hints so far!

I’m happy to report that the template light works as intended now.
The key was to use color_temp everywhere and use mireds in every formula.
Now setting brightness and temperature work, included the fact that due to the nature of how this light is made, the extremely warm or extremely cold can’t be set at more than 50% brightness. Also, since the warm white is at 3000K or 333 mireds, warmer whites cannot be achieved, but the max_mireds attribute is equal to 500. I did not find a way to correct this
Here is the config:

light:
  - platform: template
    lights: 
      corrimano:
        friendly_name: "Corrimano"
        value_template: "{{ (state_attr('light.corrimano_caldo', 'brightness')|int + state_attr('light.corrimano_freddo', 'brightness')|int) > 0 }}"
        level_template: "{{ ((state_attr('light.corrimano_caldo', 'brightness')|float + state_attr('light.corrimano_freddo', 'brightness')|float)/2)|int }}"
        temperature_template: "{{ 1000000/(((state_attr('light.corrimano_caldo', 'brightness')|float*3000 + state_attr('light.corrimano_freddo', 'brightness')|float*6500)/((state_attr('light.corrimano_caldo', 'brightness')|int + state_attr('light.corrimano_freddo', 'brightness')|int)))) }}"
        set_level:
          - service: light.turn_on
            data:
              entity_id: light.corrimano_caldo
              brightness: "{{ ((brightness*2) * (6500-1000000/state_attr('light.corrimano', 'color_temp')|int) / (6500-3000)|int) }}"
          - service: light.turn_on
            data:
              entity_id: light.corrimano_freddo
              brightness: "{{ ((brightness*2) * (1000000/state_attr('light.corrimano', 'color_temp')|int - 3000) / (6500-3000)|int) }}"
        set_temperature:
          - service: light.turn_on
            data:
              entity_id: light.corrimano_caldo
              brightness: "{{ ((state_attr('light.corrimano', 'brightness')|int*2) * (6500 - 1000000/color_temp) / (6500 - 3000)|int) }}"
          - service: light.turn_on
            data:
              entity_id: light.corrimano_freddo
              brightness: "{{ ((state_attr('light.corrimano', 'brightness')|int*2) * (1000000/color_temp - 3000) / (6500 - 3000)|int) }}"
        turn_on:
          - service: light.turn_on
            data:
              entity_id: light.corrimano_caldo
          - service: light.turn_on
            data:
              entity_id: light.corrimano_freddo
        turn_off:
          - service: light.turn_off
            data:
              entity_id: light.corrimano_caldo
          - service: light.turn_off
            data:
              entity_id: light.corrimano_freddo

Thanks for the hints that made this possible

2 Likes

Hi I try your template and is working, but still look little buggy when I am usin it.

Do you have same situation like on this video when you using it?

001

Errors in ha core regarding this light template:

2022-03-04 17:41:53 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'None' when rendering template '{{ 1000000/(((state_attr('light.livingroom_light_01_white_warm', 'brightness')|float*3000 + state_attr('light.livingroom_light_01_white_cool', 'brightness')|float*6500)/((state_attr('light.livingroom_light_01_white_warm', 'brightness')|int + state_attr('light.livingroom_light_01_white_cool', 'brightness')|int)))) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
2022-03-04 17:41:53 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'float' got invalid input 'None' when rendering template '{{ 1000000/(((state_attr('light.livingroom_light_01_white_warm', 'brightness')|float*3000 + state_attr('light.livingroom_light_01_white_cool', 'brightness')|float*6500)/((state_attr('light.livingroom_light_01_white_warm', 'brightness')|int + state_attr('light.livingroom_light_01_white_cool', 'brightness')|int)))) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2022.1
2022-03-04 17:41:53 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'int' got invalid input 'None' when rendering template '{{ 1000000/(((state_attr('light.livingroom_light_01_white_warm', 'brightness')|float*3000 + state_attr('light.livingroom_light_01_white_cool', 'brightness')|float*6500)/((state_attr('light.livingroom_light_01_white_warm', 'brightness')|int + state_attr('light.livingroom_light_01_white_cool', 'brightness')|int)))) }}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.1
2022-03-04 17:41:53 WARNING (MainThread) [homeassistant.helpers.template] Template warning: 'int' got invalid input 'None' when rendering template '{{ 1000000/(((state_attr('light.livingroom_light_01_white_warm', 'brightness')|float*3000 + state_attr('light.livingroom_light_01_white_cool', 'brightness')|float*6500)/((state_attr('light.livingroom_light_01_white_warm', 'brightness')|int + state_attr('light.livingroom_light_01_white_cool', 'brightness')|int)))) }}' but no default was specified. Currently 'int' will return '0', however this template will fail to render in Home Assistant core 2022.1

Another possible solution is to create two helper variables that control the color and temperature.
The input of the sliders update the helpers variables first. Then the values for the warm and cold white is calculated from the helpers.

- platform: template
  lights:
    keuken_werkblad:
      friendly_name: "Keuken werkblad"
      unique_id: lampen_keukenwerkblad
      value_template: "{{ (state_attr('light.keukenwerkblad_channel_3', 'brightness')|int(0) + state_attr('light.keukenwerkblad_channel_4', 'brightness')|int(0)) > 0 }}"
      level_template: "{{ states('input_number.keukenwerkblad_brightness')|int }}"
      temperature_template: "{{ states('input_number.keukenwerkblad_color_temp')|int }}"
      set_level:
        - service: input_number.set_value
          data:
            value: "{{ brightness|int }}"
          target:
            entity_id: input_number.keukenwerkblad_brightness
        - service: light.turn_on
          data:
            entity_id: light.keukenwerkblad_channel_3
            brightness: "{{ ((states('input_number.keukenwerkblad_color_temp')|int-153)/347*states('input_number.keukenwerkblad_brightness')|int)|int }}"
        - service: light.turn_on
          data:
            entity_id: light.keukenwerkblad_channel_4
            brightness: "{{ ((500-states('input_number.keukenwerkblad_color_temp')|int)/347*states('input_number.keukenwerkblad_brightness')|int)|int }}"
      set_temperature:
        - service: input_number.set_value
          data:
            value: "{{ color_temp|int }}"
          target:
            entity_id: input_number.keukenwerkblad_color_temp
        - service: light.turn_on
          data:
            entity_id: light.keukenwerkblad_channel_3
            brightness: "{{((states('input_number.keukenwerkblad_color_temp')|int-153)/347*states('input_number.keukenwerkblad_brightness')|int)|int}}"
        - service: light.turn_on
          data:
            entity_id: light.keukenwerkblad_channel_4
            brightness: "{{((500-states('input_number.keukenwerkblad_color_temp')|int)/347*states('input_number.keukenwerkblad_brightness')|int)|int}}"
      turn_on:
        - service: light.turn_on
          data:
            entity_id: light.keukenwerkblad_channel_3
            brightness: "{{((states('input_number.keukenwerkblad_color_temp')|int-153)/347*states('input_number.keukenwerkblad_brightness')|int)|int}}"
        - service: light.turn_on
          data:
            entity_id: light.keukenwerkblad_channel_4
            brightness: "{{((500-states('input_number.keukenwerkblad_color_temp')|int)/347*states('input_number.keukenwerkblad_brightness')|int)|int}}"
      turn_off:
        - service: light.turn_off
          data:
            entity_id: light.keukenwerkblad_channel_3
        - service: light.turn_off
          data:
            entity_id: light.keukenwerkblad_channel_4
2 Likes

Hi. Trying your code for myself, but seems that сode needs editing now. HA says that there is error in "data: value: " blocks. Have you edited the code for yourself?

Seems to be working well for me. @gaggio code had served me for a year pretty well but would return log errors I was never able to resolve. My suspicion is that with a more talented person than me, either one of these solutions could work well.

This worked well for me (after fixing a couple small errors on the syntax).
However it didn’t let me set the LED strip to max power, e.g. CW 100% + WW 100%, or anything above 50% total power.

I created a new template that solves the original problem in addition to unlocking the option to set the strip up to full power.
The problem is less trivial than it can sound, here is my blog post with a detailed explanation, link to the template code and instructions.

2 Likes

Hi, could you please share those fixes you’re talking about?

Sorry it was just a quick fix while testing the code, I didn’t save it given that I then built a new template.

Okay! thanks for the reply!

@gfra firstly, I just wanted to say what an incredible job you did with writing this post. It is precisely what I wanted/needed for my shelly RGBW2 LED lights with CC/WW lights.

I do have a question/favour to ask. I’m hoping you can help. I’m trying to use the Adaptive Lighting integration to control the Template Light I’ve created using your code. However, it’s not working. I’ve done some troubleshooting and it seems that there is a few problems when calling the services for brightness and colour in Developer Tools and targeting the Template Light.

I initially through this was a problem with the Adaptive Lighting code and I’ve documented what I’ve found as much as I can in their GitHub here. So I’m going to direct there for all the logs etc. I’m hoping this is a quick fix, and appreciate any suggestions for any others here as well. Thanks!

To further add to this, I think the issue is that when calling the turn_on service for the Template Light and attempting to set brightness and colour temp at the same time, the service call fails if the settings are out of bounds for the template light

Happy to know you found it useful :slight_smile:
I’m not familiar with AL, but my bets are on the issue with the out of bound values you described.
At the moment some color temperature parameters are hard coded in the template. I guess it’s possible that something breaks if values outside the range are used.

Making the template parametric for any value of color temperature would require a couple of more input helpers I guess. Not the cleanest solution, maybe it’s worth spending the time integrating the whole thing into a native component.
However the fact that HA still uses mired internally for color temperature is a bit of a show stopper imho.

Thanks for replying and sharing your thoughts on this @gfra. Making this into a full-blown parametric integration is beyond me at this stage. But I was wondering if - as a short term solution - we could make some small tweaks to the yaml for the template so that any out of bounds values are captured and set to their max allowed for that temperature/brightness combination? But I need help with the syntax and logic.

I guess one parameter would have to take precedence of the other in that decision tree? Maybe temperature could always have the highest priority and brightness must follow?

Hi, I did a couple of tests and updated the template, see this commit or the repository.
Now if you force an out of bound color temperature (e.g. calling the service from dev tools) it defaults to the upper or lower limit, depending if you overshot high or low.

This behaviour should be now consistent with how other CWWW lights work (e.g. my shelly duo works like that).

In the previous version the template was just throwing an error, now it sets the max/min temperature. Anyway, it would probably be a good idea to always respect the limits used by the slider in the “light: turn on” HA service (2000K - 6500K).

Amazing! Thanks so much for this. This is definitely an improvement. There does still seem to be an issue with the Light:Turn_On service call when targeting the template light and setting the brightness to something around 50% and the color temp to any value. The template light seems to conform to the color temp setting but the bightness setting is not set. The only way I can change the brightness is by not including the color_temp in the service call and doing it separately.

Is this another quick code change to force the min/max brightness allowed?

I tested it on my light, if I set brightness and color in the same service call it does not set the brightness, only the color.

This is not an easy fix since if you send an arbitrary couple of brightness + color you might end up outside of the reachable region of the CWWW light. For example if you set full brightness at the warmest temperature.
In that case the template has no logic to decide whether brightness or color should be prioritized, in fact this should be a user choice since it depends on the use case.

Looking at the template light docs, it seems that you can pass both color_temp and brightness to the set_color action, at the moment only color_temp is used. Maybe try adding that, however I think you’ll run into other bugs with AL since I suppose it expects a light where you can set any combination of color + brightness, while in my template that’s not possible by design.

I’m on the same boat as you lennon101. WWCW led strip to use with Adaptive Lighting. I have 1 strip that is kinda working on the kitchen (goes to 100% then down), but another one I can not make it work. When changing the brightness or temperature of the new light created by the template, nothing change until i turn the light off and on again.

I checked everything and the I’m starting to suspect that the second shelly RGBW2 is different and do not act like the 1st one. Anyone had such issue before ?

With the service Light: Turn on, when the target is the device but not the entity it work well when adjusting the brightness, but then the temperature is not available to change.