# Mqtt #-message split and write to database

**URL:** <https://discourse.nodered.org/t/mqtt-message-split-and-write-to-database/65130>\
**Category:** General\
**Tags:** function-node, mqtt\
**Created:** [15 July 2022 08:41 UTC](https://discourse.nodered.org/t/mqtt-message-split-and-write-to-database/65130 "2022-07-15T08:41:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mssib28](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@mssib28](https://discourse.nodered.org/u/mssib28)\
**Post date:** [15 July 2022 08:41 UTC](https://discourse.nodered.org/t/mqtt-message-split-and-write-to-database/65130/1 "2022-07-15T08:41:18Z")

</div>

Hi together,

I would like to get data via MQTT to Node-Red.  
I use the Node 'mqtt in' with the topic 'Speicher/t\_day\_min/#'.  
Via debug node I get  
**15.7.2022, 10:36:04[node: DB](http://192.168.178.100:1880/#)**  
**Speicher/t\_day\_min/Wert : msg.payload : string[6]**  
**" 30.7"**  
and  
same for date/time.  
Now I don't know how to get the 2 values splited so that I can "Insert ... into ..."  
How do I get for e. g.  
let temp = variable1;  
let datetime\_var = variable2;

Thank you very much in advance.  
Greetings  
Martin

---

<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:** [15 July 2022 08:47 UTC](https://discourse.nodered.org/t/mqtt-message-split-and-write-to-database/65130/2 "2022-07-15T08:47:29Z")

</div>

I assume the 2 values arrive at separate times & have different topics?

If so, then you need to join them together into 1 msg so that you can access them both at the same time.

Then you can use them as parameters for your SQL INSERT (or if you enjoy taking risks you can concatenate them into a SQL INSERT string)

### How to join separate messages into 1...

See [this article in the cookbook](https://cookbook.nodered.org/basic/join-streams) for an example of how to join messages into one object.

---

<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:** [15 July 2022 15:11 UTC](https://discourse.nodered.org/t/mqtt-message-split-and-write-to-database/65130/3 "2022-07-15T15:11:53Z")

</div>

If you have control of the mqtt messages, and the timestamp is not the current time, then, if possible, put the time and value in one mqtt message. If it _is_ the current time then you don't need to get it via MQTT.

---

<div class="post-metadata">

**Author:** ![mssib28](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@mssib28](https://discourse.nodered.org/u/mssib28)\
**Post date:** [19 July 2022 20:35 UTC](https://discourse.nodered.org/t/mqtt-message-split-and-write-to-database/65130/4 "2022-07-19T20:35:29Z")

</div>

Hi Steve-Mci and Colin,

I use now the measured value from BME280-Esp8266 via MQTT to a function-node in Node-Red.  
In Node-Red I store the coresponding datetime.  
I have also a trigger, when day changes. When day changes I write the min. and max. values of the temperature and datetime into MYSQL.

It works fine so far.  
Thank you both for your ideas / input.

Kind regards

Martin

---

<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:** [17 September 2022 20:35 UTC](https://discourse.nodered.org/t/mqtt-message-split-and-write-to-database/65130/5 "2022-09-17T20:35:50Z")

</div>

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