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:
function consume(event) {
var ruleState = event.state || {};
//your implementation goes here...
}
Initially event.state property is an empty object!
An example of updating a rule state property from Custom Logic Block script:
function consume(event) {
var ruleState = event.state || {};
//your implementation goes here...
ruleState["numOfSamples"] = numOfSamples;
emit('state', ruleState);
}
or
function consume(event) {
//your implementation goes here...
emit('state', { "numOfSamples": numOfSamples });
}
An example of a Custom Logic Block script that makes use of the state property:
function consume(event) {
var temperature = event.inputs["temp1"];
var threshold = event.properties["tempT"];
var ruleState = event.state;
if (temperature > threshold) {
var numOfSamples = ruleState.numOfSamples;
if (typeof numOfSamples !== "undefined") {
numOfSamples = numOfSamples + 1;
} else {
numOfSamples = 1;
}
emit('state', { "numOfSamples": numOfSamples });
//Emitting action when numOfSamples is an even number
if (numOfSamples % 2 === 0) {
emit('action', { "message": "Rule has triggered the action!", "addedTemp": threshold + temperature });
}
}
}
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:
// rule that adaptively changes the threshold based on the average temperature
function consume(event) {
const alpha = 0.2;
const initialThreshold = event.properties.initialThreshold;
const currentTemperature = event.inputs.temperature;
let state = event.state || {};
let movingAverage = state.movingAverage || 0;
let threshold = state.threshold || initialThreshold;
let numberOfSamples = state.numberOfSamples || 0;
//compute a moving average
movingAverage =
(currentTemperature + numberOfSamples * movingAverage) /
(numberOfSamples + 1);
// let's define the threshold by 20% above the average
const currentThreshold = movingAverage * 1.2;
// exponentially smooth the threshold in order to account
// for the phase where not much data is present
threshold = alpha * currentThreshold + (1 - alpha) * threshold;
if (currentTemperature > threshold) {
emit("action", {
message: `temperature (${currentTemperature}°C) is above threshold (${threshold})`,
});
}
numberOfSamples += 1;
state = { threshold, numberOfSamples, movingAverage };
emit("state", state);
}
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 variablesstate
object, contains the user defined rule statetype
string, indicates how the rule was invoked (eithertimer
oruplink
)dataSources
object, see data source propertiesdevice
object, see device propertiesproperties
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 objectdevice
will be undefined
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 namedescription
the device descriptionintegrationId
the integrationId of the device (only for LoRaWAN devices)workspaceId
the workspaceId of the devicedataFlowId
the data flow id of the deviceconnectivity
the connectivity of the deviceid
the akenza device iddeviceId
the unique device id
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 propertiesdeviceId
the unique device idakenzaDeviceId
the akenza device idtopic
the topic of the sampletimestamp
the timestamp of the sampledata.*
access any values of the sample.meta.*
access any values of the meta objectuplinkMeta
the complete uplink meta objecttrigger
boolean, indicates whether the data source has triggered the uplinkdeviceInput
boolean, indicates if the data source is a devicetagInput
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 recievedbytesReceived
the number of bytes received in the uplink requestprocessingStart
the ISO-8601 timestamp when the processing was startedscriptRunUplinkStart
the ISO-8601 timestamp when script run was startedscriptRunUplinkEnd
the ISO-8601 timestamp when script run endedprocessingEnd
the ISO-8601 timestamp when the processing endedoutputProduced
the ISO-8601 timestamp when all output were produceduplinkDuration
the ISO-8601 duration of the whole uplink flowprocessingDuration
the IOS-8601 duration of the processingscriptRunningDuration
the ISO-8601 duration of the script run
Last modified 1yr ago