Page 2 of 2

Re: Creating virtual devices in new automation

Posted: 18 May 2014 15:51
by billyboy
I have tried everything but I can't add a virtual device to show up. Could you please give me some working demo code?

Thank you verry mutch.

Re: Creating virtual devices in new automation

Posted: 23 May 2014 00:24
by PoltoS
Please check automation/modules/BatteryPolling/index.js for examplr

Re: Creating virtual devices in new automation

Posted: 16 Jun 2014 09:30
by billyboy
Hi All,

Still I was not able to create a simple virtual device, binary switch which run python code when switched.
Is there no one how has done something like this and want to share his code?
I can run python code from main.js on init state, so that's not the problem, creating a virtual device and run code on switch event is.

Kind regeards,
Roy

Re: Creating virtual devices in new automation

Posted: 18 Jun 2014 12:37
by PoltoS
There is now Dummy device - use it as a sample code - it creates the device you need.

Re: Creating virtual devices in new automation

Posted: 20 Jun 2014 15:46
by billyboy
Thanks 1 step further.
I added 2 dummy devices in the new ui: Z-Way Home Automation UI
But how do I now execute code on a switch change? Can't find it in the UI.
Do I have to do this in code? if yes where?

Re: Creating virtual devices in new automation

Posted: 20 Jun 2014 17:26
by billyboy
Almost there!
added in automation/main.js:

Code: Select all

vDev = controller.devices.get("DummyDevice_bn_12");
vDev.on("change:metrics:level", function (vDev) { 
	var level;
	if (vDev.SwitchBinary.data.level.value == "on")	{
		level = 1;
	} else {
		level = 0;
	}
	try {
		system("python /opt/z-way-server/automation/port.py 7 1");// + level); 
		console.log("Executing: python /opt/z-way-server/automation/port.py 7 " + level);
	} catch(err) { 
		console.log("Failed to execute script system call: "+ err.toString()); 
	}	

});
The only problem I have now is getting the level during the event. I need a 0 or a 1.

Re: Creating virtual devices in new automation

Posted: 23 Jun 2014 09:33
by billyboy
I am trying to get the lavel out of the vDev, but I can't find how??
When I look this post, it does not say how:
viewtopic.php?f=3422&t=20282
controller.on('device.metricUpdated', function (vdevId, name, value) { .... }); -----> vDev.on('change:metrics:...", function (vDev) { ... });
Who knows how to get the level from an binary switch?

Re: Creating virtual devices in new automation

Posted: 23 Jun 2014 10:09
by billyboy
Found how to execute a python script when changing the state of a virtual device.

Code: Select all

	vDev = controller.devices.get("DummyDevice_bn_12");
	vDev.on("change:metrics:level", function (vDev) { 
		var level = vDev.get('metrics:level');
		var strCMD = "python /opt/z-way-server/automation/port.py 7 "
		if (level == "on") {
			level = "1";
		} else {
			level = "0";
		}
		strCMD += level;
		try {
			system(strCMD);
			console.log("Executing: " + strCMD);
		} catch(err) { 
			console.log("Failed to execute script system call: "+ err.toString()); 
		}	

	});
But what about getting the state?
Running "python port.py get 8" returns a "0" or a "1"
How to program this??

Re: Creating virtual devices in new automation

Posted: 13 Jul 2014 20:36
by kambis
You might like to have a look on this post:
viewtopic.php?f=3424&t=20528

Re: Creating virtual devices in new automation

Posted: 30 Jul 2014 15:10
by billyboy
Here the complete code to execute a python script on switching a virtual device.
I also tried to implement code to execute when the z-wave system needs to get the cuurent state,
but I never se it be executed.

Is it correct like this??

Code: Select all

	vDev = controller.devices.get("DummyDevice_bn_12");
	vDev.on("change:metrics:level", function (vDev) { 
		var level = vDev.get("metrics:level");
		setPortState(8, level);
		//getPortState(8);
	});
	vDev.on("get:metrics:level", function (vDev) { 
		vDev.set("metrics:level", getPortState(8));
	});

function setPortState(port, state)
{
	var level;
	if (state == "on") {
		level = "1";
	} else {
		level = "0";
	}
	var strCMD = "python /opt/z-way-server/automation/port.py set " + port + " " + level;
	try {
		var result = system(strCMD);
		console.log("Executing: " + strCMD);
		console.log("Result: " + result);
	} catch(err) { 
		console.log("Failed to execute script system call: "+ err.toString()); 
	}
}

function getPortState(port)
{
	var level = "0";
	var strCMD = "python /opt/z-way-server/automation/port.py get " + port;
	try {
		var result = system(strCMD);
		console.log("Executing: " + strCMD);
	} catch(err) { 
		console.log("Failed to execute script system call: "+ err.toString()); 
	}
	var temp = String(result).substr(2, 1);
	if (temp === "1" )	{
		return "on";
	} else {
		return "off";
	}
}