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.
Creating virtual devices in new automation
Re: Creating virtual devices in new automation
Please check automation/modules/BatteryPolling/index.js for examplr
Re: Creating virtual devices in new automation
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
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
There is now Dummy device - use it as a sample code - it creates the device you need.
Re: Creating virtual devices in new automation
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?
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
Almost there!
added in automation/main.js:
The only problem I have now is getting the level during the event. I need a 0 or a 1.
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());
}
});
Re: Creating virtual devices in new automation
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
When I look this post, it does not say how:
viewtopic.php?f=3422&t=20282
Who knows how to get the level from an binary switch?controller.on('device.metricUpdated', function (vdevId, name, value) { .... }); -----> vDev.on('change:metrics:...", function (vDev) { ... });
Re: Creating virtual devices in new automation
Found how to execute a python script when changing the state of a virtual device.
But what about getting the state?
Running "python port.py get 8" returns a "0" or a "1"
How to program this??
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());
}
});
Running "python port.py get 8" returns a "0" or a "1"
How to program this??
Re: Creating virtual devices in new automation
You might like to have a look on this post:
viewtopic.php?f=3424&t=20528
viewtopic.php?f=3424&t=20528
Re: Creating virtual devices in new automation
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??
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";
}
}