• 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

    javascript for "this month"

    Mango Automation general Discussion
    2
    13
    3.5k
    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
      atkins.chrisw
      last edited by

      I have the need to follow the MAX of a point for each month. I need it to keep logging this peak data every 15 minutes, but as the peak changes throughout the month it will keep using the highest as its logging point. The problem is, I don't know how to write the date script to look at the current month.

      Any ideas out there?

      1 Reply Last reply Reply Quote 0
      • phildunlapP
        phildunlap
        last edited by phildunlap

        You could probably do it most efficiently by some kind of checking if it were the first value in a month. Something like this, on a 15 minute cron:

        var dt = new Date(); var bt = new Date(my.time);
        if(bt.getMonth() != dt.getMonth()) return peak.value; //new month, so whatever is current peak is current max peak
        else if( peak.value > my.value ) return peak.value;
        else return my.value
        

        It may have issues if your system is offline (it won't consider older peak data in the month if it didn't run for the first few days, for some reason). But you can do it the brute force way like this:

        var dt = new Date();
        dt.setDate(1); dt.setHours(0); dt.setMinutes(0); dt.setSeconds(0); dt.setMilliseconds(0);
        
        peakValues = peak.getValuesSince( dt.getTime() );
        var max = -100;
        for(var k in peakValues) {
            var pvt = peakValues[k];
            if(pvt.value > max) max = pvt.value;
        }
        return max;
        

        Of course, if it's that straightforward, max is included in the statistics function, so you could do:

        var dt = new Date();
        dt.setDate(1); dt.setHours(0); dt.setMinutes(0); dt.setSeconds(0); dt.setMilliseconds(0);
        var stats = peak.past(SECOND, (new Date().getTime() - dt.getTime())/1000 );
        return stats.maximumValue;
        
        A 1 Reply Last reply Reply Quote 0
        • A
          atkins.chrisw
          last edited by

          Thanks Phil. Can I use this in the Mango Metapoints?

          1 Reply Last reply Reply Quote 0
          • phildunlapP
            phildunlap
            last edited by

            Yeah, those are provided with Meta points in mind, where "peak" is the name of the context point providing the peak information, and "my" is the self-reference for the meta point.

            1 Reply Last reply Reply Quote 0
            • A
              atkins.chrisw
              last edited by

              Awesome that worked great!!!

              1 Reply Last reply Reply Quote 0
              • A
                atkins.chrisw
                last edited by

                Is there a variation on this equation to figure out a delta for this same "month" period? Like this?

                var dt = new Date();
                dt.setDate(0); dt.setHours(0); dt.setMinutes(0); dt.setSeconds(0); dt.setMilliseconds(0);
                var delta = delta.past(SECOND, (new Date().getTime() - dt.getTime())/1000 );
                return stats.deltaValue;

                					*SUBSTITUTE delta WITH THE POINT NAME VAR
                
                1 Reply Last reply Reply Quote 0
                • phildunlapP
                  phildunlap
                  last edited by phildunlap

                  Hi Chris,

                  Yes! Analog statistics objects do have a 'delta' member variable. You can access it at stats.delta

                  I encourage you to use the print(stats) function to get the object printed below the script entry box.

                  Also, in answering this question I became suspicious there may be some continuous time type shenanigans or an error in the stats.delta value, so, if you think of delta values as being the difference between the last and first values, I would use

                  if( stats.firstValue !== null ) //Check that there is data in the range
                      return stats.lastValue - stats.firstValue;
                  return 0;
                  1 Reply Last reply Reply Quote 0
                  • A
                    atkins.chrisw @phildunlap
                    last edited by

                    @phildunlap Hey Phil,

                    will this reset each beginning of the month. Im trying to establish the peak for each month.

                    1 Reply Last reply Reply Quote 0
                    • phildunlapP
                      phildunlap
                      last edited by phildunlap

                      Yes, it will be using the server's timezone as the month delineation.

                      1 Reply Last reply Reply Quote 0
                      • A
                        atkins.chrisw
                        last edited by

                        ok.

                        Do you have a good one for kwh delta for each month. I have a totalizer that I need to see how much is using each month.

                        1 Reply Last reply Reply Quote 0
                        • phildunlapP
                          phildunlap
                          last edited by phildunlap

                          Update event: start of month
                          Execution delay: 10s (might not matter)

                          var now = new Date();
                          var lastMonth = new Date( now.getTime() - 24*60*60*1000 ); //Get yesterday, definitely in last month
                          lastMonth.setDate(1); lastMonth.setHours(0); lastMonth.setMinutes(0); lastMonth.setSeconds(0); lastMonth.setMilliseconds(0);
                          
                          var oldValue = acc.pointValueBefore( lastMonth.getTime() ); //Get the value just before last month
                          return acc.value - oldValue.value;
                          
                          1 Reply Last reply Reply Quote 0
                          • A
                            atkins.chrisw
                            last edited by

                            So I need a function to keep a running delta during the month. I've tried this, but for some reason it seems the firstDay is actually like 1 day before the last day of the previous month.
                            What have I missed?

                            var date = new Date();
                            
                            var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
                            firstDay.setDate(0);
                            firstDay.setHours(0);
                            firstDay.setMinutes(0);
                            firstDay.setSeconds(0);
                            firstDay.setMilliseconds(0);
                            
                            var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
                            
                            var oldValue = p892.pointValueAfter( firstDay.getTime() );
                            
                            return (p892.value - oldValue.value)/1000;
                            
                            1 Reply Last reply Reply Quote 0
                            • phildunlapP
                              phildunlap
                              last edited by

                              Whoops! My mistake. I shouldn't have been using setDate(0); The first day of the month is 1, and it accepts negative numbers, so 0 is a day before 1.

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