akenza.io
Search…
Stateful Operations
Sharing data between rule runs
Alongside user-defined inputs and parameters for Custom Logic Blocks, the implicit event property called state is provided. Using the state allows users to persist and share data between different rule runs ("memory"). This can be achieved by the emitting state which will be accessible in the next rule run. The state property is shared only between runs of the same rule, regardless of the source that triggered a rule execution.
Accessing the rule state property from a Custom Logic Block script:
1
function consume(event) {
2
var ruleState = event.state || {};
3
//your implementation goes here...
4
}
Copied!
Initially event.state property is an empty object!
An example of updating a rule state property from Custom Logic Block script:
1
function consume(event) {
2
var ruleState = event.state || {};
3
//your implementation goes here...
4
ruleState["numOfSamples"] = numOfSamples;
5
emit('state', ruleState);
6
}
Copied!
or
1
function consume(event) {
2
//your implementation goes here...
3
emit('state', { "numOfSamples": numOfSamples });
4
}
Copied!
An example of a Custom Logic Block script that makes use of the state property:
1
function consume(event) {
2
var temperature = event.inputs["temp1"];
3
var threshold = event.properties["tempT"];
4
var ruleState = event.state;
5
if (temperature > threshold) {
6
var numOfSamples = ruleState.numOfSamples;
7
if (typeof numOfSamples !== "undefined") {
8
numOfSamples = numOfSamples + 1;
9
} else {
10
numOfSamples = 1;
11
}
12
emit('state', { "numOfSamples": numOfSamples });
13
//Emitting action when numOfSamples is an even number
14
if (numOfSamples % 2 === 0) {
15
emit('action', { "message": "Rule has triggered the action!", "addedTemp": threshold + temperature });
16
}
17
}
18
}
Copied!
The rule state will be reset on rule deactivation or deletion. Once a rule is activated again, the rule state property will be set to a default value.
A more advanced example of using rule state:
1
// rule that adaptively changes the threshold based on the average temperature
2
function consume(event) {
3
const alpha = 0.2;
4
const initialThreshold = event.properties.initialThreshold;
5
const currentTemperature = event.inputs.temperature;
6
7
let state = event.state || {};
8
let movingAverage = state.movingAverage || 0;
9
let threshold = state.threshold || initialThreshold;
10
11
let numberOfSamples = state.numberOfSamples || 0;
12
13
//compute a moving average
14
movingAverage =
15
(currentTemperature + numberOfSamples * movingAverage) /
16
(numberOfSamples + 1);
17
18
// let's define the threshold by 20% above the average
19
const currentThreshold = movingAverage * 1.2;
20
21
// exponentially smooth the threshold in order to account
22
// for the phase where not much data is present
23
threshold = alpha * currentThreshold + (1 - alpha) * threshold;
24
if (currentTemperature > threshold) {
25
emit("action", {
26
message: `temperature (${currentTemperature}°C) is above threshold (${threshold})`,
27
});
28
}
29
30
numberOfSamples += 1;
31
state = { threshold, numberOfSamples, movingAverage };
32
emit("state", state);
33
}
Copied!

Event Object

The Custom Logic Block script is invoked with the event object as param. It contains the following properties & strucutre:
  • inputs object, contains the values of the specified variables
  • state object, contains the user defined rule state
  • type string, indicates how the rule was invoked (either timer or uplink)
  • dataSources object, see data source properties
  • device object, see device properties
  • properties object, contains the user defined properties of the custom logic block
Note: if type has the value of timer certain properties will behave differently:
  • the variable values of the inputs object will be null
  • dataSources will be an empty object
  • device will be undefined

Device Properties

The following properties of a device can be accessed when using template syntax.
Note: The prefix device. is always required in order to access sub properties.
  • name the device name
  • description the device description
  • integrationId the integrationId of the device (only for LoRaWAN devices)
  • workspaceId the workspaceId of the device
  • dataFlowId the data flow id of the device
  • connectivity the connectivity of the device
  • id the akenza device id
  • deviceId the unique device id

Data Source Properties

The following properties of a data source can be accessed when using template syntax.
To access a specific data source, use dataSources.X where X is the number of the data source as specified in the rule (starting at 1).
Some properties can be null in certain circumstances (e.g. the correlationId or the device) if e.g. the rule was triggered by a timer event or the data source is set to access the last sample.
Note: The prefix dataSources.X. is always required in order to access sub properties.
  • correlationId the correlation id of the data source (only available if the data source was triggering the flow)
  • device the complete device object. See Device Properties for sub properties
  • deviceId the unique device id
  • akenzaDeviceId the akenza device id
  • topic the topic of the sample
  • timestamp the timestamp of the sample
  • data.* access any values of the sample.
  • meta.* access any values of the meta object
  • uplinkMeta the complete uplink meta object
  • trigger boolean, indicates whether the data source has triggered the uplink
  • deviceInput boolean, indicates if the data source is a device
  • tagInput boolean, indicates if the data source is a tag
The values related to the sample (namely correlationId, topic, timestamp, data, meta and uplinkMeta) are resolved based on which data source triggered the rule evalution. If the data source is triggering (trigger = true) the rule, the values will be the one from the triggering uplink. Otherwise the values will correlate the most recent stored sample.
The following properties of uplink meta data can be accessed when using template syntax
Note: The prefix uplinkMeta. is always required in order to access sub properties.
  • dataReceived the ISO-8601 timestamp when the data was recieved
  • bytesReceived the number of bytes received in the uplink request
  • processingStart the ISO-8601 timestamp when the processing was started
  • scriptRunUplinkStart the ISO-8601 timestamp when script run was started
  • scriptRunUplinkEnd the ISO-8601 timestamp when script run ended
  • processingEnd the ISO-8601 timestamp when the processing ended
  • outputProduced the ISO-8601 timestamp when all output were produced
  • uplinkDuration the ISO-8601 duration of the whole uplink flow
  • processingDuration the IOS-8601 duration of the processing
  • scriptRunningDuration the ISO-8601 duration of the script run
Copy link
Contents
Event Object