• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
CDP Studio logo

CDP Studio

The no-code and full-code software development tool for distributed control systems and HMI

  • Doc
  • Why CDP
    • Software developers
    • Automation engineers
    • Managers
  • Products
    • Automation Designer
    • HMI Designer
    • Maritime HMIs
  • Services
  • Use cases
  • Pricing
  • Try CDP

CDP Studio Documentation

  • Protocols - OPC UA I/O
  • Event Conversion ChaiScripts
  • 5.0.0

EventsToOPC-UA OPC UA TLS Certificate Setup

Event Conversion ChaiScripts

Event Conversion ChaiScripts

The EventsFromOPC-UA and EventsToOPC-UA blocks use ChaiScript expressions in the EventConverterScript property to convert the event from one system to another.

ChaiScript is a strongly typed declarative language with syntax similar to C++. The scripts support the same set of built-in ChaiScript operators and functions that are described in the Chai script documentation.

The EventConverterScript's' can refer to all the source event attributes in the C++ like map<string,any> variable map, with attribute name as the key pointing to the value for that attribute. The script task is to set into similar C++ like map<string,any> variable map of the target event type.

Note: When accessing the map values, the script should be prepared to handle the case when the map key does not exist or the value is not of the expected type. This can be done either by looking if the key exists using mapname.count("keyname") > 0 expression or by using try-catch blocks around the map value access statements. Look at the Example Conversion Script for different examples how these can be used.

CDP Event Attributes

The CDP Event Attributes are always in the map<string,any> named CDPEvent. In EventsToOPC-UA block this map is read-only and acts as the source for conversion, whereas in EventsFromOPC-UA block this map is writeable and its attributes should be valued by the script. The scripts can refer to all CDP event attributes via this map, including these:

AttributeTypeDescription
Handleunsigned intThe CDP handle of the event sender
SenderstringThe CDP path of the event sender
Codeunsigned intThe CDP event code. See CDP Event Code Flags for details. Note, that when the EventsFromOPC-UA block calculates Code to 0, the event is not forwarded to the CDP system at all (this feature can be used to not to ignore some OPC UA events and not to send them to CDP).
Statusunsigned intThe new status of the object that caused the CDP event. For alarms, see the Alarm Status Defines in CDPAlarm documentation.
LevelstringThe alarm level (in case of an alarm event)
TextstringThe event message
DescriptionstringThe event description

OPC UA Event attributes

The OPC UA Event Attributes are always in the map<string,any> named UAEvent. In EventsFromOPC-UA block this map is read-only and acts as the source for conversion, whereas in EventsToOPC-UA block this map is writeable and its attributes should be valued by the script. The script can refer to all OPC UA attributes via this map, including these:

AttributeTypeDescription
EventIdstringThe OPC UA event identifier
MessagestringThe OPC UA event message
Severityunsigned intThe OPC UA event severity
SourceNamestringThe OPC UA event source name
SourceNodestringThe OPC UA event source node id
CommentstringThe OPC UA condition comment
ConditionNamestringThe OPC UA condition name
EnabledState/IdbooleanThe OPC UA alarm two-state-variable EnabledState/Id value (true/false)
ActiveState/IdbooleanThe OPC UA alarm two-state-variable ActiveState/Id value (true/false)
AckedState/IdbooleanThe OPC UA alarm two-state-variable AckedState/Id value (true/false)
ConfirmedState/IdbooleanThe OPC UA alarm two-state-variable ConfirmedState/Id value (true/false)

Note: In addition to these, in EventsFromOPC-UA block scripts all the event attributes that were added to the OPC UA event by the FilterSelect are also available in the UAEvent map. On the other hand, in EventsToOPC-UA block scripts, all the OPC UA event or alarm event attributes can be set via this UAEvent map. Explore your OPC UA server event type definition for the list of available attributes.

Example Conversion Script

As an example, the following is the default ChaiScript code in EventsFromOPC-UA block that calculates the CDP event attributes from OPC UA event attributes. You can find the reasoning next to the statements as ChaiScript comments.

