Simple Scripting trigger vdev ID

Discussions about Z-Way software and Z-Wave technology in general
gugrim
Posts: 8
Joined: 25 Apr 2022 16:17

Re: Simple Scripting trigger vdev ID

Post by gugrim »

Solved the deactivation problem. Mapped all installed event handling functions to their vDevIds to ensure that the exact same function is passed to controller.devices.off when deactivating.

New diff:

Code: Select all

47c47
<       this.onEvent = function(trigger) {
---
>       this.onEvent = function() {
55d54
<                       self.trigger = trigger;
67,69d65
<       // event handlers for each vDevId
<       self.eventHandlers = [];
< 
72,74c68
<                       var onEvent = function() { self.onEvent(vDevId); };
<                       self.eventHandlers[vDevId] = onEvent;
<                       self.controller.devices.on(vDevId, "change:metrics:level", onEvent);
---
>                       self.controller.devices.on(vDevId, "change:metrics:level", self.onEvent);
88,90c82
<                       var onEvent = self.eventHandlers[vDevId];
<                       if (onEvent)
<                               self.controller.devices.off(vDevId, "change:metrics:level", onEvent);
---
>                       self.controller.devices.off(vDevId, "change:metrics:level", self.onEvent);
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Simple Scripting trigger vdev ID

Post by PoltoS »

The problem with deactivation was introduced by you when you introduced the trigger parameter. Now it looks like a viable solution.

Or not yet.. Check eventHandlers before making one more function - otherwise, if you accidentally type twice the make ID, first function for this vDev will not be saved
gugrim
Posts: 8
Joined: 25 Apr 2022 16:17

Re: Simple Scripting trigger vdev ID

Post by gugrim »

I was never in doubt that the deactivation problem was made by me, since it worked fine before I started messing with the code. I mentioned it in the hope that someone would spot the error.

Anyway, I have now added the check you suggested. Works fine!

The new diff:

Code: Select all

47c47
<       this.onEvent = function(trigger) {
---
>       this.onEvent = function() {
55d54
<                       self.trigger = trigger;
67,69d65
<       // event handlers for each vDevId
<       self.eventHandlers = [];
< 
72,77c68
<                       var onEvent = self.eventHandlers[vDevId];
<                       if (!onEvent) {
<                               onEvent = function() { self.onEvent(vDevId); };
<                               self.eventHandlers[vDevId] = onEvent;
<                       }
<                       self.controller.devices.on(vDevId, "change:metrics:level", onEvent);
---
>                       self.controller.devices.on(vDevId, "change:metrics:level", self.onEvent);
91,93c82
<                       var onEvent = self.eventHandlers[vDevId];
<                       if (onEvent)
<                               self.controller.devices.off(vDevId, "change:metrics:level", onEvent);
---
>                       self.controller.devices.off(vDevId, "change:metrics:level", self.onEvent);
Thanks for all your help,
Gunnar
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Simple Scripting trigger vdev ID

Post by PoltoS »

You can push it to our git, if you want. We accept Pull Requests ;)
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Simple Scripting trigger vdev ID

Post by PoltoS »

BTW, it is now part of the v4.0.0. Thanks for your contribution
Post Reply