Generate SQL statement from 2 mqtt messages

Good evening gents, I have a few issues that are just getting the better of me and don't seem to figure this out, probably this something small, but I don't seem to get it right.

Hardware: Raspberry Pi 4 / Node Red version : 1.0.6

Issue:1 - Add data to the SQLite dB from 2 different incoming MQTT nodes. I have used the previous code that is confirmed working in the other flow but for some reason or syntax issue, I just don't seem to get it to pass the data to the reliant DB.

// the code that works now modified below for a new table ..
 
var myymd= msg.payload.myymd;
var mytimes= msg.payload.mytimes;
var _msgid= msg.payload._msgid;
var door_id= msg.payload["Door ID"];
var door_otp= msg.payload.OTP;
var hashkey= msg.payload.hashkey;


//create SQL command
msg.topic = `INSERT INTO ACCREQ (
  DATE, TIME, MSGID_ID, DOOR_ID, DOOR_OTP, HASHKEY
) values (
   '${myymd}', '${mytimes}', '${_msgid}', ${door_id}, ${door_otp} ,${hashkey}
);`
return msg;

Issue:2, Now I need to collect the data for the user's table one by one, encrypt them and check if the key matches = if true, then pass 1 or 0

Can some please be of some assistance, it will be greatly appreciated. Thank you in advance

Below is my complete flow of what I would like to achieve ....tonight ( : wink:)

herewith my flow

