Examples
  • 10 Jul 2023
  • 5 Minutes to read
  • PDF

Examples

  • PDF

Article Summary

The following examples show the 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;

Communication Diagnostics for Poll/Response Protocols:

/*In general terms, the best way to diagnose communication issues is using an expression that checks the quality values of several
tags. However, for some protocols based on poll/response
such as Modbus, a better approach is checking whether or not the tag values are updated.

If the Tag value doesn't change for a set period of time
defined by another tag named Timeout, it will return 1,
once the condition is not met it will return 0 */ 

if ((Date.now()-$.input.Tag.ts)>$.input.Timeout.value)

  $.output=1;
  
else

  $.output=0;

Create an Alarm With an Active Delay Attribute:

//If the Process value is above or below the Setpoint
//for a set period of time, defined by another tag named Delay, it will return 1,
//once the alarm condition is not met it will return 0

if ($.input.Process_Value.value-$.input.Setpoint.value==0)

  $.output=0;
  
else if (($.currentValue.value==0) && (Math.floor((Date.now()-$.currentValue.ts)/1000)<Delay.value))
 
  $.output={value:0, ts:$.currentValue.ts};

else
  
  $.output=1;

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;

Capture a Tag Value at Midnight in a Given Time Zone:

const currentDay = moment.tz($.currentValue.ts, "Europe/Madrid").get("day");
const newDay = moment.tz($.input.Captured_tag.ts, "Europe/Madrid").get("day");

if(currentDay !== newDay){
    $.output = $.input.Captured_tag.value;
} else {
    $.output = $.currentValue;
}

Bit Extraction From a 16-bit-integer Value:

$.output = ($.input.source.value & 0xF000) >> 12; 
// bits 15 to 12 from unit16

$.output = ($.input.source.value & 0xE0) >> 5; 
// bits 7 to 5 from unit16

$.output = ($.input.source.value & 0xF00) >> 8; 
// bits 11 to 8 from unit16

$.output = ($.input.source.value & 0xE000) >> 13;
// bits 15 to 13 from unit16

$.output = ($.input.source.value & 0x1C) >> 2; 
// bits 4 to 2 from unit16

Get the Total RAM for This Machine:

Note:
From N3uron version 1.21.4 onwards, it is possible to retrieve this information through a Node Tag.

//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:

Note:
From N3uron version 1.21.4 onwards, it is possible to retrieve this information through a Node Tag.

//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:

Note:
From N3uron version 1.21.4 onwards, it is possible to retrieve this information through a Node Tag.

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

Get Derived Tags Memory Usage:

Note:
From N3uron version 1.21.4 onwards, it is possible to retrieve this information through a Node Tag.

//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 Derived Tags 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.

Note:
From N3uron version 1.21.4 onwards, it is possible to create a Redundant Tag without requiring an Expression Tag by selecting the Redundant Tag mode.




 

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

 

  • Step 2: Configure the Logger and API for the Derived Tags 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 1 - Derived Tags configuration options


  • 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.

 


Result:

 


Aggregated Tag

In this example, the Derived Tags module will be used to output the result of an aggregation of a source tag in a tag value over a period of time.

 

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


  • Step 2: Configure the Logger and API for the Derived Tags 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 1 - Derived Tags configuration options


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

Figure - Aggregation Tag 1

 

Result:


Node tag

In this example, the Derived Tags module will be used to get information about the disk and the RAM usage in our node.

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


  • Step 2: Configure the Logger and API for the Derived Tags 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 1 - Derived Tags configuration options


  • Step 4: Configure the tag Source section. In this example, Mode is set to Node tag, Type is set to Node information, and the Variable is set to Disk usage and RAM usage respectively in both tags.

Figure - Node Example


Result:

Figure - Node Real Time


Alias Tag

In this example, the Derived Tags module will be used to make a copy of the value of an input tag. 

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


  • Step 2: Configure the Logger and API for the Derived Tags 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 1 - Derived Tags configuration options


  • Step 4: Configure the tag Source section. In this example, Mode is set to Alias tag, and the Input tag is /OPCUA/Kep_Group1_Sine01.

Figure - Alias Tag 1


Result:

Figure - Alias Real time 2

Figure - Alias Real time

Derived Tags Full Product Details


Was this article helpful?

What's Next