domoticz-zigbee2mqtt-plugin icon indicating copy to clipboard operation
domoticz-zigbee2mqtt-plugin copied to clipboard

Simulate tungsten color temp when dimming (code example)

Open d-EScape opened this issue 5 years ago • 0 comments

Hi,

Development of the plugin has come a long way. Thanks!

This is not really a new device, but a suggestion based on personal preference. Bij modifying the rgbw_adapter.py with the code below a led bulb that supports color temperature will emit a warmer color on low levels and colder color for high rightness levels. Just likt a tungsten bulb does. It is still possible to change to other colors and change (only) the brightness in the color picker. The modification only affects the dimmer-slider and using "Set level" in scrips/api calls.

I find the effect very pleasing and led dimming now matches other (non-led) lights in my home. Since this is a personal preference I did nog make it al pull request, but offer this example for consideration. You can tweak the color temperature curve with the variables.

I did the same thing for dimmable_ct_adapter.py, but here's the example for the RGBW lights. There is a downside. Although you are stille able to set any color you want the manual control is not as straight forward as it should be: If your lights is set to e.g. blue and you use the dimmer-slider to increase the brightness it will switch to (warm) white.

If you want I can make a PR for both files, but maybe you have a better option to get the same effect?

from adapters.base_adapter import Adapter
from adapters.generic.mixins.rgb import RGBMixin
from devices.color_colortemp_light import RGBWLight


class RGBWAdapter(Adapter, RGBMixin):
    def __init__(self, devices):
        super().__init__(devices)

        self.dimmer = RGBWLight(devices, 'light', 'state_brightness_color')
        self.devices.append(self.dimmer)

    def convert_message(self, message):
        message = super().convert_message(message)
        
        if 'color_temp' in message.raw:
            message.raw['color_temp'] = int(message.raw['color_temp'] * 255 / 500)

        return message

    def handleCommand(self, alias, device, device_data, command, level, color):
        topic = device_data['friendly_name'] + '/set'

        if command == 'Set Level':
            #if we are only setting the level on a rgbw adapter then add a color temperature to simulate tungsten light
            #you can still use the rgb/ct picker in domoticz to set some other color (and the brightness slider in de rgb/ct picker)
            ctwarm=255                              #warmest color temperature (preference)
            ctcold=100                              #coldest color temperature (preference)
            ctbase=30                               #level at which lights start to get colder (preference)
            ctrange=100-ctbase                      #calculated level range
            ctdelta=ctwarm-ctcold                   #calculated color temperature range
            ctpos=max(0,level-ctbase)/ctrange       #percentage within the level range
#            colortemp=int(ctwarm-ctpos*ctdelta)        #color temperature for this percentage in the ct range
            colortemp=int(ctwarm-ctpos*ctpos*ctdelta)       #color temperature for this percentage in the ct range exponentially
            #now replace the 'set level' command with a set color command, so CT can be included
            color='{"b":0,"cw":0,"g":0,"m":0,"r":0,"t":'+str(colortemp)+',"ww":'+str(colortemp)+'}'
            command='Set Color'
            Domoticz.Debug('ctpos=' + str(ctpos))
            Domoticz.Debug('converted cmd=' + str(command) + ' ;level=' + str(level) + ' ;color=' + str(color))
        return self.set_color(topic, command, level, color)

d-EScape avatar Oct 31 '20 09:10 d-EScape