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

Dimmer.js Handler work greatly but got feedback only once if changed by other tool #149

Closed chulochula closed 4 years ago

chulochula commented 5 years ago

Dimmer.js Handler work as expected in Home App. However, if the dimmer value was changed used other tools, like ETS, the feedback value on Home App was shown only once when went from 0% to my setting. Then no more feedback value shown until next switching off.

But I found an old Dimmer handler here about 2016, it worked as expected with correct feedback value, of course missing some features.

Please help.

oliversluke commented 5 years ago

@chulochula - Thank you for sharing this. I think I have the same challenges as you. Thus, could you post the "old" handler you are using? Maybe we can combine the the ideas into one?

happylichuanxi commented 4 years ago

@chulochula - Thank you for sharing this. I think I have the same challenges as you. Thus, could you post the "old" handler you are using? Maybe we can combine the the ideas into one?

hi,have you solve the problem yet?I meet the same problem,any help would be grateful.

songzh96 commented 4 years ago

@chulochula

Try modifying your Dimmer.js

onKNXValueChange(field, oldValue, knxValue) {
        // value for HomeKit
        var newValue;
        log('INFO: onKNXValueChange(' + field + ", "+ oldValue + ", "+ knxValue+ ") ");
        if (this.lastState===true) {
            // still in switch on timeout period
            if (field === 'On' && knxValue === 0) {
                // switched off
                this.lastState=false;
                clearTimeout(this.timer);
                this.timer = undefined;
                this.myAPI.setValue('On', false);
            } // do not accept any Brightness status messages during switch on period

            else if (field === 'Brightness' && knxValue>0) {
                this.lastState=true;
                clearTimeout(this.timer);
                this.timer = undefined;
                this.myAPI.setValue('On', true);    
                this.myAPI.setValue('Brightness', Math.round(knxValue*100/255));
            }

        } else if (this.lastState===false) {
            if (field === 'On' && knxValue === 1) {
                // switching on
                this.lastState=true;
                clearTimeout(this.timer);
                this.timer = undefined;
                this.myAPI.setValue('On', true);
            } else if (field === 'Brightness' && knxValue>0) {
                this.lastState=true;
                clearTimeout(this.timer);
                this.timer = undefined;
                this.myAPI.setValue('On', true);    
                this.myAPI.setValue('Brightness', Math.round(knxValue*100/255));
            }
        }       
    }
micha1983 commented 4 years ago

Hi is there any solution? I've tried your change but this hasn't worked for me.

i don't see any Brightness feedback in the home app if it is changed by another part/app (example ets). In KNXD with the vbusmonitor is see the new status value (2/1/33) with the new brigthness value. But Homebridge doesn't show it in the log. is there somehting wrong configured, that the for the brigthness the listen doesn't work? for the "On" field it's working like a charme.

thanks you in advance.

my config file look like this.

{

"DeviceName": "Spots",
"Services": [
        {
          "ServiceType": "Lightbulb",
          "Handler": "Dimmer",
          "ServiceName": "Spots",
          "Characteristics": [
            {
              "Type": "On",
              "Set": [
                "2/0/30"
              ],
              "Listen": [
                "2/1/30"
              ]
            },
            {
              "Type": "Brightness",
              "Set": [
                "2/0/33"
              ],
              "Listen:": [
                "2/1/33"
              ],
              "DPT": "DPT5.001"
            }
          ],
          "LocalConstants": {
            "FadingTimeMS": 100,
            "InitBrightness": 30,
            "RestoreLastBrightness": true
          }
        }
      ]
    }
songzh96 commented 4 years ago

Hi @micha1983 new version, try again

/* The Dimmer issue is one of the oldest annoyances with Homekit:
 * HomeKit assumes that everyone wants to have each dimmable light switched on at 100% brightness, 
 * and that an additional ON command is good also when adjusting the brightness.
 * Both are wrong.
 * 
 *  There are multiple KNX dimmers on the market (and gateways to dali and others) that behave differently.
 *  
 *  
 */

/* jshint esversion: 6, strict: true, node: true */

'use strict';
/**
 * @type {HandlerPattern}
 */
var HandlerPattern = require('./handlerpattern.js');
var log = require('debug')('DimmerHandler');

/**
 * @class A custom handler to modify HomeKit's annoying dimmer control   
 * @extends HandlerPattern
 */
class Dimmer extends HandlerPattern {

    constructor(knxAPI) {
        super(knxAPI); // call the super constructor first. Always.
        this.timer = undefined;
        try {
            this.lastKnownBrightness = this.myAPI.getLocalConstant('InitBrightness');
        } catch (e) {
            this.lastKnownBrightness = 100.0;
        }

    }