[{"id":"c7ac4dd1.1d648","type":"tab","label":"New Acc Request","disabled":false,"info":""},{"id":"3fba7a44.43c286","type":"debug","z":"c7ac4dd1.1d648","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":650,"y":160,"wires":[]},{"id":"168fe769.a484b9","type":"mqtt in","z":"c7ac4dd1.1d648","name":"PSA001 - QR Value","topic":"accreq","qos":"0","datatype":"auto","broker":"57c985f0.a39e8c","x":170,"y":160,"wires":[["3fba7a44.43c286","64b4ac9b.9d95a4","38b16044.77229","190917f3.d79a18","a9dd3d9f.c5e95"]]},{"id":"64b4ac9b.9d95a4","type":"ui_text","z":"c7ac4dd1.1d648","group":"6771f7fc.19f008","order":2,"width":26,"height":1,"name":"PSA001 - QR Value Dissplay","label":"PSA001 - DoorID:001 - QR HASH Value : ","format":"{{msg.payload}}","layout":"row-left","x":720,"y":200,"wires":[]},{"id":"9bfc35d3.f29208","type":"rpi-gpio out","z":"c7ac4dd1.1d648","name":"PSA001 INStatus  LED","pin":"33","set":true,"level":"0","freq":"","out":"out","x":1100,"y":120,"wires":[]},{"id":"829ab0fc.2211b","type":"ui_led","z":"c7ac4dd1.1d648","group":"6771f7fc.19f008","order":1,"width":4,"height":1,"label":"Recived ","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"0","valueType":"num"},{"color":"green","value":"1","valueType":"num"}],"allowColorForValueInMessage":false,"name":"","x":1050,"y":80,"wires":[]},{"id":"38b16044.77229","type":"trigger","z":"c7ac4dd1.1d648","op1":"1","op2":"0","op1type":"num","op2type":"str","duration":"5","extend":false,"units":"s","reset":"5","bytopic":"all","name":"","x":660,"y":120,"wires":[["829ab0fc.2211b","9bfc35d3.f29208"]]},{"id":"f73eb69a.3337b8","type":"sqlite","z":"c7ac4dd1.1d648","mydb":"9db34194.66aca","sqlquery":"msg.topic","sql":"","name":"PSA-DB ","x":1540,"y":320,"wires":[["25617dd6.b83862"]]},{"id":"25617dd6.b83862","type":"debug","z":"c7ac4dd1.1d648","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1730,"y":320,"wires":[]},{"id":"10aa3ead.9de511","type":"mqtt in","z":"c7ac4dd1.1d648","name":"PSA001 - OTP","topic":"psa001/d1/user_input ","qos":"0","datatype":"json","broker":"57c985f0.a39e8c","x":120,"y":580,"wires":[["7ed0ff44.ba906"]]},{"id":"190917f3.d79a18","type":"function","z":"c7ac4dd1.1d648","name":"Get Hashkey from mqtt payload","func":"h = msg.payload;\nreturn {topic:\"psa001/d1/qrvalue\", hashkey:h};","outputs":1,"noerr":0,"x":670,"y":340,"wires":[["b41435aa.e49b58"]]},{"id":"7ed0ff44.ba906","type":"simpletime","z":"c7ac4dd1.1d648","name":"Time Stamp ","mydate":false,"myymd":true,"myyear":false,"mymonth":false,"mymonthn":false,"mydom":false,"mydoy":false,"myday":false,"myhourpm":false,"myhour":false,"mytime":false,"mytimes":true,"myminute":false,"myminutes":false,"mysecond":false,"mymillis":false,"myepoch":false,"myrawdate":false,"mypm":false,"x":570,"y":580,"wires":[["b41435aa.e49b58"]]},{"id":"b41435aa.e49b58","type":"join","z":"c7ac4dd1.1d648","name":"Wait and Join ","mode":"custom","build":"merged","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":960,"y":420,"wires":[["d709170b.774078"]]},{"id":"a9dd3d9f.c5e95","type":"delay","z":"c7ac4dd1.1d648","name":"Delay 2sec","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":130,"y":420,"wires":[["c63e22af.c892c"]]},{"id":"cb55e176.c7aa7","type":"change","z":"c7ac4dd1.1d648","name":"","rules":[{"t":"move","p":"payload","pt":"msg","to":"complete","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":630,"y":420,"wires":[["b41435aa.e49b58"]]},{"id":"40ebe0ca.2bbef","type":"debug","z":"c7ac4dd1.1d648","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1530,"y":420,"wires":[]},{"id":"c63e22af.c892c","type":"trigger","z":"c7ac4dd1.1d648","op1":"true","op2":"0","op1type":"bool","op2type":"str","duration":"0","extend":false,"units":"ms","reset":"5","bytopic":"all","name":"Trigger TRUE Value ","x":380,"y":420,"wires":[["cb55e176.c7aa7","1f8956cf.724ea9"]]},{"id":"1f8956cf.724ea9","type":"trigger","z":"c7ac4dd1.1d648","op1":"5","op2":"","op1type":"str","op2type":"str","duration":"0","extend":false,"units":"s","reset":"true ","bytopic":"all","name":"","x":380,"y":500,"wires":[["c63e22af.c892c"]]},{"id":"d709170b.774078","type":"function","z":"c7ac4dd1.1d648","name":"PSA DB - Insert - ACCREQ","func":"var myymd= msg.payload.myymd;\nvar mytimes= msg.payload.mytimes;\nvar _msgid= msg.payload._msgid;\nvar door_id= msg.payload[\"Door ID\"];\nvar door_otp= msg.payload.OTP;\nvar hashkey= msg.payload.hashkey;\n\n\n//create SQL command\nmsg.topic = `INSERT INTO ACCREQ (\n  DATE, TIME, MSGID_ID, DOOR_ID, DOOR_OTP, HASHKEY\n) values (\n   '${myymd}', '${mytimes}', '${_msgid}', ${door_id}, ${door_otp} ,${hashkey}\n);`\nreturn msg;","outputs":1,"noerr":0,"x":1280,"y":420,"wires":[["40ebe0ca.2bbef","ba7bf1bd.b6b0a","999245d.337c3b8","f73eb69a.3337b8"]]},{"id":"ba7bf1bd.b6b0a","type":"function","z":"c7ac4dd1.1d648","name":"Read DB for user 1 ","func":"SELECT * FROM USERS \n \nreturn msg;\n\nthen\n\n// use the result from the db quiry to encrypt one by one with the \"Door_id\"+\"Door_otp\" \n\nvar user_name= msg.payload.user_name;\nvar user_surname= msg.payload.User_surname;\nvar user_pin= msg.payload.user_pin;\nvar user_acc_level= msg.payload.user_acc_level;\nvar user_acc_code= msg.payload.user_acc_code;\n\n\n","outputs":1,"noerr":7,"x":810,"y":680,"wires":[["a728b689.0aca28","6365a6f2.77f0f8"]]},{"id":"a728b689.0aca28","type":"digest","z":"c7ac4dd1.1d648","name":"Encrypt User Data to SHA256","algorithm":"SHA256","x":1130,"y":680,"wires":[["4f8f6e92.4651c"]]},{"id":"999245d.337c3b8","type":"function","z":"c7ac4dd1.1d648","name":"Read DB for ACCREQ Data ","func":"SELECT * FROM USERS \n \nreturn msg;\n\nthen\n\n// use the result from the db quiry to encrypt one by one with the \"Door_id\"+\"Door_otp\" \n\nvar user_name= msg.payload.user_name;\nvar user_surname= msg.payload.User_surname;\nvar user_pin= msg.payload.user_pin;\nvar user_acc_level= msg.payload.user_acc_level;\nvar user_acc_code= msg.payload.user_acc_code;\n\n\n","outputs":1,"noerr":7,"x":780,"y":760,"wires":[["6365a6f2.77f0f8","a728b689.0aca28"]]},{"id":"4f8f6e92.4651c","type":"match","z":"c7ac4dd1.1d648","name":"","rules":[{"property":"payload","propertyType":"msg","type":"cont","value":"payload.hashkey ","valueType":"msg"}],"x":1370,"y":720,"wires":[["2f4c4ef0.861cd2","fb57ee09.6fa6f","848a2e81.568cc","7816a6e6.61ff28"],["2f4c4ef0.861cd2","fb57ee09.6fa6f"]]},{"id":"848a2e81.568cc","type":"mqtt out","z":"c7ac4dd1.1d648","name":"Notify Base Station node ","topic":"server/accreq/result ","qos":"","retain":"","broker":"d8ebb20f.0a884","x":1730,"y":580,"wires":[]},{"id":"2f4c4ef0.861cd2","type":"ui_led","z":"c7ac4dd1.1d648","group":"162eae12.1020b2","order":1,"width":0,"height":0,"label":"Access Status ","labelPlacement":"left","labelAlignment":"left","colorForValue":[{"color":"red","value":"false","valueType":"bool"},{"color":"green","value":"true","valueType":"bool"}],"allowColorForValueInMessage":false,"name":"Access Status ","x":1700,"y":720,"wires":[]},{"id":"fb57ee09.6fa6f","type":"ui_text","z":"c7ac4dd1.1d648","group":"162eae12.1020b2","order":0,"width":0,"height":0,"name":"Granted / Denied ","label":"Access ","format":"{{msg.payload}}","layout":"row-spread","x":1710,"y":800,"wires":[]},{"id":"7816a6e6.61ff28","type":"mqtt out","z":"c7ac4dd1.1d648","name":"Notify Door node ","topic":"server/accreq/result ","qos":"","retain":"","broker":"d8ebb20f.0a884","x":1710,"y":640,"wires":[]},{"id":"6365a6f2.77f0f8","type":"digest","z":"c7ac4dd1.1d648","name":"Encrypt User Data to SHA256","algorithm":"SHA256","x":1130,"y":760,"wires":[["4f8f6e92.4651c"]]},{"id":"57c985f0.a39e8c","type":"mqtt-broker","z":"","name":"PSA Server 1","broker":"localhost ","port":"1883","clientid":"psa003","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"6771f7fc.19f008","type":"ui_group","z":"","name":"Traffic From Psa001","tab":"8d4ff28.725211","order":1,"disp":true,"width":30,"collapse":false},{"id":"9db34194.66aca","type":"sqlitedb","z":"","db":"psa.db","mode":"RWC"},{"id":"d8ebb20f.0a884","type":"mqtt-broker","z":"","name":"PSA Server 1","broker":"10.11.12.101","port":"1880","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"162eae12.1020b2","type":"ui_group","z":"","name":"PSA-VPN / Pass: Psa@123! - Please Scan QR to get access to PSA-QR GEN-SYST ","tab":"24e16ee9.7041c2","order":2,"disp":true,"width":15,"collapse":false},{"id":"8d4ff28.725211","type":"ui_tab","z":"","name":"Traffic","icon":"dashboard","order":3,"disabled":false,"hidden":false},{"id":"24e16ee9.7041c2","type":"ui_tab","z":"","name":"IP ACCESS ","icon":"dashboard","disabled":false,"hidden":false}]

sorry Gents, I have not posted the error, well here it is .

7/6/2020, 8:07:41 PMnode: 40ebe0ca.2bbef
INSERT INTO ACCREQ ( DATE, TIME, MSGID_ID, DOOR_ID, DOOR_OTP, HASHKEY ) values ( 'undefined', 'undefined', 'undefined', 1, 1234 ,undefined ); : msg : Object

At a quick glance, you might be better off storing your values in flow or global context and building the SQL from the stored values.

Do you know how to do that?

Here is a starter...

Node that generates data > function node ...

var key = msg.topic;
var value = msg.payload;
flow.set(key,value);
return msg;

Then after all values have been commited to flow context, you will be able to access them when you build the SQL like this...

//get values from flow context
var door_id = flow.get("the_topic_you_stored_this_value_as");
var door_otp = flow.get("the_topic_you_stored_this_value_as");
//... etc ...

//create SQL command
msg.topic = `INSERT INTO ACCREQ (
  DATE, TIME, MSGID_ID, DOOR_ID, DOOR_OTP, HASHKEY
) values (
   '${myymd}', '${mytimes}', '${_msgid}', ${door_id}, ${door_otp} ,${hashkey}
);`
return msg;

Do that for MSGID_ID, DOOR_ID, DOOR_OTP and HASHKEY values

HI Steve , awesome to here from you again and thank you for your quick response ( your a absolute super star!!!)

Let me digest your response quick ...ps dont go away please :slight_smile:

Can you send me the output of these 2 nodes?

Ok, Let me see if i understood you correctly

1 : I create a new Function (1) to temp store the value's coming in from 2 different locations / nodes in a global or flow context as :wink:

var key = msg.topic;    //what values is this ? from mqtt 1 node ? 
var value = msg.payload;  // values from mqtt node 2 ? 
flow.set(key,value);
return msg;   // this spits out the combined data ? 

then I create/inject into the next function (2) ( INSERT INTO DB / TABLE ) such as :

//get values from flow context
var door_id = flow.get("the_**topic**_you_stored_this_value_as"); why the topic ? 
var door_otp = flow.get("the_**topic**_you_stored_this_value_as"); this come in as the same Topic ? is that right ? 
//... etc ...

//create SQL command
msg.topic = `INSERT INTO ACCREQ (
  DATE, TIME, MSGID_ID, DOOR_ID, DOOR_OTP, HASHKEY
) values (
   '${myymd}', '${mytimes}', '${_msgid}', ${door_id}, ${door_otp} ,${hashkey}
);`
return msg;

Cool, 1 min

I dont think you are understnding the concept.

flow or global is a "bucket" / a store for putting things in / getting them back later (its a memory store of KEY / VALUE pairs).

Your data messages hold the values you want to put into the database - but they arrive from different places at different times - SO - you need somewhere to store them till everything is ready.

Thats why I suggested you store them in flow context.

As the flow context is a bucket of unknowns - you need to store items/values against a KEY.

As MQTT has a topic - use that as the key.

So, when you store any item in context, against a KEY (in your case the topic) you get it back by using the same key that it was stored..

This is much easier to understand once look at the context store in the side-bar

HI Steve , ok here are the two Debug nodes outputs as requested :

The First one / TOP :

7/6/2020, 8:45:38 PMnode: Output from MQTT-PSA001/ QR Value in SHA256 key
accreq : msg : Object
object
topic: "accreq"
payload: "74657ce6651d26d4650b2da3c16127de6017ed409492fcf62a0a48eee27c2667"
qos: 0
retain: false
_msgid: "3aaa90eb.4cda9"

and then the Output values from the bottom debug node

7/6/2020, 8:47:41 PMnode: Output from MQTT-PSA001/ User Data before Hash
psa001/d1/user_input : msg : Object
object
topic: "psa001/d1/user_input "
payload: object
Name : "Pieter"
Surname: "Hattingh"
PIN: 5054
OTP: 1234
Door ID: 1
qos: 0
retain: false
_msgid: "f11a076f.82dd18"

hope its what / or how you wanted it. i have remained the Debug nodes to id the origin
PS, thanks for your help

Thats no good - I need the actual JSON to be able to simulate your flow - please use the copy button from the debug msg

image

OOPS , my bad , sorry ( Rookey mistakes )

Top / First Debug

{"topic":"accreq","payload":"34e1602a9f5695ecc5f8df055951e5c564d30b5e45745f900a928776d19f348a","qos":0,"retain":false,"_msgid":"5d99ac14.906ee4"}

and the second one / Bottom debug

{"topic":"psa001/d1/user_input ","payload":{"Name ":"Pieter","Surname":"Hattingh","PIN":5054,"OTP":1234,"Door ID":1},"qos":0,"retain":false,"_msgid":"f11a076f.82dd18"}
1 Like

One more thing - what fields from the data map to what fields in the databse..

e.g.

top MQTT

  • msg.payload --> field hashkey

bottom MQTT

  • msg.payload.Name --> DB field aaa?
  • msg.payload.Surname --> DB field bbb?
  • msg.payload.PIN --> DB field ccc?
  • OTP --> DB field ddd?
  • Door ID --> DB field eee?

Also, what is the signal to actually TRIGGER the DB insert command? When the bottom MQTT fires?

OK, Let me explain

I have a door node ( Device running a Raspberry pi zero and Node red - link to a wifi router on the sme network ) this device do 2 things ,
1 : User inputs test data in a UI table ( Name , surname, Pin number , Door OTP and Door Id) then this data get digested with the encryption node Sha265 and spits out a hash key , i then generate a QR code from this key . The door node generate a new OTP one every new access attempt / request .

  1. the device reads the QR code and the message is send to the server in 2 messages from MQTT 1 node (topic:psa001/d1/user_data ) and MQTT2 node ( topic:psa001/d1/accreq) .

the server is a Raspberry pi 4 with node red . On the incoming nodes ( MQTT 1 / MQTT 2) the messages must be combined and the following values need to be stored in a exciting sqlite db / ACCREQ table ( Access requests Table )
value 1 = Date ( Day of Access request ) - (generated with a node on entry om server )
value 2 = time ( Hours and Min ) - (generated with a node on entry om server )
value 3 = msg_id ( to track / authenticated a acc req msg later ) ( Generic msg_id from payload)
Value 4 = Door_id ( to check what door was try to gain access from / and or if its valid from user acc level ) ( from MQTT2)
value 5 = Door OTP ( The uniqe 4 digit value the door generates on very access request ) ( From MQTT2)
Value 6 = The HashKey ( The encrypted data from the QR code from MQTT1)

then , i need to some how READ from "USER" table ( Name , Surname , Pin , Acc level , Acc Code )

And add READ From "ACCREQ" table the ( OTP )

Combine results in a singel message then encrypt/digest SHA256 it and

Then match the key generated and the one received for a mate and

IF match, pass a 1 and Not pass a 0

That's the challenge steve. I really hope this explanation helped you understand the end goal I am trying to achieve and I would really appreciate all your advice and help. without you I am no ware

Is this done in one function one , two or 3 ?

So if i understand this...

top MQTT

  • msg.payload --> DB field hashkey

bottom MQTT

  • msg._msg_id --> DB field MSGID_ID?
  • msg.payload["Door ID"] --> DB DOOR_ID?
  • msg.payload.OTP --> DB DOOR_OTP?

Other info...

  • Current Date --> DB field DATE
  • Current Time --> DB field TIME

correct?

Yes , correct - spot on

The objective is the Server must get the same OTP used be for the key was generated and combine the OTP with user data from the user table , encrypt and see if the incoming key just arrived match any of the users , and if true , witch one ? with and true value and user details.

The result...

How i got there...

the flow...

[{"id":"5c5f4de2.d52c84","type":"debug","z":"249dc9ac.f8eff6","name":"To Database","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":890,"y":180,"wires":[]},{"id":"60bcd9e9.848a28","type":"function","z":"249dc9ac.f8eff6","name":"PSA DB - Insert - ACCREQ","func":"var now = (new Date()).toISOString().slice(0,-5);\nvar dateParts = now.split(\"T\");\n\nvar door_id= msg.payload[\"psa001/d1/user_input \"][\"Door ID\"];\nvar door_otp= msg.payload[\"psa001/d1/user_input \"].OTP;\nvar hashkey= msg.payload.accreq;\nvar _msgid= msg._msgid;\nvar date = dateParts[0];\nvar time = dateParts[1];\n\n\n//create SQL command\nmsg.topic = `INSERT INTO ACCREQ (\n  DATE, TIME, MSGID_ID, DOOR_ID, DOOR_OTP, HASHKEY\n) values (\n   '${date}', '${time}', '${_msgid}', ${door_id}, ${door_otp} ,${hashkey}\n);`\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":840,"y":100,"wires":[["5c5f4de2.d52c84"]]},{"id":"a20642ce.b3c99","type":"inject","z":"249dc9ac.f8eff6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":100,"wires":[["cf6472e1.95f0c"]]},{"id":"cf6472e1.95f0c","type":"function","z":"249dc9ac.f8eff6","name":"Fake PSA001 - QR Value","func":"\nreturn {\"topic\":\"accreq\",\"payload\":\"34e1602a9f5695ecc5f8df055951e5c564d30b5e45745f900a928776d19f348a\",\"qos\":0,\"retain\":false,\"_msgid\":\"5d99ac14.906ee4\"};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":290,"y":100,"wires":[["dcc8398d.667e18"]]},{"id":"c906ec8a.39d78","type":"inject","z":"249dc9ac.f8eff6","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":180,"wires":[["27d11c6b.97e964"]]},{"id":"27d11c6b.97e964","type":"function","z":"249dc9ac.f8eff6","name":"Fake PSA001 - OTP","func":"\nreturn {\"topic\":\"psa001/d1/user_input \",\"payload\":{\"Name \":\"Pieter\",\"Surname\":\"Hattingh\",\"PIN\":5054,\"OTP\":1234,\"Door ID\":1},\"qos\":0,\"retain\":false,\"_msgid\":\"f11a076f.82dd18\"};","outputs":1,"noerr":0,"initialize":"","finalize":"","x":280,"y":180,"wires":[["dcc8398d.667e18"]]},{"id":"dcc8398d.667e18","type":"join","z":"249dc9ac.f8eff6","name":"Wait and Join ","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"2","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":540,"y":100,"wires":[["60bcd9e9.848a28","c7f84c0e.c889b"]]},{"id":"c7f84c0e.c889b","type":"debug","z":"249dc9ac.f8eff6","name":"check it","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":560,"y":180,"wires":[]},{"id":"d13e9249.84356","type":"comment","z":"249dc9ac.f8eff6","name":"Fake the MQTT data .............................................","info":"","x":220,"y":60,"wires":[]},{"id":"b8dd0e46.32f84","type":"comment","z":"249dc9ac.f8eff6","name":"Join 2 messages by topic","info":"","x":570,"y":60,"wires":[]},{"id":"6874f64c.48f9f8","type":"comment","z":"249dc9ac.f8eff6","name":"Build SQL","info":"","x":780,"y":60,"wires":[]},{"id":"c6bb7cd8.87409","type":"comment","z":"249dc9ac.f8eff6","name":"Fake the MQTT data .......................................","info":"","x":210,"y":140,"wires":[]}]

EDIT - on the join node...
I set the join node settings to join values based on topic - look at the debug msg called "Check it" to understand whats happening

1 Like

Will do Steve, give me a min to implement this , again Thanks , lets hope for the best

Hey Steve, hope you are still here. sorry for the delay, had to restart the devices and fix some bugs.

I get the following couple of errors

The function node says ...

TypeError: Cannot read property 'Door ID' of undefined

From the Function node debug...

{"topic":"INSERT INTO ACCREQ (\n  DATE, TIME, MSGID_ID, DOOR_ID, DOOR_OTP, HASHKEY\n) values (\n   '2020-07-06', '20:07:46', 'bcf1657a.f29f28', 1, 1234 ,74657ce6651d26d4650b2da3c16127de6017ed409492fcf62a0a48eee27c2667\n);","payload":{"psa001/d1/user_input ":{"Name ":"Pieter","Surname":"Hattingh","PIN":5054,"OTP":1234,"Door ID":1},"accreq":"74657ce6651d26d4650b2da3c16127de6017ed409492fcf62a0a48eee27c2667"},"qos":0,"retain":false,"_msgid":"bcf1657a.f29f28","myymd":"2020-07-06","mytimes":"22:07:20"}

from the SQLITE Note (PSA DB / ACCREQ db table )....

Error: SQLITE_ERROR: unrecognized token: "74657ce6651d26d4650b2da3c16127de6017ed409492fcf62a0a48eee27c2667"