snowdd1 / homebridge-knx

KNX platform shim for homebridge
https://github.com/nfarina/homebridge
GNU General Public License v2.0
97 stars 56 forks source link

Using RGBWWCWLight.js for Tunable White with LED Controller MDT AKD-0424V.02 #166

Closed Cocco17 closed 3 years ago

Cocco17 commented 4 years ago

Hi everybody, I'm starting to implement the most important devices from my KNX installation to Homebridge. For lighting, there are a couple of normal lights with dimming functions, which work fine with the general "Lightbulb" service provided by HAP. However, in most of my rooms there is a Tunable White LED lighting installed. I was pleased to find the Custom Handler "RGBWWCWLight.js" in the Add-Ins. However, Homebridge crashes every time I set the color of the light.

@snowdd1, @wendelit: Any idea, where this error comes from? My knx_config.json file looks as follows:

{
    "knxd":  true,
    "knxdconnection": "knxd",
    "knxd_ip": "192.168.1.51",
    "knxd_port": 6720,
    "AllowWebserver": true,
    "AllowKillHomebridge": false,
    "Devices": [
        {
            "DeviceName": "Kueche-Panelleuchte",
            "Services": [{
                "ServiceType": "Lightbulb",
                "Handler": "RGBWWCWLight",
                "ServiceName": "Kueche-Panelleuchte",
                "Characteristics": [{
                    "Type": "On",
                    "Set": "5/0/23",
                    "Listen": "5/0/24",
                    "DPT": "DPT1"
                },
                {
                    "Type": "Brightness",
                    "Set": "5/0/33",
                    "Listen": "5/0/34",
                    "DPT": "DPT5.001"
                },
                    {
                        "Type": "ColorTemperature"
                    }],
                "KNXObjects": [
                    {
                        "Type": "WarmWhite",
                        "Set": "5/0/28",
                        "Listen": "5/0/13",
                        "DPT": "DPT5.001"
                    },
                    {
                        "Type": "ColdWhite",
                        "Set": "5/0/27",
                        "Listen": "5/0/12",
                        "DPT": "DPT5.001"
                    }
                ],
                "KNX-ReadRequests": [
                    "5/0/24",
                    "5/0/12",
                    "5/0/13"
                ]
            }]
        },
        {
            "DeviceName": "Kueche-Pendelleuchten",
            "Services": [
                {
                    "ServiceType": "Lightbulb",
                    "ServiceName": "Kueche-Pendelleuchten",
                    "Characteristics": [
                        {
                            "Type": "On",
                            "Set": [
                                "5/0/0"
                            ],
                            "Listen": [
                                "5/0/3"
                            ]
                        },
                        {
                            "Type": "Brightness",
                            "Set": [
                                "5/0/2"
                            ],
                            "Listen": [
                                "5/0/4"
                            ]
                        }
                    ],
                    "subtype": "SUB_24dfc7e0-f47e-452f-9ea8-a7ffeb8979f9"
                }
            ],
            "UUID": "46f8abf4-6b77-4b8a-9179-4399a00d4c82"
        }
    ],
    "GroupAddresses": []
}
wendelit commented 4 years ago

Hallo, It was quite some time since I used this, can you post the error you get from homebrige when it crashes ?

Cocco17 commented 4 years ago