    /****
     * onKNXValueChange is invoked if a Bus value for one of the bound addresses is received
     * 
     */
    onKNXValueChange(field, oldValue, knxValue) {
        // value for HomeKit
        var newValue;
        log('INFO: onKNXValueChange(' + field + ", " + oldValue + ", " + knxValue + ") ");

        // still in switch on timeout period
        if (field === 'On' && knxValue === 0) {
            // switched off

            clearTimeout(this.timer);
            this.timer = undefined;
            this.myAPI.setValue('On', false);
        } // do not accept any Brightness status messages during switch on period

        else if (field === 'On' && knxValue === 1) {
            // switching on

            clearTimeout(this.timer);
            this.timer = undefined;
            this.myAPI.setValue('On', true);
        } else if (field === 'Brightness' && knxValue > 0) {

            clearTimeout(this.timer);
            this.timer = undefined;
            this.myAPI.setValue('On', true);
            this.myAPI.setValue('Brightness', Math.round(knxValue*100/255));
        }

    } // onBusValueChange

    /****
     * onHKValueChange is invoked if HomeKit is changing characteristic values
     * 
     */
    onHKValueChange(field, oldValue, newValue) {
        // 
        log('INFO: onHKValueChange(' + field + ", " + oldValue + ", " + newValue + ")");

        if (field === 'On') {
            if (newValue === true) {
                if (this.timer) {
                    // the timer is still running, so we do not accept any new ON commands from homekit

                    //clearTimeout(this.timer);
                } else {

                    // light was off before
                    if (this.myAPI.getLocalConstant('RestoreLastBrightness')) {
                        // use only on 1 ping, vassily
                        // restore brightness 
                        this.myAPI.knxWrite('Brightness', this.lastKnownBrightness);

                    } else {
                        // we only use the ON command once to switch the light on
                        this.myAPI.knxWrite('On', true);
                    }
                    // start the countdown
                    this.timer = setTimeout(function () {
                        this.timer = undefined;
                    }.bind(this), this.myAPI.getLocalConstant('FadingTimeMS'));

                }
            } else {
                // switching off

                // if it was on before, switch it off
                this.myAPI.knxWrite('On', false);
                clearTimeout(this.timer);
                this.timer = undefined;

            }
        } else if (field === 'Brightness') {
            var knxValue = newValue;

            // it was off before
            if (newValue === 100) {
                // HomeKit meant "On"
                if (this.myAPI.getLocalConstant('RestoreLastBrightness')) {
                    // use only on 1 ping, vassily
                    // restore brightness 
                    this.myAPI.knxWrite('Brightness', this.lastKnownBrightness);

                } else {
                    // we only use the ON command once to switch the light on
                    this.myAPI.knxWrite('On', true);
                }
                // start the countdown
                this.timer = setTimeout(function () {
                    this.timer = undefined;
                }.bind(this), this.myAPI.getLocalConstant('FadingTimeMS'));

            } else {
                this.myAPI.knxWrite('Brightness', knxValue);
                this.lastKnownBrightness = knxValue;
            }
            //start to count down
            this.timer = setTimeout(function () {
                this.timer = undefined;
            }.bind(this), this.myAPI.getLocalConstant('FadingTimeMS'));

            // it was already lit
            if (!(this.timer && newValue === 100)) {
                // not 100% and within the timeout period, good to send!
                this.myAPI.knxWrite('Brightness', knxValue);
                this.lastKnownBrightness = knxValue;
                // start new timeout to avoid flickering by knx feedback
                clearTimeout(this.timer);
                this.timer = setTimeout(function () {
                    this.timer = undefined;
                }.bind(this), this.myAPI.getLocalConstant('FadingTimeMS'));

            }
        }
    }// onHKValueChange

} // class  
module.exports = Dimmer;
micha1983 commented 4 years ago

Hi @songzh96 i've checked it. still the same issue. the brigthnesscontrol is working probably. but if i change the brigthness value via my Gira X1 app or any other tool .the brightness value is not updated at the homekit. when i open the vbusmonitor of KNXD i can see the status telegram of the new brigthness value.

how can i be sure, that homebridge is working with the new brigthness information? is it working for you in a right way? so you get the feedback?

songzh96 commented 4 years ago

