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

Capture data as an event for start and finish of a data flow


  • As the title says (hopefully) I'm trying to capture whilst a value transitions from being zero to anything above zero and then from a value back to zero; and store those timestamps into a metapoint/virtual datapoint.
    By doing this it means I can have a user select from a drop down a chosen event (in chronological order) which will provide the starting and end timestamps for the data I need.
    I've been trying to capture this in a metadata point and I'm just having no luck. I even went as far as using the JSON rest api to store data as a placeholder so once I have both the start and end of an 'above 0' period the values are stored into the datapoint.
    And no prevail.
    Has anyone got any ideas or what I could do to implement and ensure I obtain the desired script behaviour?

    I check between two values to see the status and if there is a change and the value is zero I take a snapshot.

    //Start Date
    // 0 --> .>0
    if( values[1].value>values[0].value && values[0].value===0 )
    {
        var tmp = {
          title:"Commencing "+  new Date(parseInt(values[0].time)).toLocaleString("NZST"),
          startTime:values[0].time,
          endTime:''
        };
       // setJSONStore("Effluent-Pump-1","user",tmp);
       events.set( JSON.stringify(tmp) );
       return JSON.stringify(tmp);
      
    }
    
    //end date
    // >0  --> 0
    if( values[1].value<values[0].value && values[1].value===0 )
    { 
        //getJSONStore("Effluent-Pump-1","user",getEventData,{}); //custom global script I threw together.
        var tmp = JSON.parse(IE.value);
        tmp.endTime = values[1].time;
        IE.set( JSON.stringify(tmp), volume.lastValue.time  );
    events.set( JSON.stringify(tmp), volume.lastValue.time  );
    }
    

  • Hi Fox,

    Looking at your script I notice a few things,

    1. events seems to be the point you're setting the JSON to with the initial event, but IE is the point it is being parsed back from. Perhaps this should be my.value ? Is IE for completed events only?
    2. It is not clear how you got the values array, but I've noticed in certain situations you'll need to use values[n].doubleValue instead to explicitly get a double instead of a DataValue. If you used the .last(n) function then I think the list ordering is right for what you're trying to do. I would expect the type checking in the === to fail, for instance.
    3. Not sure your need to parseInt the .time, that should be a long already.

    Hope that helps!


  • Thanks Phil I'll try your advice. By the end of it all I got to the point of throwing caution to the wind and aimlessly trying anything to ensure I could get things formatted correctly and in the right order. I'll look at giving more detail tomorrow.

    Fox


  • I've been working more on it and I get the feeling that I'm not properly getting the pointValue times right.
    I've been using CONTEXT.getRuntime() but I strongly get the feeling that this value is constant for when the script is fired - including when I do a force recalculate history.
    I've the metadata point itself. the variable name IE. events is a virtual datapoint I was hoping to try and catch the data into but I'm thinking I might just leave it in the meta point so I don't double up.
    The other is supposed to be when I've got a pump running. Sorry Phil I'm running circles round myself here!

    var ROLLUPMINS  = 15; 
    var numberOfVals = 2; //I want two values, now, and the previous.
    var MS_PER_MINUTE = 60000;
    var from = new Date(CONTEXT.getRuntime() - (ROLLUPMINS* numberOfVals * MS_PER_MINUTE) );
    var values = [];
    //print(from);
    
    var valueCallback = function( value, index ) { //function definition in com.infiniteautomation.mango.db.query.PVTQueryCallback
      // print(value);print(index);
     // LOG.info(value);
       values.push( value );
    };
    //values.push(volume.last(2) );
    //values.push(volume.last(0));
    //print(volume.lastValue(1));
    //print(volume.pointValuesSince(from));
    
    
    PointValueQuery.query( 
        [ volume.getDataPointWrapper().getId()],
        from.valueOf(), 
        CONTEXT.getRuntime(),
        false, 
        valueCallback);  
    
    print( values );
        
    /*    PointValueQuery.query( 
        [ volume.getDataPointWrapper().getId()],
        _24th, 
        _25th,
        false, 
        valueCallback);  */
    
    
    // Start Date:
    function getEventData(status, headers, content) { //setResponseCallback
    	var data = content;//	print(status);
    //	print(data);
    	 data = JSON.parse(data);
        //print(data.jsonData.startTime);
        data.jsonData.endTime=values[0].time;
        return JSON.stringify(data.jsonData),data.jsonData.startTime;
    }
    
    /*
    if( values[1].value!=values[0].value && values[0].value==0 )
    {
        print("va1 > val2");
        LOG.info("va1 > val2");
        return "va1 > val2";
    }
    
    if( values[1].value!=values[0].value && values[1].value==0 )
    {
        print("va1 < val2");
        LOG.info("va1 < val2");
        return "va1 < val2";
    }*/
    
    print(IE.lastValue().value);
    if( 0<values[1].value && values[0].value==0 )
    {
       
        LOG.info(IE.value);
        if(IE.lastValue().value=='')
        {
            LOG.info(values[0].time);
            var tmp = {
              title:"Commencing "+ new Date(parseInt(values[0].time)).toLocaleString("NZST"),
              startTime:values[0].time,
              endTime:''
            };
    
           events.set( JSON.stringify(tmp) ,values[0].time);
           IE.set(JSON.stringify(tmp),values[0].time);
    
        }
        else
        {
             var last = JSON.parse(IE.lastValue().value);
             if(last.endTime.length)
             {
            LOG.info(values[0].time);
            var tmp = {
              title:"Commencing "+ new Date(parseInt(values[0].time)).toLocaleString("NZST"),
              startTime:values[0].time,
              endTime:''
            };
    
           events.set( JSON.stringify(tmp) ,values[0].time);
           IE.set(JSON.stringify(tmp),values[0].time);
    
             }
        }
    }
    
    //end date
    
    if( values[1].value==0)
        var tmp = JSON.parse(IE.lastValue().value);
        tmp.endTime = values[1].time;
        IE.set( JSON.stringify(tmp), tmp.startTime  );
        events.set( JSON.stringify(tmp), tmp.startTime  );
    }
    

  • Hi Fox,

    I've been using CONTEXT.getRuntime() but I strongly get the feeling that this value is constant for when the script is fired - including when I do a force recalculate history.

    As in the value is the same for each historical execution? That is not the intended behavior, nor what I observed with an Alphanumeric meta point that was doing,

    return CONTEXT.getRuntime();

    and then regenerated history. I could clearly see, whether my point was executing on a cron or context update, that the time returned was not the same. However, it does not change throughout the execution of the script, so,

    print(CONTEXT.getRuntime()); //time the script kicked off
    RuntimeManager.sleep(1000);
    print(CONTEXT.getRuntime()); //still the time the script kicked off
    

    You may also in some occasions want to use CONTEXT.getTimestamp(), which should always be the time of the triggering event without the possibility of server load delaying task execution by a few (or many) milliseconds creeping in as with the runtime (really only affects live values).

    If you're still doing the same sort of thing I'm curious why you'd switch over to the PointValueQuery tool instead of using the last() calls like before. I didn't get too deep into what that last script is trying to achieve. Maybe the issue is that during history generation last() doesn't work properly? You could use

    var pvt = p.pointValueBefore(CONTEXT.getTimestamp()+1);
    var pvt2 = p.pointValueBefore(pvt.time);