// First, calculate CDP Event Code:
CDPEvent["Code"] = 0; // default to zero that means no event will be forwarded to CDP
try {
  if (UAEvent["EnabledState/Id"]) { // check if the event is from OPC UA enabled alarm/condition
    if (UAEvent["ActiveState/Id"]) { // check if the event is from OPC UA activated alarm/condition
      CDPEvent["Code"] |= 0x1; // hexadecimal value for CDP alarm Set event code
    } else {
      CDPEvent["Code"] |= 0x2; // hexadecimal value for CDP alarm Cleared event code
    }
    if (UAEvent["AckedState/Id"]) {
      CDPEvent["Code"] |= 0x4; // hexadecimal value for CDP alarm Acknowledged event code
    }
  }
} catch (e) {} // prevents the script from exiting with an error when UAEvent map values do not exist or their type is not compatible
// For alarms, calculate CDP Event Status and Level:
CDPEvent["Status"] = 0;
try {
  if (UAEvent["EnabledState/Id"]) {
    CDPEvent["Group"] = "OPC-UA-Alarms"; // set group name for OPC UA alarms
    if (UAEvent["ActiveState/Id"]) {
      if (UAEvent["Severity"] < 100) {
        CDPEvent["Status"] |= 0x1; // hexadecimal value for CDP alarm status Notify+Set
        CDPEvent["Level"] = "NOTIFY";
      } else if (UAEvent["Severity"] < 500) {
        CDPEvent["Status"] |= 0x10; // hexadecimal value for CDP alarm status Warning+Set
        CDPEvent["Level"] = "WARNING";
      } else {
        CDPEvent["Status"] |= 0x100; // hexadecimal value for CDP alarm status Error+Set
        CDPEvent["Level"] = "ERROR";
      }
    }
    if (!UAEvent["AckedState/Id"]) {
      if (UAEvent["Severity"] < 100) {
        CDPEvent["Status"] |= 0x10000; // hexadecimal value for CDP alarm status Notify+Unacked
      } else if (UAEvent["Severity"] < 500) {
        CDPEvent["Status"] |= 0x100000; // hexadecimal value for CDP alarm status Warning+Unacked
      } else {
        CDPEvent["Status"] |= 0x1000000; // hexadecimal value for CDP alarm status Error+Unacked
      }
    }
    if (UAEvent["Severity"] < 100) {
      CDPEvent["Level"] = "NOTIFY"; // set CDP alarm level to NOTIFY for OPC UA alarms with severity less than 100
    } else if (UAEvent["Severity"] < 500) {
      CDPEvent["Level"] = "WARNING"; // set CDP alarm level to WARNING for OPC UA alarms with severity less than 500
    } else {
      CDPEvent["Level"] = "ERROR"; // set CDP alarm level to ERROR for OPC UA alarms with severity greater or equal than 500
    }
  }
} catch (e) {}
if (UAEvent.count("Message") > 0) {
  CDPEvent["Text"] = UAEvent["Message"]; // use Message as CDP event Text, when available
}
if (UAEvent.count("Comment") > 0) {
  CDPEvent["Description"] = UAEvent["Comment"]; // use Comment as CDP event Description, when available
}

Note: The default script is provided as a base for further customization. The script can be modified to suit the specific requirements of the events and alarms occurring in the system.

CDP Event Code Flags

Common event codes used in the CDP framework (note multiple flags can be set at the same time)): Common event codes used in the CDP framework (note that multiple flags can be set at the same time):

NameCodeDescription
AlarmSet0x1The alarm's Set flag/state was set. The alarm changed state to "Unack-Set" (The Unack flag was set if not already set).
AlarmClr0x2The alarm's Set flag was cleared. The Unack state is unchanged.
AlarmAck0x4The alarm changed state from "Unacknowledged" to "Acknowledged". The Set state is unchanged.
AlarmReprise0x40A repetition/update of an event that has been reported before. Courtesy of late subscribers.
SourceObjectUnavailable0x100The provider of the event has become unavailable (disconnected or similar).
NodeBoot0x40000000The provider reports that the CDPEventNode has just booted.

See also EventsFromOPC-UA and EventsToOPC-UA.

EventsToOPC-UA OPC UA TLS Certificate Setup

The content of this document is confidential information not to be published without the consent of CDP Technologies AS.

CDP Technologies AS, www.cdpstudio.com

Get started with CDP Studio today

Let us help you take your great ideas and turn them into the products your customer will love.

Try CDP Studio for free
Why CDP Studio?

CDP Technologies AS
Hundsværgata 8,
P.O. Box 144
6001 Ålesund, Norway

Tel: +47 990 80 900
E-mail: info@cdptech.com

Company

About CDP

Contact us

Services

Partners

Blog

Developers

Get started

User manuals

Support

Document download

Release notes

My account

Follow CDP

  • LinkedIn
  • YouTube
  • GitHub

© Copyright 2025 CDP Technologies. Privacy and cookie policy.

Return to top