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

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.

I have recently installed a Shelly RGBW2 to control a warm + cold white led light strip. Having spent quite some time browsing the HA forums for a similar setup to mine, as many of you I image already know, I arrived to the conclusion that trying to make it operate as a single light entity via templates is not immediately straightforward.

Thanks to the great blog post by @gfra, I had a clearer picture of what the end result would look like.

I quickly realized that HA templates were not the most appropriate solution, given the amount of tasks that they would need to cover inside the (quite limited) templating language:

  • checking the input for out-of-bound values
  • handling a request to set only the color temperature OR only the brightness OR both the color temperature + brightness
  • keeping track of the previous state

I decided therefore for a more direct approach, arriving at the following custom integration:

I would greatly appreciate some feedback if you would like to try it out, before making an official post on this forum for all the rest of the community to see…

P.S.: If anyone is interested in the nitty gritty details of this integration, here is a detailed Python notebook explaining how its math works out.

2 Likes

I just have it installed seem to work perfectly right now, Shelly RGBW2. Will check if Adaptive Lighting work with it, but so far so good ! Way easier that creating custom template for lights.
Thanks a lot for this :wink:

The only issue from my side is the integration missing it’s icon :stuck_out_tongue:

@mion00 colour me impressed! Pun intended! This is some incredible work mate. It completely meets my needs and does exactly what I would expect it to do. I’ve currently integrated this for my Shelly RGBW lights that are controlling a warm and White channel instead of RGB lights. I was using a complicated mass of code per light to do what this integration has done for me with a couple of simple clicks. Well done mate. I’d love to shout you a coffee.