Sequence Check of Booleans

Hello all,

i would need some help on a function to create a sequence check of multiple incomming booleans after each other:
I have 3 booleans that are incomming one after each other:
first message : msg.payload.valueBool = true
second message : msg.payload.valueBool = false
third message = msg.payload.valueBool = true

Based on that i would like to compare these message "sequence" with an defined sequence in a function node.
The function is currently looking like that:

var expectedSequence = [true, false, true];
var currentSequence = [];

function sequenceValidation(msg) {
  var value = msg.payload.valueBool;


  if (currentSequence.length > expectedSequence.length) {

  var sequenceCheck = JSON.stringify(currentSequence) === JSON.stringify(expectedSequence);

  msg.payload.sequence_check = sequenceCheck;
  msg.payload.sequence = currentSequence;
  return msg;

newMsg = sequenceValidation(msg);
return newMsg;

Currently the Output looks like this, but the append / push operation to the array is not working and therefore the sequence check does not work:


If they are three separate messages you will need to join or store them in context storage

e.g context

let expectedSequence = [true, false, true];
let currentSequence = context.get("currentSequence") || [];
currentSequence = currentSequence.slice(-expectedSequence.length)
let sequenceCheck = currentSequence.join("") === expectedSequence.join("");
msg.payload.sequence_check = sequenceCheck;
msg.payload.sequence = currentSequence;
context.set("currentSequence", currentSequence)
return msg;

Existing solution on forum...

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.