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

24 hour Percent Increase / Decrease meta script question on generating History.


  • I am using the following script to create a percent increase or percent decrease historical value entry by comparing the current hour value with the same value exactly 24 hours earlier. This should update once every hour, comparing every hour of the day with the corresponding hour of the previous day.

    var previous = p155.ago(HOUR, 24);
    var current = p155.value;
    return ((current - previous)/previous)*100;
    

    My problem comes when trying to manually create a history by clicking the "Generate History" button. I'm not exactly sure if the comparison is calculating backward through historical entries. Does the p155.ago(HOUR, 24) always calculate from the current time, or when generating history, can it iterate through previous historical values, and subtract 24 hours from those entries instead. Not sure if I'm exactly clear with my question, but if anybody can help with the proper script, that would be great.


  • Hi Ray,

    Your question seems clear. Have you already tried this and are of the opinion it is using the value from 24 hours ago today for every calculation? That's the only thing that seemed unclear in your question to me: whether it had been tried or not.

    The answer is that yes, the ago() call will be relative to the time the script is simulating execution at. So, if the script is generating the history of yesterday, the ago() call should reference two days ago.

    Your script looks okay to me. I guess a caveat to the ago() function is that what it really means is the last value before that time, so if there is a value at that millisecond it is missed.


  • Hi Phil,

    I did try generating history, and the results for the current hour today seemed incorrect compared to to when I validated the script. The validation result value was different for the current hour, than the "Generate History" for the current hour.


  • Okay, I'll give it a shot and let you know what I find....


  • Hi Ray,

    I used a scripting data source to generate data into a virtual point, then ran a meta point over that history I generated to see what would happen. Here's the script that generated the data:

    var now = new Date();
    var end = now.getTime();
    now.setDate(1);
    now.setHours(12);
    now.setMinutes(0);
    now.setSeconds(0);
    now.setMilliseconds(0);
    
    while(now.getTime() < end) {
        p.set(now.getDate(), now.getTime());
        now.setDate(now.getDate()+1);
    }
    

    So now there is 1 counting to 20 at exactly hour 12 and no milliseconds in my data point. Its chart looks like this:
    0_1487694903305_src.png

    For the first run of the meta point, I gave it your script,

    var previous = p.ago(HOUR, 24);
    var current = p.value;
    return ((current-previous)/previous)*100;
    

    and got this chart and data:
    0_1487694990837_no-delay.png
    0_1487694999939_no-delay-data.png

    Which does indeed seem strange! There was only 1 infinite percent increase, and no 200% increase ever. This is caused by that caveat I mentioned. Let's rerun the same script, but now we'll add an execution delay of 1s to the meta point, such that it will have a runtime 1s after these point updates.

    Again, chart and data:
    0_1487695140752_1-sec-delay.png
    0_1487695156336_1-sec-delay-data.png

    Looks a lot better. It's possible it would be better for the ago() function to include timestamps at the exact millisecond it refers to, and it does seem from this testing that it is getting the value from two days ago due to this in the first data set!

    You could try using a 1s execution delay in the meta point, or using something like ago(SECOND, 86399)


  • Your the best Phil,

    ago(SECOND, 86399)
    

    The above took care of my issue. My Generated history looks more accurate now. Thanks so much!