Binding to value change fires multiple times / bug ?

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
kbs
Posts: 5
Joined: 18 Sep 2015 10:18

Binding to value change fires multiple times / bug ?

Post by kbs »

Hello!

I Activated app "Load custom JavaScript file" and wrote very simple js file with code that binds to sensor value change like this:

zway.devices[6].instances[3].SensorMultilevel.data[1].val.bind(function () {
var val = this.value;
http.request({
url : "http://myserver/temperature_log/?sensor=mysensor&value=" + val,
method : "GET"
});
});

Everything works fine, except that value change is triggered multiple times. I noticed, that when I saved and activated App for the first time, on next value change it triggered only once as supposed to. However, when I Activated and deactivated the app again, it began to trigger twice. When activated and deactivated one more time, it began to fire 3 times and so on. Basically each time I hit deactivate and activate, it just adds one time to stack.

Is this intended behavior?

Best regards
Attachments
Where I hit activate / deactivate
Where I hit activate / deactivate
act_deact.JPG (62.48 KiB) Viewed 7182 times
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: Binding to value change fires multiple times / bug ?

Post by pofs »

You should unsubscribe from zway objects when your module unloads, otherwise you'll have both old and new handlers called. And because both they do the same thing, you might get confused into thinking they're called twice.

"Load custom JavaScript file" is not very much suitable for such task, because it has no place where you can undo your bindings. If sensor value logging (which does pretty much the same as your code) doesn't fit your needs, I'd suggest you to create a custom module.

The other point of developing a custom module is that subscribing to low-level z-wave events is unreliable, because there's no guarantee your code will be executed after zway object is created. You should handle the opposite situation as well.
kbs
Posts: 5
Joined: 18 Sep 2015 10:18

Re: Binding to value change fires multiple times / bug ?

Post by kbs »

Thank you for the answer.

Example was just for testing purposes. What I actually need to do, is to switch GPIOs on/off based on sensor value and my custom logic, and I would like to do that each time when sensor value changes.

If I understood correctly, the correct way of doing this is to develop custom module. The statement "there's no guarantee your code will be executed after zway object is created" seems a bit confusing to me... So what is the correct way or place to write such js code that uses bind? Or you are implying that the code has to somewhat check for zway object availability using setTimeout or smth?

Thanx in advance.
pofs
Posts: 688
Joined: 25 Mar 2011 19:03

Re: Binding to value change fires multiple times / bug ?

Post by pofs »

No, no need to setTimeout or whatever.
You just need to subscribe for Z-Wave binding object creation notification (and you'll receive it if the binding is created after your code startup) and iterate already existing bindings (in case it was created before your code was loaded). Look into the Recipies section, there's a lot of user modules already. You may use them as a basis for your own module.

You may also take a look at the default SwitchControlGenerator module. It uses quite an abstract approach of data binding not directly to low-level data holders, but using high-level message exchange with z-wave binding module. It also saves a lot of effort by unbinding all previously registered bindings with just a single line.
kbs
Posts: 5
Joined: 18 Sep 2015 10:18

Re: Binding to value change fires multiple times / bug ?

Post by kbs »

I salute you, sir! thank you.
Post Reply