# Sequence Check of Booleans

**URL:** <https://discourse.nodered.org/t/sequence-check-of-booleans/75688>\
**Category:** General\
**Tags:** function-node\
**Created:** [24 February 2023 13:01 UTC](https://discourse.nodered.org/t/sequence-check-of-booleans/75688 "2023-02-24T13:01:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakobgabriel](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jakobgabriel/32/68166_2.png) [@jakobgabriel](https://discourse.nodered.org/u/jakobgabriel)\
**Post date:** [24 February 2023 13:01 UTC](https://discourse.nodered.org/t/sequence-check-of-booleans/75688/1 "2023-02-24T13:01:25Z")

</div>

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:

```auto
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:

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

```

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [24 February 2023 13:08 UTC](https://discourse.nodered.org/t/sequence-check-of-booleans/75688/2 "2023-02-24T13:08:14Z")

</div>

If they are three separate messages you will need to [join](https://cookbook.nodered.org/basic/join-streams) or store them in [context storage](https://nodered.org/docs/user-guide/context)

e.g context

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [24 February 2023 14:07 UTC](https://discourse.nodered.org/t/sequence-check-of-booleans/75688/3 "2023-02-24T14:07:01Z")

</div>

Existing solution on forum...

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b51c81367ac76c301299b4348951619e40be0243.gif) ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/5/b51c81367ac76c301299b4348951619e40be0243.gif)

> [@IF time interval between events, THEN](https://discourse.nodered.org/t/if-time-interval-between-events-then/63065/4):
>
> Here is a simple flow for detecting any sequence you wish (adjust it in the function node) [chrome\_DUEMJ7iNo8] Demo flow (use CTRL+I to import) [{"id":"9917b371dd14555c","type":"trigger","z":"b4132d0e1950d012","name":"timeout","op1":"reset","op2":"reset","op1type":"str","op2type":"str","duration":"2","extend":false,"overrideDelay":false,"units":"s","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":1580,"y":80,"wires":[["b4f1358385ad8b13"]]},{"id":"6f86f87ba4e65d92","type":"inject","z…

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [10 March 2023 14:07 UTC](https://discourse.nodered.org/t/sequence-check-of-booleans/75688/4 "2023-03-10T14:07:51Z")

</div>

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