Simple example JS code for "Load Custom JS Code" app

Tips, Tricks and Scripts to enhance your home automation and workaround known device bugs, limitations and incompatibilities
Post Reply
RolfKunkel
Posts: 58
Joined: 03 Jan 2017 18:35

Simple example JS code for "Load Custom JS Code" app

Post by RolfKunkel »

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

Re: Simple example JS code for "Load Custom JS Code" app

Post by PoltoS »

Please check help on http://developer.z-wave.me

This should help:
controller.devices.get("DummyDevice_9").performCommand("off")
RolfKunkel
Posts: 58
Joined: 03 Jan 2017 18:35

Re: Simple example JS code for "Load Custom JS Code" app

Post by RolfKunkel »

That works. Many thanks for your help.
Rolf
RolfKunkel
Posts: 58
Joined: 03 Jan 2017 18:35

Re: Simple example JS code for "Load Custom JS Code" app

Post by RolfKunkel »

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");
};
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Simple example JS code for "Load Custom JS Code" app

Post by PoltoS »

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
RolfKunkel
Posts: 58
Joined: 03 Jan 2017 18:35

Re: Simple example JS code for "Load Custom JS Code" app

Post by RolfKunkel »

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");
};
};
);
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Simple example JS code for "Load Custom JS Code" app

Post by PoltoS »

RolfKunkel wrote:device_to_be_monitored = controller.devices.get(„DummyDevice_9“);
Sure you want to use these quotes: „....“ ?
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Simple example JS code for "Load Custom JS Code" app

Post by PoltoS »

This look really strange:
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");
};
};
);
Note that device_to_be_monitored.„change:metrics:level“,() should be "DummyDevice_9", "change:metrics:level", function() {

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
RolfKunkel
Posts: 58
Joined: 03 Jan 2017 18:35

Re: Simple example JS code for "Load Custom JS Code" app

Post by RolfKunkel »

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");
}
);
User avatar
PoltoS
Posts: 7562
Joined: 26 Jan 2011 19:36

Re: Simple example JS code for "Load Custom JS Code" app

Post by PoltoS »

My fault, should be:

Code: Select all

controller.devices.on("DummyDevice_9", "change:metrics:level", function(vDev) {
    device_status = vDev.get("metrics:level");
});
Again, please look into BndDevices module
Post Reply