Bind and unbind to devices.
Posted: 25 Jul 2017 03:00
Hi. I am trying to write something that turn my SwitchBinary off whenever it's been on for 120 minutes. I decided to use the app "TimerSwitch" for timer.
This is my first attempt to write any code so far. It does the job, but I don’t manage to close binding to the virtual device. And I seem to catch multiple true events from the SwitchBinary (dev17), which I think starts multiple bindings to the virtual device. Does anyone feel like reviewing my code and give me any pointers or suggestions?
Regards, Fredrik.
This is my first attempt to write any code so far. It does the job, but I don’t manage to close binding to the virtual device. And I seem to catch multiple true events from the SwitchBinary (dev17), which I think starts multiple bindings to the virtual device. Does anyone feel like reviewing my code and give me any pointers or suggestions?
Code: Select all
devTimerSwitch = controller.devices.get('TimerSwitch_29');
zway.devices[17].SwitchBinary.data.level.bind( function() {
if (this.value === true) {
devTimerSwitch.performCommand('exact',{ level: 120 });
devTimerSwitch.on('change:metrics:level', function(vDev) {
level = devTimerSwitch.get("metrics:level");
if (level <= 0) {
zway.devices[17].instances[0].SwitchBinary.Set(0);
devTimerSwitch.off('change:metrics:level', function(vDev) {
debugPrint(‘myScript unbinding devTimerSwitch.off');
});
}
});
}
});