How to setup and event on rate of change?
-
Yeah, just checking. That script I posted surely runs for me, here's a version of yours with lots of print statements, maybe it will help find the issue?
print("bbl1:"); print(bbl1); var data = bbl1.pointValuesSince(new Date().getTime() - 5*60*1000); //Five minutes in milliseconds print("data:"); print(data); if( data.length <= 1 ) //we have the same edge conditions for our range return false; print("Passed the length condition..."); var firstValue = data[0]; var changeThreshhold = -20; //so, this would mean that if any point in the period it is more than for( var k = 1; k < data.length; k+=1 ) { if( firstValue.value - data[k].value < changeThreshhold) return true; } return false;
If you see no output, I encourage you to 1) check for updates, 2) clear your browsers cache, 3) delete Mango/work/jsp and then try again.
-
Hi Phil, it doesn't seem to run. Nothing appears from the debug. I also deleted the jsp directory and still the same.
-
Are you getting errors in the log, or just on the script's page? Is it still reporting null at line 4? Did you try a very simple script, just
return false;
And see if it reports success result=false?
Well, would it be possible for you to email me the JSON for your Mango and I'll try to find the problem locally? I'd like your whole configuration.
-
Looking at the log file, it is still failing at this line.
var firstValue = data[0];
ERROR 2016-07-29 07:39:00,004 (com.serotonin.m2m2.meta.JavaScriptPointLocatorRT.executeImpl:54) - sun.org.mozilla.javascript.EvaluatorException: Java class "java.util.ArrayList" has no public instance field or method named "0". (<Unknown Source>#9) in <Unknown Source> at line number 9 in <Unknown Source> at line number 9
I export and send you the file.
-
It works fine for me, so, I wonder if it's a Java version issue.
Judging from that error message, I would try using data.get(0) instead of data[0]
Can you run "java -version" on the command line and report the result?
-
It's running on a Centos system and the "java -version" command gives me this error.
Error occurred during initialization of VM Could not reserve enough space for object heap Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.
I have Mango running as a service on this machine. Java is install and running otherwise Mango won't run.
-
With .get(0) I get this error.
ERROR 2016-07-29 08:13:13,576 (com.serotonin.m2m2.meta.JavaScriptPointLocatorRT.executeImpl:54) - sun.org.mozilla.javascript.EcmaError: TypeError: Cannot find function get in object {minimumValue: 99.05402923333332, minimumTime: 1469804893540, maximumValue: 99.568, maximumTime: 1469805193538, average: 99.15065249671122, sum: 397.38296884523055, count: 4, delta: 0.5139707666666737, integral: 29745.195749013365, firstValue: 99.05402923333332, firstTime: 1469805036550, lastValue: 99.568, lastTime: 1469805193538, periodStartTime: 1469804893540, periodEndTime: 1469805193540}. (<Unknown Source>#9) in <Unknown Source> at line number 9 in <Unknown Source> at line number 9 javax.script.ScriptException: sun.org.mozilla.javascript.EcmaError: TypeError: Cannot find function get in object {minimumValue: 99.05402923333332, minimumTime: 1469804893540, maximumValue: 99.568, maximumTime: 1469805193538, average: 99.15065249671122, sum: 397.38296884523055, count: 4, delta: 0.5139707666666737, integral: 29745.195749013365, firstValue: 99.05402923333332, firstTime: 1469805036550, lastValue: 99.568, lastTime: 1469805193538, periodStartTime: 1469804893540, periodEndTime: 1469805193540}. (<Unknown Source>#9) in <Unknown Source> at line number 9 in <Unknown Source> at line number 9
-
Dave,
That last post shows that you are using the statistics method to get the data. I'll just ignore that and focus on the first problems you were having. You are expecting the method:
bbl1.pointValuesSince()
to return an Array, but it actually returns a Java style ArrayList. The difference is that ArrayLists have a method called size() instead of the member length and accessing the values is done via get(index).
So here is an updated version of your script to run using an array list instead of an array:
var data = bbl1.pointValuesSince(new Date().getTime() - 5*60*1000); //Five minutes in milliseconds if( data.size() <= 1 ) //we have the same edge conditions for our range return false; var firstValue = data.get(0); var changeThreshhold = -20; //so, this would mean that if any point in the period it is more than for( var k = 1; k < data.size(); k+=1 ) { if( firstValue.value - data.get(k).value < changeThreshhold) return true; } return false;
Hope this helps.
-
Thanks Terry!
Terry's code will work on Java 7 and 8. I guess I've mostly been interacting with Java 8... mine only works on Java 8's JavaScript engine. So, you can convert .length to .size() as per Terry's suggestion and use get() instead of square brackets []. I will try to be more conscious of this in the future when contributing example code snippets.
Sorry for the confusion!
-
No worries Phil, without this interaction you would not have known this so in the end we both benefit from the exchange of data.
I am slowly learning Mango and it's pretty impressive what you can do with it. Your only downside of the whole system is the lack of really good documentation on things like the scripting etc. Other than this, with the support on the forum it makes up for this until such time as you have the documentation done.
Thanks Terry, That seems to work this time. There are no errors appearing in the log file now. I just need to wait for the sensor to change and then hopefully I can detect the change.
-
Very true. The community definitely makes me take a closer look at many things, which I enjoy.
Virtual points can be useful to check if your meta point is behaving as you expect.
The blue help icon (?) in sections with scripts has a 'related item' called 'Mango JavaScript' that documents the functions a data point has. Examples are definitely spread around the forum as well. There's a whole section called 'Scripting general discussion'