I am trying to send MQTT sensor data to an SQL db. It works when I use the inject node but nothing happens when I use it with function node that takes data from the MQTT In node. I use the following code for the function, that take the current time and the sensor msg.payload.
var sqliteTimeStamp = Math.round(Date.now() / 1000); //converting to seconds instead of milliseconds epoch
msg.topic = "INSERT INTO heatindex (time, value) VALUES ('" + sqliteTimeStamp + "', '" + msg.payload + "');";
The flow:
[
{
"id": "7dfb81cf2d764495",
"type": "mqtt in",
"z": "8154c9b075d69e4f",
"name": "",
"topic": "upperFloor/realTemp",
"qos": "2",
"datatype": "auto",
"broker": "15ad3e48f1d54338",
"nl": false,
"rap": true,
"rh": 0,
"inputs": 0,
"x": 610,
"y": 260,
"wires": [
[
"a0c67f9b30120712"
]
]
},
{
"id": "a0c67f9b30120712",
"type": "function",
"z": "8154c9b075d69e4f",
"name": "",
"func": "var sqliteTimeStamp = Math.round(Date.now() / 1000); //converting to seconds instead of milliseconds epoch\nmsg.topic = \"INSERT INTO heatindex (time, value) VALUES ('\" + sqliteTimeStamp + \"', '\" + msg.payload + \"');\";",
"outputs": 1,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 980,
"y": 240,
"wires": [
[
"e93f7022226fc3f2",
"aa5c3967ce2a95c5"
]
]
},
{
"id": "aa5c3967ce2a95c5",
"type": "debug",
"z": "8154c9b075d69e4f",
"name": "function debug",
"active": true,
"tosidebar": true,
"console": false,
"tostatus": false,
"complete": "payload",
"targetType": "msg",
"statusVal": "",
"statusType": "auto",
"x": 1200,
"y": 320,
"wires": []
},
{
"id": "e93f7022226fc3f2",
"type": "sqlite",
"z": "8154c9b075d69e4f",
"mydb": "5c4597463672354d",
"sqlquery": "msg.topic",
"sql": "",
"name": "",
"x": 1430,
"y": 240,
"wires": [
[]
]
},
{
"id": "15ad3e48f1d54338",
"type": "mqtt-broker",
"name": "",
"broker": "localhost",
"port": "1883",
"clientid": "",
"autoConnect": true,
"usetls": false,
"protocolVersion": "5",
"keepalive": "60",
"cleansession": true,
"birthTopic": "",
"birthQos": "0",
"birthPayload": "",
"birthMsg": {},
"closeTopic": "",
"closeQos": "0",
"closePayload": "",
"closeMsg": {},
"willTopic": "",
"willQos": "0",
"willPayload": "",
"willMsg": {},
"sessionExpiry": ""
},
{
"id": "5c4597463672354d",
"type": "sqlitedb",
"db": "/home/pi/Documents/NodeRedLogs/upperFloor.db",
"mode": "RWC"
}
]
Any ideas?