Examples
    • 14 Jan 2022
    • 2 Minutes to read
    • PDF

    Examples

    • PDF

    Article summary

    The following examples show different expressions that can be used in an Expression Tag.

    Sum of two tags:

    //Returns the sum of the value of tags a and b
    $.output= $.input.a.value + $.input.b.value;
    

    Set a value based on a condition:

     //If the value of a is above the threshold, it will return 1,
    //otherwise it will return 0
    if ($.input.a.value > threshold) $.output =1;
    else $.output = 0;
    

    Increment a global counter based on a condition:

    //First, the global variable counter needs to exist. The counter //then needs to be incremented when the condition is set to true //(a.value must be even)
    if ($.input.a.value % 2 === 0) counter++; 
    //Return counter so that it is saved to the tag;
    $.output=counter;
    

    Get total RAM for this machine:

    //os.totalmem() returns the total memory of the machine in
    //bytes, divided twice by 1024 to obtain MB.
    $.output = os.totalmem()/1024/1024;
    

    Get free RAM for this machine:

    //os.freemem() returns the free memory of the machine in
    //bytes, divided twice by 1024 to obtain MB.
    $.output = os.freemem()/1024/1024;
    

    Get hostname:

    //os.hostname() returns the hostname as a string
    $.output = os.hostname();
    
    

    Get uptime:

    //os.uptime() returns the host machine in seconds as a number
    $.output =  os.uptime();
    

    Get memory usage of Derived Tags:

    //process.memoryUsage() returns an object with memory 
    //information. The rss property holds the total memory usage
    //of the process (in this case DerivedTags), in bytes
    $.output =  process.memoryUsage().rss/1024/1024;
    

    Assigning a quality based on a condition:

    //If the value of the tag is above 100, we set the quality
    //to 16 (BAD_SENSOR_FAILURE)
    if ($.input.a.value > 100) $.output = {value: -1, quality:16}
    else $.output = {value: a.value, quality:192};
    

    Redundant tags

    In this example, the DerivedTags module is used to provide an output tag after evaluating the quality of two redundant tags coming from two redundant nodes linked to a central node.

    Figure 12: Main source node linked to the central node


    Figure 13: Redundant source node linked to the central node


    Figure 14: Central node

     

    • Step 1: Add a new module in the “Modules” section, give it a name, and select “DerivedTags” in the “Module type” field.

    Figure 15: Adding module

     

    • Step 2: Configure the Logger and API for the DerivedTags module. In this example, the default configuration settings have been left unedited, since (in most cases) this is a valid configuration option.
    • Step 3: Configure the module. In this example, the default configuration settings have been left unedited.

    Figure 16: Module configuration

     

    • Step 4: Configure the tag Source section. In this example, Mode is set to Expression tag and Trigger is set to Periodic. The Aliases created are “Amplitude”, which has the Input tag /SITE01/LINE01/MACHINE01/AMPLITUDE of the main node, and “AmplitudeBis”, which has the Input tag /SITE01Bis/LINE01/MACHINE01/AMPLITUDE of the redundant node. Figure N°16 shows the JavaScript written in Expression.

    Figure 17: Tag configuration

     

    Figure 18: Java script Code

     

    Result:

    Figure 19: Redundant tag


    Aggregated tag

    In this example, the DerivedTags module will be used to output the result of an aggregation of a source tag in a tag value over a period of time.Figure 20: The tag “AP” is used for aggregation in this example

     

    • Step 1: Add a new module in the “Modules” section, give it a name, and select “DerivedTags” in the “Module type” field.

    Figure 21: Adding module

     

    • Step 2: Configure the Logger and API for the DerivedTags module. In this example, the default configuration settings have been left unedited, since (in most cases) this is a valid configuration option.
    • Step 3: Configure the module. In this example, the default configuration settings have been left unedited.

    Figure 22: Module configuration

     

    • Step 4: Configure the tag Source section. In this example, Mode is set on Aggregated tag, Aggregation method is set on Average, and the Source Tag is /000_PLANT/010_ZONE/010_MTR/AP.

    Figure 23: Tag configuration

     

    Result:Figure 24: Aggregated tag




    Was this article helpful?

    What's Next