Triggers and Actions - open-inc/openware GitHub Wiki

Triggers

Actions

Templating for Actions

Within all parameters and data fields of Action you can make use of a templating context object to fill your action's settings and texts dynamically. Templating can be done either via JSON-Path (old version) or via Apache Velocity Templates.

Templating context

The templating context object gives access to various information that relate to the action being triggered. The following table gives an overview about the context's structure.

Field Description
target The value for target as defined in the Action definition (see Example)
topic The value for topic as defined in the Action definition (see Example)
payload The value for payload as defined in the Action definition (see Example)
options The whole action definition object (see Example)
datasets The data items that triggered the action. May contain multiple items as JSON Array based on the Basic Data Structure
data Shortcut to access the first element of the datasets array, as most of the time only one data item is responsible for triggering actions
user The user that created created the alarm or triggered the action. The user object definition can be found here

JSON-Path Templates

You can use JSON-Path to retrieve information from the template context object, by inserting {{#JSONPATH_STRING_HERE#}} inside any field of the action. It will get replaced by the value that the JSON-Path resolves to.

JSON-Path Example

Action before evaluation:

{
  "_id" : "ID",
  "type" : "email",
  "target" : "{{#$.user.email#}}",
  "topic" : "Alarm {{#$.data.name#}} triggered",
  "payload" : "Der Wert lag bei: {{#$.data.values[0].value[{{#$.options.item.dimension#}}]#}}",
  "extra" : { },
  "label" : "Email Notification",
  "templateType" : "jp"
}

(As you can see in the 'Payload'-Part, it is possible to have nested expressions)

Action after evaluation:

{
  "_id" : "ID",
  "type" : "email",
  "target" : "[email protected]",
  "topic" : "Alarm Temperature Room 1  triggered",
  "payload" : "Der Wert lag bei: 26 °C",
  "extra" : { },
  "label" : "Email Notification",
  "templateType" : "jp"
}

VTL Templates

The preferred way to create templates is to use Apache Velocity Templates (VTL, Docs). They enable you to have more dynamic templates, e.g. using conditionals and loops. The context object as described above is made in available for templating. You can directly access the fields using the variable name (see Example).

VTL Example

Action before evaluation:

{
  "_id" : "ID",
  "type" : "email",
  "target" : "$user.email",
  "topic" : "Alarm $data.name triggered",
  "payload" : "Der Wert lag bei: $data.values[0].value[$options.item.dimension]",
  "extra" : { },
  "label" : "Email Notification",
  "templateType" : "VTL"
}

(As you can see in the 'Payload'-Part, it is possible to have nested expressions)

Action after evaluation:

{
  "_id" : "ID",
  "type" : "email",
  "target" : "[email protected]",
  "topic" : "Alarm Temperature Room 1  triggered",
  "payload" : "Der Wert lag bei: 26 °C",
  "extra" : { },
  "label" : "Email Notification",
  "templateType" : "VTL"
}