# Inporting data into InfluxDB database

**URL:** <https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310>\
**Category:** General\
**Tags:** database\
**Created:** [8 March 2023 16:14 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310 "2023-03-08T16:14:24Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 16:14 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/1 "2023-03-08T16:14:24Z")

</div>

Hi everyone. Im new to IOT and node-red and everything. Im currently working on my final thesis and one part of the thesis is monitoring the temperature and humidity in labs at university and storing that data in Influx.  
I use M5Stickc Plus as a mqtt data publisher. The data comes to node-red like this:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/5/455d36fac168faedc5e776f7ac10734b0b321477.png)  
"izba"=room where is the sensor located  
"23.97192"= temperature  
I then used split function to split that message into 2 separate messages. As far as i know, i need with change function change that data into field and tags. But i cant figure it out how to change that data and write it into database.  
i know its probably really easy step, but i spent a lot of time on this and im lost

---

<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:** [8 March 2023 16:38 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/2 "2023-03-08T16:38:28Z")

</div>

Welcome to the forum @danco

Do you have control over the data coming in? If you have it would be better if the data were a json string something like  
`{"izba": 23.7192}`  
or even better  
`{"room": "izba", "temperature": 23.7192}`

Sticking with what you have for the moment though, you don't want to split it into two messages. Keep it in one message.

Have a look at the help text for the influx out node and see if you can work out how the message you send to it needs to be structured. I presume that you want a tag called "room" and a field called "temperature".

Have you watched the recommended playlist for newcomers? [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

Pay particular attention to the section Working with Messages which will give you ideas on how to restructure the data.

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 16:48 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/3 "2023-03-08T16:48:37Z")

</div>

Thanks for your quick answer Colin!  
I dont know if you are familiar with uiflow or M5Flow, but this is how looks my mqtt output.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/4/d430c854a41efa71c6a3aa2884eac0a4dd2b3f39.png)  
I probably cant add "temperature" in front of measurement.  
I watched few of the videos, but im going to watch them all now.

---

<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:** [8 March 2023 16:53 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/4 "2023-03-08T16:53:05Z")

</div>

No I am not familiar with that.  
If you want humidity and pressure too can you get them all in one MQTT message? That would make it much easier as I imagine you want them all together in the db.

---

<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:** [8 March 2023 17:04 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/5 "2023-03-08T17:04:13Z")

</div>

if you construct your M5flow msg section like below, you could send all 3 reading in a JSON string  
`{"room":"izba", "temperature": ` + `get env3_0 temperature` +  
`, "humidity": ` + `get env3_0 humidity` + `, "pressure": ` +  
`get env3_0 pressure` + `}`

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 17:10 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/6 "2023-03-08T17:10:41Z")

</div>

Do i need to construct it via Python or can be this done in Blockly too ?

---

<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:** [8 March 2023 17:12 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/7 "2023-03-08T17:12:44Z")

</div>

Ahh that's Blocky. confused by name in text above image.

You should be able to do it in blocky then, but best place would be where ever published from.

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 17:23 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/8 "2023-03-08T17:23:32Z")

</div>

This is the best what i was able to create.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/b/fb318dc0b1d1f66b12467f281fb6ca069a56c5b4.png)  
If its not ok, it would be ok for me to have 3 different databases in influx for each measurement.  
I just dont know how to change the message to the format i need for influx.

---

<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:** [8 March 2023 17:25 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/9 "2023-03-08T17:25:44Z")

</div>

My example had `{` at beginning and a `}` at end and you replace or removed the commas.

Best to learn to do things correctly, @Colin advise about putting all 3 readings together, is good advise.  
ps. It's easier to split the reading if needed, rather than trying to join them later.

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 17:34 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/11 "2023-03-08T17:34:19Z")

</div>

I dont know if you have experience with m5flow or blockly, its really hard to send it in one message 😃

---

<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:** [8 March 2023 17:35 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/12 "2023-03-08T17:35:36Z")

</div>

You have it in one message, you just need to put commas between the individual components, and if possible add {} round it.  
Having done that the MQTT node will automatically generate a javascript object, and the rest is easy.

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 17:36 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/13 "2023-03-08T17:36:43Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/2/021f192ddb302d08eb941bcba2e7901a20fe99ab.png)  
is this right ?

---

<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:** [8 March 2023 17:38 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/14 "2023-03-08T17:38:22Z")

</div>

Looks ok. Is that coming from an MQTT node? if so then tell it to automatically detect JSON. If not then feed it through a JSON node.

---

<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:** [8 March 2023 17:40 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/15 "2023-03-08T17:40:19Z")

</div>

I have seen them . You just sent it in one message, just missing important `{,}` missing.

Once you have the input correct, here are some recent topics that show how the data should look [Search results for 'sending json data influxdb order:latest\_topic' - Node-RED Forum](https://discourse.nodered.org/search?expanded=true&q=sending%20json%20data%20influxdb%20order%3Alatest_topic) .  
There should also be examples in the help text and flow examples in the import library.  
[edi] The new image looks correct.  
You coould also get blocky to construct the message in the correct format for inflxdb if so inclined.

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 17:42 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/16 "2023-03-08T17:42:48Z")

</div>

Yes, it comes from MQTT node. I changed the output to JSON.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/c/9c224ed80a4a8e153fbca29887521da88800c560.png)

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 17:46 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/17 "2023-03-08T17:46:28Z")

</div>

it was seriously this simple ?  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/c/ccf9bb3198af23c43418a55a4051284f4523a8f6.png)

---

<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:** [8 March 2023 17:53 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/18 "2023-03-08T17:53:46Z")

</div>

~~Did you tag the room?~~  
scratch that i'm blind

---

<div class="post-metadata">

**Author:** ![danco](https://avatars.discourse-cdn.com/v4/letter/d/97f17d/32.png) [@danco](https://discourse.nodered.org/u/danco)\
**Post date:** [8 March 2023 17:56 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/19 "2023-03-08T17:56:25Z")

</div>

Tag for a room is "izba". Its in my native language. Im just testing everything, in the future there will be tag with the number of the room at university.

---

<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:** [8 March 2023 20:27 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/20 "2023-03-08T20:27:15Z")

</div>

In the help text for the node it says  
If **msg.payload** is an array containing two objects, the first object will be written as the set of named fields, the second is the set of named tags.

So if you want the room as a tag and the rest as fields, which I guess you do, then in a function node you can do

```auto
msg.payload = [
  {
    Temperature: msg.payload.Temperature,
    Humidity: msg.payload.Humidity,
    Pressure: msg.payload.Pressure
  },
  {
    room: msg.payload.room
  }
}

```

---

<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:** [7 May 2023 20:27 UTC](https://discourse.nodered.org/t/inporting-data-into-influxdb-database/76310/21 "2023-05-07T20:27:25Z")

</div>

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