• Recent
    • Tags
    • Popular
    • Register
    • Login

    Please Note This forum exists for community support for the Mango product family and the Radix IoT Platform. Although Radix IoT employees participate in this forum from time to time, there is no guarantee of a response to anything posted here, nor can Radix IoT, LLC guarantee the accuracy of any information expressed or conveyed. Specific project questions from customers with active support contracts are asked to send requests to support@radixiot.com.

    Radix IoT Website Mango 3 Documentation Website Mango 4 Documentation Website Mango 5 Documentation Website

    Variable's value persistence between script executions

    Scripting general Discussion
    2
    5
    2.9k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • A
      aoliver
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • JoelHaggarJ
        JoelHaggar
        last edited by

        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);
        
        1 Reply Last reply Reply Quote 0
        • A
          aoliver
          last edited by

          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!

          1 Reply Last reply Reply Quote 0
          • JoelHaggarJ
            JoelHaggar
            last edited by

            Yes, your example is better but the current value of a data point is stored in cache so does not require a database read.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post