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:
e.g.:
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;

  currentSequence.push(value);

  if (currentSequence.length > expectedSequence.length) {
    currentSequence.shift();
  }

  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:

{"_msgid":"14392d94b3744644","payload":{"valueBool":false,"sequence_check":false,"sequence":[false]},"topic":""}

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.push(msg.payload.valueBool);
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.