Hi! Actually there is no error shown in the protocol. I've tried to find the log files that are described in the Trouble Shooting section of the homebridge git, but couldn't. Any idea where I should look for a log file that could give us some more information? ([https://github.com/homebridge/homebridge/wiki/Basic-Troubleshooting]())

Homebridge just restarts every time I am adjusting the color.

Cocco17 commented 4 years ago

@wendelit any ideas? :)

wendelit commented 4 years ago

Sorry, but i do not use this anymore so I had to take some time to get into it. I think the problem is that it's written for RGB+warm white+cold white so when you set the color temperature it tries to set the RGB values to 0 but those are not set in your configuration.

Could you try to set the R & G & B value to some KNX adress ?

so in the KNXObjects add these (the adress can be anything) { "Type": "Red", "Set": "1/2/2", "DPT": "DPT5" }, { "Type": "Green", "Set": "1/2/3", "DPT": "DPT5" }, { "Type": "Blue", "Set": "1/2/4", "DPT": "DPT5" },`

Cocco17 commented 3 years ago

Hi @wendelit,

thanks for the response and sorry about the long pause. I just tried your idea but it doesn't seem to be the (only) problem - Homebridge still crashes every time, the device is changed.

Any other ideas?

That is the log from Homebridge:

INFO: onKNXValueChange(On, null, 0)
[8/6/2020, 21:10:00] [KNX] RGBWWCWLight->customServiceAPI.getProperty(On, minValue)
[8/6/2020, 21:10:00] [KNX] RGBWWCWLight->customServiceAPI.getProperty(On, maxValue)
[8/6/2020, 21:10:00] [KNX] RGBWWCWLight->customServiceAPI.getProperty(On, perms)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:00] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:00] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:00] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:00] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
[8/6/2020, 21:10:00] Got SIGTERM, shutting down Homebridge...
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:01] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:02] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
INFO: onKNXValueChange(Brightness, null, 0)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, minValue)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, maxValue)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getProperty(Brightness, perms)
[8/6/2020, 21:10:03] [KNX] RGBWWCWLight->customServiceAPI.getValue(Saturation)
[8/6/2020, 21:10:05] [HB Supervisor] Homebridge Process Ended. Code: 143, Signal: null
[8/6/2020, 21:10:10] [HB Supervisor] Restarting Homebridge...
[8/6/2020, 21:10:10] [HB Supervisor] Starting Homebridge with extra flags: -I
[8/6/2020, 21:10:10] [HB Supervisor] Started Homebridge v1.1.1 with PID: 1895
Cocco17 commented 3 years ago

Hey guys, I figured out that it might be easier to just try it myself, and this is the result:

Custom Handler for Tunable White Lights - CWWW.js

/* Lightbulb extension for Tunable White Lights
* Infos:
*   - used in combination with KNX LED-Controller MDT AKD-0xxx
*   - controls the "absolute" adresses for brightness and color temperature in percent [DPT5.001]
* Issues: 
*   - The LED-Controller offers smooth switch on and off effects for HCL-Mode (Human Centered Lighting). However, it also sends the current brightness value during that, which triggers the handler to adjust the brightness
*       --> Workaround: Deactivate status information during HCL changes. 
*/
/* jshint esversion: 6, strict: true, node: true */
'use strict';
/**
 * @type {./handlerpattern.js~HandlerPattern}
 */
var HandlerPattern = require('./handlerpattern.js');
var log = require('debug')('CWWW');

/**
 * @classdesc A custom handler for light apperatures with tunable white functionality
 * @extends HandlerPattern
 */
class CWWW extends HandlerPattern {

    /****
     * onKNXValueChange is invoked if a Bus value for one of the bound addresses is received
     *
     */
    onKNXValueChange(field, oldValue, knxValue) {
        log('INFO: onKNXValueChange(' + field + ", " + oldValue + ", " + knxValue + ")");
        switch (field) {
            case "On":
                this.myAPI.setValue(field, knxValue); // knxValue: values either 0 or 1
                break;
            case "Brightness":
                this.myAPI.setValue(field, knxValue/255*100); // knxValue: values between 0-255
                break;
            case "ColorTemperature":
                let currentwhite_M = this.convertBetweenMiredandAbsolute(knxValue) // knxValue: values between 0-255
                this.myAPI.setValue(field, currentwhite_M);
                break;
        }
    }

    convertBetweenMiredandAbsolute(value) {
        // K=Kelvin, M=Mired
        var warmwhite_K = this.myAPI.getLocalConstant("max_warmwhite");
        var coldwhite_K = this.myAPI.getLocalConstant("max_coldwhite");
        var currentwhite_K = ((value/255)*(coldwhite_K - warmwhite_K)) + warmwhite_K;
        var currentwhite_M = (Math.pow(10, 6) / currentwhite_K)
        return currentwhite_M;
    }

    // onBusValueChange
/* ********************************************************************************************************************** */
    /****
     * onHKValueChange is invoked if HomeKit is changing characteristic values
     *
     */
    onHKValueChange(field, oldValue, newValue) {
        log('INFO: onHKValueChange(' + field + ", " + oldValue + ", " + newValue + ")");
        switch (field) {
            case "On":
                //skip "turn on" knx message if brightness has just been set
                if ((newValue? 1:0) !== (oldValue? 1:0)) {
                    if (newValue !== 1 || !this.brightnessSet) {
                        this.myAPI.knxWrite(field, newValue, "DPT1");
                    }
                }
                break;
            case "Brightness":
                this.myAPI.knxWrite(field, newValue, "DPT5.001");

                // set "brightness has just been set" flag to true for next 2s
                this.brightnessSet = true;
                var that = this;
                if (this.timer) clearTimeout(this.timer);
                this.timer = setTimeout(function () {
                    that.brightnessSet = false;
                    },2000);
                break;
            case "ColorTemperature":
                var absolute = this.convertBetweenAbsoluteAndMired(newValue);
                log('INFO Writing color temperature in absolute value of ' + absolute + ' to KNX bus');
                this.myAPI.knxWrite(field, absolute, "DPT5.001");
                break;
        }
    }

    convertBetweenAbsoluteAndMired(value) {
        // K=Kelvin, PC=Percent
        var warmwhite_K = this.myAPI.getLocalConstant("max_warmwhite");
        var coldwhite_K = this.myAPI.getLocalConstant("max_coldwhite");
        var currentwhite_K = Math.round(Math.pow(10, 6) / value);
        let currentwhite_PC = ((currentwhite_K - warmwhite_K) / (coldwhite_K - warmwhite_K))*100;
        return currentwhite_PC;
    }
// onHKValueChange
} // class

module.exports = CWWW;

/* **********************************************************************************************************************

config.json The config for that should look like this (Reverse keyword is not allowed for custom handlers)

"Services": [
    {
        "DeviceName": "Panel",
        "Services": [
            {
                "ServiceType": "Lightbulb",
                "Handler": "CWWW",
                "ServiceName": "Panel",
                "Characteristics": [
                    {
                        "Type": "On",
                        "Set": [
                            "5/0/23"
                        ],
                        "Listen": [
                            "5/0/24"
                        ],
                        "DPT": "DPT1"
                    },
                    {
                        "Type": "Brightness",
                        "Set": [
                            "5/0/33"
                        ],
                        "Listen": [
                            "5/0/34"
                        ],
                        "DPT": "DPT5.001"
                    },
                    {
                        "Type": "ColorTemperature",
                        "Set": [
                            "5/0/29"
                        ],
                        "Listen": [
                            "5/0/30"
                        ],
                        "DPT": "DPT5.001"
                    }
                ],
                "KNXReadRequests": [
                    "5/0/24",
                    "5/0/30",
                    "5/0/34"

                ],
                "LocalConstants": {
                    "max_warmwhite": 2700,
                    "max_coldwhite": 6500
                }
            }
        ]
    }
]