Hi, @micha1983 , Yeh I`ve tested it. I have been using this file and it works very well

  1. shutdown homebridge
  2. Find the homebridge-knx / lib / addins folder and replace this file
  3. save it
  4. restart homebridge
  5. check your config.json,KNXD: your KNX gateway IP. not 127.0.0.1
micha1983 commented 4 years ago

@songzh96 thanks for support. why 127.0.0.1 is not allowed?

songzh96 commented 4 years ago

@songzh96 thanks for support. why 127.0.0.1 is not allowed?

Have you solved it? If you use 127.0.0.1, the KNX msg will sometimes not be transmitted.

micha1983 commented 4 years ago

no :-(

should i use the external ip of my raspby instead of 127.0.0.1?

songzh96 commented 4 years ago

try use your knx-gateway ip, or try use KNX multicast ip

micha1983 commented 4 years ago

but then knxd is not used. is it necessary?

i'm confused.

songzh96 commented 4 years ago

Can you post your knxd config and homebridge config first

micha1983 commented 4 years ago

Homebridge config.json

{
    "bridge": {
        "name": "Homebridge",
        "username": "CC:22:3D:E3:CE:33",
        "port": 51826,
        "pin": "031-45-154"
    },
    "description": "Zuhause",
    "platforms": [
        {
            "name": "Config",
            "port": 8080,
            "auth": "none",
            "theme": "red",
            "restart": "sudo -n systemctl restart homebridge",
            "temp": "/sys/class/thermal/thermal_zone0/temp",
            "tempUnits": "c",
            "sudo": true,
            "log": {
                "method": "systemd",
                "service": "homebridge"
            },
            "platform": "config"
        }
    ],
    "accessories": []
}

knx_config.json

{
    "knxd_ip": "raspberrypi.michiathomoe.com",
    "knxd_port": 6720,
    "AllowWebserver": true,
    "AllowKillHomebridge": false,
    "Devices": [
        {
            "DeviceName": "Tischlicht",
            "Services": [
                {
                    "ServiceType": "Lightbulb",
                    "ServiceName": "Tischlicht",
                    "Characteristics": [
                        {
                            "Type": "On",
                            "Set": [
                                "2/0/0"
                            ],
                            "Listen": [
                                "2/1/0"
                            ]
                        }
                    ],
                    "subtype": "SUB_6cd8c47b-c070-4c8b-8b7f-4e99eeb1e377"
                }
            ],
            "UUID": "79a56e88-f085-4d77-8221-850b0bef1604"
        },
        {
            "DeviceName": "Spots",
            "Services": [
                {
                    "ServiceType": "Lightbulb",
                    "Handler": "Dimmer_MW",
                    "ServiceName": "Spots",
                    "Characteristics": [
                        {
                            "Type": "On",
                            "Set": [
                                "2/0/30"
                            ],
                            "Listen": [
                                "2/1/30"
                            ]
                        },
                        {
                            "Type": "Brightness",
                            "Set": [
                                "2/0/33"
                            ],
                            "Listen:": [
                                "2/1/33",
                                "2/0/33"
                            ],
                            "DPT": "DPT5.001"
                        }
                    ],
                    "LocalCobstants": {
                        "FadingTimeMS": 100,
                        "InitBrightness": 30,
                        "RestoreLastBrightness": true
                    },
                    "subtype": "SUB_a1952979-4bc7-4289-9fa0-e1769d8b5b20"
                }
            ],
            "UUID": "a0be2beb-187e-4e58-9e36-42264ff0962e"
        },
        {
            "DeviceName": "Rollo",
            "Services": [
                {
                    "ServiceType": "WindowCovering",
                    "Handler": "GiraJalousieActuator",
                    "ServiceName": "Esszimmer Rollo",
                    "Characteristics": [
                        {
                            "Type": "TargetPosition",
                            "Set": [
                                "3/0/31"
                            ],
                            "DPT": "DPT5"
                        },
                        {
                            "Type": "CurrentPosition",
                            "Listen": [
                                "3/1/13"
                            ]
                        },
                        {
                            "Type": "PositionState"
                        }
                    ],
                    "KNXObjects": [
                        {
                            "Type": "ShutterMove",
                            "Listen": "3/1/12",
                            "DPT": "DPT1"
                        }
                    ],
                    "KNXReadRequests": [
                        "3/0/31",
                        "3/1/13"
                    ],
                    "LocalConstants": {
                        "TimeOutSecs": 30
                    },
                    "subtype": "SUB_7ce23a6b-42fc-42cf-98ac-f02eaefa657a"
                }
            ],
            "UUID": "7c0ff750-66dd-4c33-ac6a-28aa0a338011"
        }
    ],
    "GroupAddresses": []
}
micha1983 commented 4 years ago

f* typing error

micha1983 commented 4 years ago

michiathomoe.com is wrong

michiathome.com is correct

micha1983 commented 4 years ago

no homebridge is starting up again, but the feedback is not working.

songzh96 commented 4 years ago

Do you have teamviwer, So I can remote control you ,can we use email to talk

micha1983 commented 4 years ago

yes. i send you an email

micha1983 commented 4 years ago

hello all, this Dimmer works like expected.

i had a typo error.

 ],
              "Listen:": [
                "2/1/33"
              ],

in the qoutation mark was an : added.