I run a z-wave system based on a Raspberry Pi with more than 15 physical devices (montion sensors, door and windor sensors a.s.o.) connected to protect my house. For the entire logic I use only available standard apps.
Now I would like to start writing own apps by using 'Load Custom JS Code' app. I have a dedicated z-wave test system (second Raspberry) to play with so that I do not impact my 'production system' in case of code errors.
I had already a look at zWay Developper Documentation (zwayDev.pdf) but I failed to create/ load a simple JS.
My test system has no physical devices connected but only one virtual device (Element ID = DummyDevice_9/ binary switch) and I would like to create JS code to switch this virtual device from 'on' to 'off'.
Using following URL command on client side to switch the binary device is working well.
http://192.168.2.121:8083/ZAutomation/a ... ommand/off
Can somebody translate this URL into JS code that can be used in 'Load Custom JS Code' app.
I tried already a lot but I always got the error 'Something went wrong' when I tried to save the new app.
Any help is welcome.
Rolf
Simple example JS code for "Load Custom JS Code" app
-
- Posts: 58
- Joined: 03 Jan 2017 18:35
Re: Simple example JS code for "Load Custom JS Code" app
Please check help on http://developer.z-wave.me
This should help:
controller.devices.get("DummyDevice_9").performCommand("off")
This should help:
controller.devices.get("DummyDevice_9").performCommand("off")
-
- Posts: 58
- Joined: 03 Jan 2017 18:35
Re: Simple example JS code for "Load Custom JS Code" app
That works. Many thanks for your help.
Rolf
Rolf
-
- Posts: 58
- Joined: 03 Jan 2017 18:35
Re: Simple example JS code for "Load Custom JS Code" app
I try to use the status of a binary device in an if-command as condition to switch another binary switch.
Can you tell me in which format the binary switch state is reported when I use "get" command.
I tried different styles for the if-command but nothing worked. Can you have a look at below JS code and tell me the correct syntaxfor the "if (status ==...)" part
var status = controller.devices.get("DummyDevice_9");
if (status == 0)
{
controller.devices.get("DummyDevice_12").performCommand("off");
};
Can you tell me in which format the binary switch state is reported when I use "get" command.
I tried different styles for the if-command but nothing worked. Can you have a look at below JS code and tell me the correct syntaxfor the "if (status ==...)" part
var status = controller.devices.get("DummyDevice_9");
if (status == 0)
{
controller.devices.get("DummyDevice_12").performCommand("off");
};
Re: Simple example JS code for "Load Custom JS Code" app
Please check the doc. In your code status stores a reference to vDev. To get status use:
var status = controller.devices.get("DummyDevice_9").get("metrics:level");
if (status === "on") // if we talk about switch binary
var status = controller.devices.get("DummyDevice_9").get("metrics:level");
if (status === "on") // if we talk about switch binary
-
- Posts: 58
- Joined: 03 Jan 2017 18:35
Re: Simple example JS code for "Load Custom JS Code" app
Thank you very much. That works. Meanwhile I extended my script to bind the virtual device "DummyDevice_9 and to switch two other virtual devices if the level of virtual device DummyDevice_9 is changing. I checked zwayDev.pdf document to find the right syntax but when I load the script it is not working. I have the impression that the binding is not working. Is something wrong with the JS code related to binding?
var device_to_be_monitored;
var device_status;
device_to_be_monitored = controller.devices.get(„DummyDevice_9“);
controller.devices.on(device_to_be_monitored.„change:metrics:level“,()
{
device_status = controller.devices.get("DummyDevice_9").get("metrics:level");
if (device_status === "off")
{
controller.devices.get("DummyDevice_12").performCommand("off");
controller.devices.get("DummyDevice_13").performCommand("off");
};
};
);
var device_to_be_monitored;
var device_status;
device_to_be_monitored = controller.devices.get(„DummyDevice_9“);
controller.devices.on(device_to_be_monitored.„change:metrics:level“,()
{
device_status = controller.devices.get("DummyDevice_9").get("metrics:level");
if (device_status === "off")
{
controller.devices.get("DummyDevice_12").performCommand("off");
controller.devices.get("DummyDevice_13").performCommand("off");
};
};
);
Re: Simple example JS code for "Load Custom JS Code" app
Sure you want to use these quotes: „....“ ?RolfKunkel wrote:device_to_be_monitored = controller.devices.get(„DummyDevice_9“);
Re: Simple example JS code for "Load Custom JS Code" app
This look really strange:
And inside you can use this.get("metrics:level") - shorter.
You problem was that once this code runs the "DummyDevice_9" is not yet ready, so device_to_be_monitored is null. You should instead use device name and not object to bind to any possible device with that name created in future.
Please look on modules/BindDevices/index.js as a simple example. It takes names from the config and binds on them
Note that device_to_be_monitored.„change:metrics:level“,() should be "DummyDevice_9", "change:metrics:level", function() {RolfKunkel wrote: var device_to_be_monitored;
var device_status;
device_to_be_monitored = controller.devices.get(„DummyDevice_9“);
controller.devices.on(device_to_be_monitored.„change:metrics:level“,()
{
device_status = controller.devices.get("DummyDevice_9").get("metrics:level");
if (device_status === "off")
{
controller.devices.get("DummyDevice_12").performCommand("off");
controller.devices.get("DummyDevice_13").performCommand("off");
};
};
);
And inside you can use this.get("metrics:level") - shorter.
You problem was that once this code runs the "DummyDevice_9" is not yet ready, so device_to_be_monitored is null. You should instead use device name and not object to bind to any possible device with that name created in future.
Please look on modules/BindDevices/index.js as a simple example. It takes names from the config and binds on them
-
- Posts: 58
- Joined: 03 Jan 2017 18:35
Re: Simple example JS code for "Load Custom JS Code" app
I changed the code accordingly (see below) and kept only one command in the function to read the device level.
When I switch DummyDevice_9 via GUI from on to off and vice versa I get following errors in the log file.
"Testattrappe 1" is the name of DummyDevice_9 in GUI.
18:41 | Error during perform command execution: TypeError: Cannot read property 'get' of null
18:41 | Testattrappe 1 is on
18:41 | Error during perform command execution: TypeError: Cannot read property 'get' of null
18:41 | Testattrappe 1 is off
var device_status;
controller.devices.on("DummyDevice_9", "change:metrics:level", function()
{
device_status = this.get("metrics:level");
}
);
When I switch DummyDevice_9 via GUI from on to off and vice versa I get following errors in the log file.
"Testattrappe 1" is the name of DummyDevice_9 in GUI.
18:41 | Error during perform command execution: TypeError: Cannot read property 'get' of null
18:41 | Testattrappe 1 is on
18:41 | Error during perform command execution: TypeError: Cannot read property 'get' of null
18:41 | Testattrappe 1 is off
var device_status;
controller.devices.on("DummyDevice_9", "change:metrics:level", function()
{
device_status = this.get("metrics:level");
}
);
Re: Simple example JS code for "Load Custom JS Code" app
My fault, should be:
Again, please look into BndDevices module
Code: Select all
controller.devices.on("DummyDevice_9", "change:metrics:level", function(vDev) {
device_status = vDev.get("metrics:level");
});