# MQTT + Sqlite? , no luck

**URL:** <https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487>\
**Category:** General\
**Created:** [16 February 2022 19:35 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487 "2022-02-16T19:35:53Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bar4593](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@bar4593](https://discourse.nodered.org/u/bar4593)\
**Post date:** [16 February 2022 19:35 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487/1 "2022-02-16T19:35:53Z")

</div>

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:

```auto
[
    {
        "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?

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 February 2022 19:51 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487/2 "2022-02-16T19:51:06Z")

</div>

So how are you passing the msg on from the function node?  
(hint: [Writing Functions : Node-RED](https://nodered.org/docs/user-guide/writing-functions))

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 February 2022 20:52 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487/3 "2022-02-16T20:52:07Z")

</div>

Add a debug node showing what is generated by the debug node and another showing what is generated by the function. They must be different. If you cannot see it then post the debug output from both here.

---

<div class="post-metadata">

**Author:** ![bar4593](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@bar4593](https://discourse.nodered.org/u/bar4593)\
**Post date:** [17 February 2022 04:51 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487/4 "2022-02-17T04:51:22Z")

</div>

Yep. Funtion get the data from the mqtt in, then passes it as a msg.topic to sqlite node. Ill give more info later on as I am away from my pc righr now.

---

<div class="post-metadata">

**Author:** ![bar4593](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@bar4593](https://discourse.nodered.org/u/bar4593)\
**Post date:** [17 February 2022 04:52 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487/5 "2022-02-17T04:52:35Z")

</div>

I added one. No output to the debug node. Ill provide more details later, when I'll be next to my pc.

---

<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:** [17 February 2022 08:18 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487/6 "2022-02-17T08:18:35Z")

</div>

Does it? @zenofmud gave you a massive hint. You did not pick it up, so here I will say it clearly. You Have not returned your function.  
eg

```auto
[{"id":"36d09e7e.f733da","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":800,"wires":[["86c0438e.f8b3f8","dbca1102.4df588"]]},{"id":"86c0438e.f8b3f8","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = \"test\"","outputs":1,"noerr":0,"initialize":"","finalize":"","x":340,"y":820,"wires":[["314634da.6b51fc"]]},{"id":"dbca1102.4df588","type":"function","z":"bf9e1e33.030598","name":"","func":"msg.payload = \"test2\"\nreturn msg","outputs":1,"noerr":0,"initialize":"","finalize":"","x":330,"y":880,"wires":[["aa72e434.9d2a8"]]},{"id":"314634da.6b51fc","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":540,"y":820,"wires":[]},{"id":"aa72e434.9d2a8","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":880,"wires":[]}]

```

One function passes on it's message, the other does not.

---

<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:** [18 April 2022 08:18 UTC](https://discourse.nodered.org/t/mqtt-sqlite-no-luck/58487/7 "2022-04-18T08:18:54Z")

</div>

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