Stateful Operations
Sharing data between uplink decodings or rule executions
Alongside user-defined inputs and parameters for Custom Device Types and Custom Logic Blocks, the implicit event property called state is provided. Using the state allows users to persist and share data ("memory") between different uplinks (state per device) and rule executions (state per rule). This can be achieved by emitting the state which will be accessible in the next invocation. The state property is shared between uplinks of the same device or for the rule engine for runs of the same rule, regardless of the source that triggered a rule execution.
Accessing the rule state property from a Custom Device Type or Custom Logic Block script:
function consume(event) {
var state = event.state || {};
//your implementation goes here...
}An example of updating a rule state property from Custom Logic Block script:
function consume(event) {
var state = event.state || {};
//your implementation goes here...
state["numOfSamples"] = numOfSamples;
emit('state', state);
}or
function consume(event) {
//your implementation goes here...
emit('state', { "numOfSamples": numOfSamples });
}Example Stateful Custom Logic Block
An example of a Custom Logic Block script that makes use of the state property:
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:
Event Object
Refer to Event Object in Rule Logic - Custom Logic Blocks for all available properties.
Last updated
Was this helpful?