Page 1 of 2

Custom Javascript code stop working until manually restarted

Posted: 29 Aug 2015 12:54
by simonc
Hi,

I got a Razberry setup with one door sensor that when is triggered start a custom Java code using the App "Load custom JavaScript code".

Everything was working fine until a couple of days ago , now it's not and I have not touched the configuration at all ( not in the OS not in the wave software I have 2.0.1).

Long story short: the sensor is working and communicating , the web interface dashboard is working and I can see the sensor activating but the Java code is not even launched.

If I go in the smart home web guy -> App -> Load custom JavaScript code and save my code again without even touching it everything seems to work again but...if i restart the raspberry everything is "NOT" working again...

It seems that the module ID 7 cannot be loaded...sometimes ( but not always) I see this error messages, in this case I have just rebooted the raspberry :

Code: Select all

[2015-08-29 11:47:48.419] [I] [core] Loading module HTTPDevice from modules/HTTPDevice
[2015-08-29 11:47:48.423] [I] [core] Executing script: /*** HTTPDevice Z-Way HA module *********************************
********** ...
[2015-08-29 11:47:48.424] [I] [core] Loading module CustomUserCode from modules/CustomUserCode
[2015-08-29 11:47:48.426] [I] [core] Executing script:   /*** CustomUserCode ZAutomation module ************************
**************** ...
[2015-08-29 11:47:48.429] [I] [core] Instantiating module 7 from class CustomUserCode
[2015-08-29 11:47:48.430] [I] [core] --- Starting module Load custom JavaScript code
[2015-08-29 11:47:48.434] [I] [core] Executing script: zway.devices[3].instances[0].commandClasses[48].data[1].level.bind(function() { ...
[2015-08-29 11:47:48.466] [E] [core] result: Error: ReferenceError: zway is not defined
    at <anonymous>:1:1
    at CustomUserCode.init (automation/modules/CustomUserCode/index.js:36:30)
    at AutomationController.instantiateModule (automation/classes/AutomationController.js:300:22)
    at AutomationController.<anonymous> (automation/classes/AutomationController.js:392:18)
    at Array.forEach (native)
    at AutomationController.loadModule (automation/classes/AutomationController.js:391:8)
    at AutomationController.<anonymous> (automation/classes/AutomationController.js:409:14)
    at Array.forEach (native)
    at AutomationController.instantiateModules (automation/classes/AutomationController.js:408:46)
    at AutomationController.start (automation/classes/AutomationController.js:147:10)
[2015-08-29 11:47:48.480] [I] [core] Notification: error (core): Cannot instantiate module: CustomUserCode: Error: Uncaught ReferenceError: zway is not defined
[2015-08-29 11:47:48.489] [I] [core] Error: Uncaught ReferenceError: zway is not defined
    at Error (native)
    at CustomUserCode.init (automation/modules/CustomUserCode/index.js:36:30)
    at AutomationController.instantiateModule (automation/classes/AutomationController.js:300:22)
    at AutomationController.<anonymous> (automation/classes/AutomationController.js:392:18)
    at Array.forEach (native)
    at AutomationController.loadModule (automation/classes/AutomationController.js:391:8)
    at AutomationController.<anonymous> (automation/classes/AutomationController.js:409:14)
    at Array.forEach (native)
    at AutomationController.instantiateModules (automation/classes/AutomationController.js:408:46)
    at AutomationController.start (automation/classes/AutomationController.js:147:10)

is there a way to use only cli to handle custom java code?
Can I restart the App Custom Java Code from the cli?

hope sameone helps!


EDIT: Looking at the logs, I don't know if there is a connection between the "zway is not defined error" and the custom code not working anymore, but I forgot to tell you that in the zway log, from the day when Customcode execution was not working there were no more messages, everything was silent...

Re: Custom Java code stop working until manually restarted

Posted: 29 Aug 2015 20:04
by PoltoS
This is a know feature. Z-Wave object might start after your Custom code. So when your code is executed, zway object is not defied yet and hence you get the error.

See for example this: viewtopic.php?t=20849&p=57468#p57380

Re: Custom Java code stop working until manually restarted

Posted: 29 Aug 2015 20:39
by simonc
Thanks for the reply.

But I don't understand the mechanism ... it's my code problem or is a CustomUserCode App Problem?

my personal code is:

Code: Select all

zway.devices[3].instances[0].commandClasses[48].data[1].level.bind(function() {
    if (this.value == true)
         system('/opt/z-way-server/automation/userModules/myscript.bash');
});
Did I have to put some code to wait at the beginning?

Is there somewhere some example? I really don't code in JS...

Re: Custom Java code stop working until manually restarted

Posted: 29 Aug 2015 20:58
by pz1
PoltoS wrote:This is a know feature. Z-Wave object might start after your Custom code. So when your code is executed, zway object is not defied yet and hence you get the error.

See for example this: viewtopic.php?t=20849&p=57468#p57380
Sorry to say this, but the code that you refer to does not work. It is highly unpredictable if the bindings do get made or not. I have to fiddle with many times doing Zway stop/start, which often leads to the loss of my configuration. Also many reboots

Since rc7 update this morning I have been unable to get both my machine to start sending these UDP messages.
Frankly it is a nail to my coffin, and as a matter of fact (regrettably) a show-stopper for me.

Update 20151202 My code (UDP sender) that PoltoS referred to is working robustly for a couple of months now. I have to thank Pofs to point me to a misunderstanding on my side

Re: Custom Java code stop working until manually restarted

Posted: 02 Dec 2015 02:19
by simonc
is there any news about this?

sorry Poltos but I don't understand how the topic you linked is about my problem, and most of all the reason that this is a "feature".

For some time I had no problem after a couple of restarts but now the problem is up again, always.
I updated the Razberry software to the latest stable release but the results are the same.. I don't know what can I do to solve this problem, Razberry is useless in this way ...

It sounds like a bug to me, a bug of custom Java code module that randomly stop working, there is no other way I can explain this...

Simone

Re: Custom Java code stop working until manually restarted

Posted: 02 Dec 2015 11:55
by pz1
I updated my post above

Re: Custom Java code stop working until manually restarted

Posted: 06 Dec 2015 13:45
by simonc
Thank you pz1 for the reply, but I 'm not a Js programmer , I had already read your thread even with the last updates but I thought that your case was different, I really am not in this type of coding.

This is my code:

Code: Select all

zway.devices[3].instances[0].commandClasses[48].data[1].level.bind(function() {
    if (this.value == true)
         system('/opt/z-way-server/automation/userModules/myscript.bash');
});
I just want to execute a bash script if one sensor is alarmed, that's it, what is that I need to check or add there? Is it there or in other files or configuration?

You are talking about iteration bindings in the other thread I really am not in that kind of programming, so here is the simple question:

1) can we write here down an official and working example of launching a shell script when one sensor is alarmed?

Re: Custom Java code stop working until manually restarted

Posted: 07 Dec 2015 02:35
by simonc
Again , I tried to adjust my script in this way:

Code: Select all

this.bindFunc1 = function (zwayName) {
   if (zwayName != "zway")
      return; // you want to bind to default zway instance

   var devices = global.ZWave[zwayName].zway.devices;

   //from here insert your devices

 zway.devices[3].instances[0].commandClasses[48].data[1].level.bind(function() {
    if (this.value == true)
         system('/opt/z-way-server/automation/userModules/sendmailalert-portacasa.bash');
});

};
// process all active bindings
if (global.ZWave) {
   global.ZWave().forEach(this.bindFunc1);
}

// and listen for future ones
global.controller.on("ZWave.register", this.bindFunc1);
The system is not return any "away not defined" error at boot or in any ways , but the script is not launched...

Re: Custom Javascript code stop working until manually resta

Posted: 16 May 2016 21:43
by ricargr
Please do you have any advance in this?
I have the same problem that simon explains


Thank you in advance

Re: Custom Javascript code stop working until manually restarted

Posted: 29 Mar 2018 13:03
by simonc
So , just for who get the same problem but cannot get help from this forum, I found the problem, here down the code that works for me:

Code: Select all

this.bindFunc1 = function (zwayName) {
   if (zwayName != "zway")
      return; // you want to bind to default zway instance

   var devices = global.ZWave[zwayName].zway.devices;

   //from here insert your devices
   devices[3].instances[0].commandClasses[48].data[1].level.bind(function() {
    if (this.value == true)
         system('/opt/z-way-server/automation/userModules/sendmailalert-portacasa.bash');
});

};
// process all active bindings
if (global.ZWave) {
   global.ZWave().forEach(this.bindFunc1);
}

// and listen for future ones
global.controller.on("ZWave.register", this.bindFunc1);