Page 1 of 1

Binding vDev changes

Posted: 13 Apr 2015 14:15
by pz1
I have got the binding on Z-Wave sensor changes working:

Code: Select all

      devices[N].SwitchBinary.data.level.bind(function()
      {
          some code
      });
This is used for sending sensor updates over UDP. I would like to do the same now with metric changes in Virtual Devices. The latest Developers Manual writes about Binding to metric changes on page 67, but that is rather cryptic for me:

Code: Select all

vDev.on(’change:metrics:... ”, function (vDev) ... ); 
Someone around who can give a little bit more explanation, or some example?

Re: Binding vDev changes

Posted: 13 Apr 2015 16:06
by dolpheen
Like this?

Code: Select all

//Get vDev reference by deviceId
binarySensor = controller.devices.get('RemoteHA_3_ZWayVDev_zway_29-0-37');

// Subscribe to metrics:level change of the device
binarySensor.on('change:metrics:level', function (vDev) {
  // The code is called when metrics:level of vDev is changed
  // vDev is the reference of device
  // So you can use it to retieve the level value
  var level = binarySensor.get("metrics:level");
}); 

// Produce the same result as the example above for binarySensor.on
controller.devices.on(binarySensor, 'change:metrics:level', handler)

// Unsubscribe from receiving events
binarySensor.off('change:metrics:level', handler)

Re: Binding vDev changes

Posted: 13 Apr 2015 20:46
by pz1
dolpheen wrote:Like this?

Code: Select all

//Get vDev reference by deviceId
binarySensor = controller.devices.get('RemoteHA_3_ZWayVDev_zway_29-0-37');

// Subscribe to metrics:level change of the device
binarySensor.on('change:metrics:level', function (vDev) {
  // The code is called when metrics:level of vDev is changed
  // vDev is the reference of device
  // So you can use it to retieve the level value
  var level = binarySensor.get("metrics:level");
}); 

// Produce the same result as the example above for binarySensor.on
controller.devices.on(binarySensor, 'change:metrics:level', handler)

// Unsubscribe from receiving events
binarySensor.off('change:metrics:level', handler)
Thanks for the reply. I do get it partially. Don't understand what "handler" means.
Below is a more complete listing for the UPD status updater that I use for z-wave devices. My aim is to get similar functionality for virtual devices I create with a number of userModules I made.

Code: Select all

	var sock = new sockets.udp();
	sock.connect("192.168.1.33", 9091);

	this.bindFunc1 = function (zwayName) {
		if (zwayName != "zway")
			return; // you want to bind to default zway instance

		var devices = global.ZWave[zwayName].zway.devices;

		//from here insert your devices
		devices[2].SwitchBinary.data.level.bind(function () {
			var status = (this.value) ? "on" : "off";
			sock.send("ZWay_2," + status);
		});

		devices[3].SwitchBinary.data.level.bind(function () {
			var status = (this.value) ? "on" : "off";
			sock.send("ZWay_3," + status);
		});
So how does your "handler" relate to my "sock.send("ZWay_2," + status)"

Re: Binding vDev changes

Posted: 13 Apr 2015 21:19
by dolpheen
pz1 wrote: Don't understand what "handler" means.
Handler means function to handle you event.

You can write the function in line of your event.
For ex. in your case

Code: Select all

binarySensor.on('change:metrics:level', function (vDev) {
  var level = binarySensor.get("metrics:level");
  sock.send(vDev.id + ',' + level); //Will send the id of vDev and status - on/off (for binary switch)
}); 
or use function expression assigned to variable

Code: Select all

var handler =  function (vDev) {
  var level = binarySensor.get("metrics:level");
  sock.send(vDev.id + ',' + level); //Will send the id of vDev and status - on/off (for binary switch)
});

//Here we use handler variable that contains function to handle our event
binarySensor.on('change:metrics:level', handler)  
Both codes produce same result in your case, the using of each pattern depends on your program structure.

Re: Binding vDev changes

Posted: 14 Apr 2015 09:20
by pz1
Thanks, I'll give it a try the coming days