Variable's value persistence between script executions
-
From the Mango Scripting help:
Example
The following is a script that implements the Lorenz equations. Three numeric points must be defined, with variable names of x, y, and z. A cron pattern of '0/2 * * * * ?' causes the script to be run every 2 seconds, which allows the data source to produce a fair amount of readings in a relatively short time.
if (x.value == 0 && y.value == 0 && z.value == 0) {
// Initial point values
y.set(1);
}if (typeof(rho) == "undefined") {
rho = 28;
sigma = 10;
beta = 8/3;
dt = 0.01;
}dx = sigma * (y.value - x.value);
dy = x.value * (rho - z.value) - y.value;
dz = x.value * y.value - beta * z.value;x.set(x.value + dx * dt);
y.set(y.value + dy * dt);
z.set(z.value + dz * dt);Note how the typeof function is used to determine if constants need to be defined. This is an effective way of initializing the script context upon startup of the data source.
If I understand correctly, the rho variable value should persist between script executions as long as the data source is enabled so I can, for example, implement an accumulator variable.
If this is correct, then it isn't working. I had tested and results in variable re-definition with every iteration (script execution).
I could work around this by using an script data point as accumulator, but that implies database read/write operation on every cycle.Is this a bug or am I misunderstanding the point?
-
Seems to work fine. This is a simple counter that works uses a variable:
counter is the variable name of a data point.
var count = counter.value; count = count +1; counter.set(count);
-
Thank you Joel!
Ok, that works, so I must review my script.
Anyway, your example reads the count value from database.A better example for in memory value storage would be:
if (typeof(count) == "undefined") { var count = 0; } count = count +1; counter.set(count);
This also works!
-
Yes, your example is better but the current value of a data point is stored in cache so does not require a database read.