Creating virtual devices in new automation

Discussions about RaZberry - Z-Wave board for Raspberry computer
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Creating virtual devices in new automation

Post 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.
User avatar
PoltoS
Posts: 7571
Joined: 26 Jan 2011 19:36

Re: Creating virtual devices in new automation

Post by PoltoS »

Please check automation/modules/BatteryPolling/index.js for examplr
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Creating virtual devices in new automation

Post 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
User avatar
PoltoS
Posts: 7571
Joined: 26 Jan 2011 19:36

Re: Creating virtual devices in new automation

Post by PoltoS »

There is now Dummy device - use it as a sample code - it creates the device you need.
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Creating virtual devices in new automation

Post 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?
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Creating virtual devices in new automation

Post 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.
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Creating virtual devices in new automation

Post 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?
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Creating virtual devices in new automation

Post 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??
User avatar
kambis
Posts: 36
Joined: 23 Jun 2014 08:06
Location: Germany
Contact:

Re: Creating virtual devices in new automation

Post by kambis »

You might like to have a look on this post:
viewtopic.php?f=3424&t=20528
billyboy
Posts: 61
Joined: 16 Mar 2013 21:33

Re: Creating virtual devices in new automation

Post 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";
	}
}
Post Reply