arachnetech / homebridge-mqttthing

A plugin for Homebridge allowing the integration of many different accessory types using MQTT.
Apache License 2.0
466 stars 104 forks source link

RGB+CCT Bulb using CIE color does not support changing color temperature and RGB at the same time #460

Open RaffoNin opened 3 years ago

RaffoNin commented 3 years ago

Here is my current setup

I am using a generic Zigbee RGB+CCT lightbulb with the following configuration:

{ "type": "lightbulb", "name": "xx", "manufacturer": "xx", "model": "xx", "serialNumber": "xx", "firmwareRevision": "xx", "url": "xx", "username": "xx", "password": "xx", "logMqtt": true, "onValue": "ON", "offValue": "OFF", "topics": { "getOn": { "topic": "xx", "apply": "return JSON.parse(message).state" }, "setOn": { "topic": "xx", "apply": "return JSON.stringify({transition: String(0.5), state: String(message)})" }, "getHSV": { "topic": "xx", "apply": "function cie_to_rgb(r,s,t){void 0===t&&(t=254);var a=1-r-s,h=(t/254).toFixed(2),o=h/sr,e=h/sa,v=1.656492o-.354851h-.255038e,i=.707196-o+1.655397h+.036152e,n=.051713o-.121364h+1.01153e;return v>n&&v>i&&v>1?(i/=v,n/=v,v=1):i>n&&i>v&&i>1?(v/=i,n/=i,i=1):n>v&&n>i&&n>1&&(v/=n,i/=n,n=1),v=v<=.0031308?12.92v:1.055Math.pow(v,1/2.4)-.055,i=i<=.0031308?12.92i:1.055Math.pow(i,1/2.4)-.055,n=n<=.0031308?12.92n:1.055Math.pow(n,1/2.4)-.055,v=Math.round(255v),i=Math.round(255i),n=Math.round(255n),isNaN(v)&&(v=0),isNaN(i)&&(i=0),isNaN(n)&&(n=0),[v,i,n]}function RGBtoHSV(r,s,t){1===arguments.length&&(s=r.g,t=r.b,r=r.r);var a,h=Math.max(r,s,t),o=Math.min(r,s,t),e=h-o,v=0===h?0:e/h,i=h/255;switch(h){case o:a=0;break;case r:a=s-t+e(s<t?6:0),a/=6e;break;case s:a=t-r+2e,a/=6e;break;case t:a=r-s+4e,a/=6e}return{h:a,s:v,v:i}}var m=JSON.parse(message),rgb=cie_to_rgb(m.color.x,m.color.y,m.brightness/2.55),hsv=RGBtoHSV(rgb[0],rgb[1],rgb[2]);hsv.h=360hsv.h,hsv.s=100hsv.s,hsv.v=254hsv.v;var result=hsv.h.toString()+\",\"+hsv.s.toString()+\",\"+(m.brightness/2.54).toString();return result;" }, "setHSV": { "topic": "xx", "apply": "function HSVtoRGB(r,a,e){var t,s,o,b,c,n,i,u;switch(1===arguments.length&&(a=r.s,e=r.v,r=r.h),n=e(1-a),i=e(1-(c=6r-(b=Math.floor(6r)))a),u=e(1-(1-c)a),b%6){case 0:t=e,s=u,o=n;break;case 1:t=i,s=e,o=n;break;case 2:t=n,s=e,o=u;break;case 3:t=n,s=i,o=e;break;case 4:t=u,s=n,o=e;break;case 5:t=e,s=n,o=i}return{r:Math.round(255t),g:Math.round(255s),b:Math.round(255o)}}function ScaledHSVtoRGB(r,a,e){return HSVtoRGB(r/360,a/100,e/100)}function rgb_to_cie(r,a,e){var t=.664511(r=r>.04045?Math.pow((r+.055)/1.055,2.4):r/12.92)+.154324(a=a>.04045?Math.pow((a+.055)/1.055,2.4):a/12.92)+.162028(e=e>.04045?Math.pow((e+.055)/1.055,2.4):e/12.92),s=.283881r+.668433a+.047685e,o=88e-6r+.07231a+.986039e,b=(t/(t+s+o)).toFixed(4),c=(s/(t+s+o)).toFixed(4);return isNaN(b)&&(b=0),isNaN(c)&&(c=0),[b,c]}var params=message.split(\",\"),result={},rgb=ScaledHSVtoRGB(params[0],params[1],100),xy=rgb_to_cie(rgb.r,rgb.g,rgb.b);result.color={x:xy[0],y:xy[1]};var b=2.54*params[2];result.brightness=b;return JSON.stringify(result);" }, "getColorTemperature": { "topic": "xx", "apply": "return JSON.parse(message).color_temp" }, "setColorTemperature": { "topic": "xx", "apply": "return JSON.stringify({color_temp: message})" } }, "accessory": "mqttthing" },

Zigbee2MQTT exposes this as the device state:

{ "brightness": 254, "color": { "hue": 357, "saturation": 100, "x": "0.6630", "y": "0.2820" }, "color_mode": "xy", "color_temp": 148, "color_temp_startup": 370, "linkquality": 44, "state": "ON" }

Currently, I can only configure the device to either show colors but it will only mix (X,Y) values to set the color temperatures or I can delete getHSV altogether and just keep the getColorTemperature in order to use the proper warm or cool light.

Suggestion An option to make the GetHSV and the GetColorTemperature to work together to properly represent RGB and Warm and Cool Light. Maybe when only color temperature is changed in Homekit it will only publish {color_temp:) and it'll only publish the values on HSV when color is changed.

arachnetech commented 2 years ago

This may be a HomeKit limitation: HomeKit certainly makes colour temperature available for RGB devices and generates appropriate RGB values when colour temperature is set. However, it may support both independently. I'll try it.

ad-borges commented 2 years ago

Something here, @arachnetech ?

With the massive offer of Zigbee Bulbs in 2022, the prices dropped. Many users are unloading their wifi networks and reducing energy costs by change WiFi to Zigbee using DIY Coordinators.

Zigbee2Mqtt + Mqtthing is natural choice for tasmota users, but now, China Zigbee Bulbs follow the Philips Hue color standards (CIE colorspace- xy mode), and virtually EVERY ZIGBEE RGBCCT BULB uses color_temp to command White "department".

Zigbee is already a reality and we need some things:

@RaffoNin did you find some workaround for this?

arachnetech commented 2 years ago

Out of interest, why are people choosing mqttthing instead of homebridge-z2m for controlling Zigbee devices?

RaffoNin commented 2 years ago

@ad-borges This isn't to talk bad about the plug-in but i think it's really meant to be used with Tasmota.

My solution was to switch from Homebridge to home assistant and use the HomeKit integration to put it in HomeKit.

Again, I'm not saying the plug-in is bad.

RaffoNin commented 2 years ago

@arachnetech It's been so long now but i remember trying to get it to work with zigbee because of adaptive lighting.

Gave up on trying to get adaptive lighting though as it wasn't useful when i tried it with my Nanoleafs.