# Compare latest 4 messages against mysql

**URL:** https://discourse.nodered.org/t/compare-latest-4-messages-against-mysql/46020
**Category:** General
**Created:** [21 May 2021 08:12 UTC](https://discourse.nodered.org/t/compare-latest-4-messages-against-mysql/46020 "2021-05-21T08:12:30Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Fires04](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fires04/32/42157_2.png) [@Fires04](https://discourse.nodered.org/u/Fires04)
#### Post date: [21 May 2021 08:12 UTC](https://discourse.nodered.org/t/compare-latest-4-messages-against-mysql/46020/1 "2021-05-21T08:12:30Z")

</div>

Hi, I looking for a way how to compare the latest 4 messages against data in MySQL.

I got a keypad that sends a message for every key pressed(MQTT message), now I using sequence-detector to check for a sequence of the correct pin but that sequence-detector works only with "hard set" values in node and there is no way how to set new pin for example with config message. I need to check the sequence of the last 4 messages against MySQL. Any suggestions?

Thanks .

---

<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: [21 May 2021 09:02 UTC](https://discourse.nodered.org/t/compare-latest-4-messages-against-mysql/46020/2 "2021-05-21T09:02:04Z")

</div>

> [@Fires04](#):
>
> now I using sequence-detector to check for a sequence of the correct pin but that sequence-detector works only with "hard set" values in node and there is no way how to set new pin for example with config message

DIY it.

![6nxxhvt7cJ](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/e/9e42aae79eb458ef6d5883bfbc0da56ddd2e4ced.gif)

```auto
[{"id":"d7fba0a9.11671","type":"function","z":"553814a2.1248ec","name":"pin","func":"var seq = context.get(\"seq\") || \"\";\nvar pin = context.get(\"pin\") || \"9999\";\nvar pinLength = pin.length;\n\nfunction showStars(count, colour) {\n var str = new Array(count+1).join(\"*\");\n node.status({ fill: colour, shape: \"ring\", text: str });\n}\n\n\n\nif(msg.topic == \"setpin\") {\n context.set(\"pin\", msg.payload);\n context.set(\"seq\", \"\");\n} else if (msg.topic == \"clear\") {\n context.set(\"seq\", \"\");\n} else {\n seq += (msg.payload + \"\");\n}\n\nshowStars(seq.length, \"red\");\n\nmsg.payload = false; //fail by default\n\nif(seq === pin) {\n seq = \"\"; //reset seq\n msg.payload = true;\n showStars(seq.length, \"green\");\n} else if (seq.length >= pinLength) {\n seq = \"\"; //restart seq\n} \n\n//store seq\ncontext.set(\"seq\", seq);\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":530,"y":1220,"wires":[["4f6e9447.bba25c"]]},{"id":"4f6e9447.bba25c","type":"debug","z":"553814a2.1248ec","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":690,"y":1220,"wires":[]},{"id":"bfa78fcf.4d9c3","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":290,"y":1140,"wires":[["d7fba0a9.11671"]]},{"id":"1b38a685.e629e9","type":"inject","z":"553814a2.1248ec","name":"set pin 1234","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"setpin","payload":"1234","payloadType":"str","x":310,"y":1080,"wires":[["d7fba0a9.11671"]]},{"id":"80fb8831.314068","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"2","payloadType":"str","x":290,"y":1180,"wires":[["d7fba0a9.11671"]]},{"id":"d45fc8b7.38c3f8","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"str","x":290,"y":1220,"wires":[["d7fba0a9.11671"]]},{"id":"b01f67c9.c5fae8","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4","payloadType":"str","x":290,"y":1260,"wires":[["d7fba0a9.11671"]]},{"id":"821671cd.2bc3","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"5","payloadType":"str","x":290,"y":1300,"wires":[["d7fba0a9.11671"]]},{"id":"694ac4.e6e3453c","type":"inject","z":"553814a2.1248ec","name":"","props":[{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"clear","x":290,"y":1360,"wires":[["d7fba0a9.11671"]]}]

```

---

<div class="post-metadata">

### Author: ![Fires04](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/fires04/32/42157_2.png) [@Fires04](https://discourse.nodered.org/u/Fires04)
#### Post date: [21 May 2021 12:27 UTC](https://discourse.nodered.org/t/compare-latest-4-messages-against-mysql/46020/3 "2021-05-21T12:27:55Z")

</div>

I edit it to detect the sequence in stream of messages. Maybe it help someone.

```auto
var pin = context.get("pin") || "9999";
var pinLength = pin.length;
var seqCorrect = context.get("seqCorrect") || 0;
var latestNumber = 0;
function showStars(count, colour) {
    var str = new Array(count+1).join("*");
    node.status({ fill: colour, shape: "ring", text: str });
}

//node.warn(msg.payload);

if(msg.topic == "setpin") {
    context.set("pin", msg.payload);
    context.set("seq", "");
}else if (msg.topic == "clear") {
    context.set("pin", "0000");
}else{ 
    latestNumber = msg.payload;
}
//node.warn("pin:"+pin);
msg.payload = false; //fail by default
//node.warn("input:"+latestNumber);
//node.warn("compare to:"+pin.toString()[seqCorrect]);
//node.warn("seq correct:"+seqCorrect);
if(latestNumber == pin.toString()[seqCorrect]){
 seqCorrect++;
}else if(latestNumber == pin.toString()[0]){
    seqCorrect=1;
}else{
 seqCorrect = 0;
}
if(seqCorrect >= pin.length){
    node.status({ fill: "green", shape: "ring", text: "CORRECT" });
 msg.payload = true;
}else{
    showStars(seqCorrect, "red");
}
context.set("seqCorrect",seqCorrect);
return msg;

```

---

<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: [4 June 2021 12:28 UTC](https://discourse.nodered.org/t/compare-latest-4-messages-against-mysql/46020/4 "2021-06-04T12:28:34Z")

</div>

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