# Mqtt -\> Node-Red -\> InfluxDB

**URL:** <https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439>\
**Category:** General\
**Created:** [24 April 2020 18:08 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439 "2020-04-24T18:08:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![URBANsUNITED](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/urbansunited/32/20584_2.png) [@URBANsUNITED](https://discourse.nodered.org/u/URBANsUNITED)\
**Post date:** [24 April 2020 18:08 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/1 "2020-04-24T18:08:35Z")

</div>

Hi!

I am trying for hours now to get a solution for my project.  
What I want:

Get the data from my tasmota energy logger into influxdb to use Grafana later.  
All is set up and working except the new Node-Red part.

This is my debug output from "mqtt in node"

```auto
Gas-Wasserzähler/tele/SENSOR : msg.payload : Object

{"Time":"2020-04-24T19:59:42","Gas":{"Count":4624.38},"Wasser":{"Count":771.473},"gas_day":0.87,"gas_month":59.65,"gas_year":1.84,"water_day":0.32,"water_month":12.7,"water_year":0.9}

```

I tried lots of things to get the data using the function node.  
ex.

```auto
var msg7 = {};

msg7.payload = [{"gas_day": msg1.payload, "gas_month": msg2.payload, "gas_year": msg3.payload, "water_day": msg4.payload, "water_month": msg5.payload, "water_year": msg6.payload}];

return [msg1,msg2,msg3,msg4,msg5,msg6,msg7];

```

It's not working. Error: `TypeError: Cannot read property 'gas_day' of undefined`

My screen shot:

 ![Bildschirmfoto 2020-04-24 um 20.04.01](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/c/1c8de1b5026ebbf93048a01491a6eb46d6dfe333.png)

I know, it's a very simple thing, but I don't get it!

Who can help a beginner here please??

Thanks in advance

URBANsUNITED

---

<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:** [24 April 2020 19:04 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/2 "2020-04-24T19:04:08Z")

</div>

> [@URBANsUNITED](#):
>
> It's not working. Error: `TypeError: Cannot read property 'gas_day' of undefined`

where are ms1, msg2 etc coming from? As far as I can see they dont exsit!

* * *

Read [working with messages](https://nodered.org/docs/user-guide/messages) - its worth the 10 minutes & in particular, how to copy the path of a variable from the debug pane

> [@URBANsUNITED](#):
>
> This is my debug output from "mqtt in node"
> 
> ```auto
> Gas-Wasserzähler/tele/SENSOR : msg.payload : Object
> 
> {"Time":"2020-04-24T19:59:42","Gas":{"Count":4624.38},"Wasser":{"Count":771.473},"g
> 
> ```

pay attention to this - "msg.payload : Object"  
its telling you msg.payload is an Object  
therefore you can access `msg.payload.this` & `msg.payload.that`  
e.g. `msg.payload.Time`

Again, cant stress enough - read "working with messages" and you'll fix this in no time.

come back if you get stuck

---

<div class="post-metadata">

**Author:** ![URBANsUNITED](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/urbansunited/32/20584_2.png) [@URBANsUNITED](https://discourse.nodered.org/u/URBANsUNITED)\
**Post date:** [25 April 2020 08:05 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/3 "2020-04-25T08:05:51Z")

</div>

Good morning,

I am so sorry, but I don't get it. 😩

Yes, I understand that I can split that message, what I did  
ex.

 ![Bildschirmfoto 2020-04-25 um 09.52.54](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/4738bc60692cdbb589e0b38e1ec6e08e9d4c1140.png)

And yes, I know how to get the path to these objects  
ex. `payload.Gas.Count`

But I am not able to pass this info to the influxdb.  
It works with the function node?!?  
But how?  
I got two values into my database, but the wrong ones, wrong naming etc....

Gas Count & gas\_month --- Where is the rest????

 ![Bildschirmfoto 2020-04-25 um 10.07.32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff26105d5a34e906ab974270e03ddfc5900cf926.png)  
One of my poor attempts:

```auto
var msg = {};

msg.payload = { payload: msg.payload.Time };

return [msg];

```

I am really no lazy guy, I have HA, homebridge etc. easy fully working. But here I am fully stressed ☹  
Can't you give me an example with one value or two values, please?

Should I use **influxdb out Node** or **influxdb batch node**?

Thanks again

URBANsUNITED

---

<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:** [25 April 2020 08:09 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/4 "2020-04-25T08:09:49Z")

</div>

> [@URBANsUNITED](#):
>
> One of my poor attempts:
> 
> ```auto
> var msg = {}; // OVERWRITE msg to a NEW EMPTY object
> 
> msg.payload = { payload: msg.payload.Time }; //how can you expect to get msg.payload - you overwrote it above ^
> 
> return [msg];
> 
> ```

see comments above to understand this problem

Answer...

```auto
var t = msg.payload.Time
var gasCount = msg.payload.Gas.Count;
msg.payload = { payload: gasCount };
return msg;

```

or...

```auto
var t = msg.payload.Time
var gasCount = msg.payload.Gas.Count;
node.send({ topic: "time", payload: t});
node.send({ topic: "gas/count", payload: gasCount });
return null;// already sent

```

---

<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:** [25 April 2020 08:12 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/5 "2020-04-25T08:12:09Z")

</div>

> [@URBANsUNITED](#):
>
> Should I use **influxdb out Node** or **influxdb batch node**?

Sorry, no idea - not using infux myself.

I can assist you with coding errors.

If you provide expected format of the msg (that influx node expects) & the data you wish to send, I can certainly help you get correct format.

---

<div class="post-metadata">

**Author:** ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)\
**Post date:** [25 April 2020 12:17 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/6 "2020-04-25T12:17:30Z")

</div>

> [@URBANsUNITED](#):
>
> Yes, I understand that I can split that message

Rather than splitting the message, have you thought about accessing the parts directly? Once split, each of those is a different message, and if you insert those parts into influx, they get inserted as different measurements, rather than one single measurement. Again, go back to the link Steve gave you, working with messages. From the original message you get out of MQTT, you have an object with the following payload:

```auto
{
  "Time": "2020-04-24T19:59:42",
  "Gas": {
    "Count": 4624.38
  },
  "Wasser": {
    "Count": 771.473
  },
  "gas_day": 0.87,
  "gas_month": 59.65,
  "gas_year": 1.84,
  "water_day": 0.32,
  "water_month": 12.7,
  "water_year": 0.9
}

```

From a function node directly behind that mqtt-in node, you can access the values as `msg.payload.gas_day`, `msg.payload.gas_month`, and so on. To get the object you have described in `msg7.payload` in your original post, you can try the following:

```auto
msg.payload = [
    {
        "gas_day": msg.payload.gas_day, 
        "gas_month": msg.payload.gas_month, 
        "gas_year": msg.payload.gas_year, 
        "water_day": msg.payload.water_day, 
        "water_month": msg.payload.water_month, 
        "water_year": msg.payload.water_year
     }
];
return msg;

```

![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5681bec02d178e0f7f91839c69d3573fa0a617c.png)

What's going wrong in the above image is that once you've split the object, the keys turn into topics, and the payload differs per type. For the `Time` key, your payload is a string. For `Gas`, it's an object, `gas_year` for example, is a number. If you did split it, then pass all those messages to a function node and have it operate on `msg.payload.???`, the contents of msg.payload, and especially the type of each payload, is different.

Can you explain what the format is that you need?

> [@Steve-Mcl](#):
>
> see comments above to understand this problem
> 
> Answer...
> 
> ```auto
> var t = msg.payload.Time
> var gasCount = msg.payload.Gas.Count;
> msg.payload = { payload: gasCount };
> return msg;
> 
> ```
> 
> or...
> 
> ```auto
> var t = msg.payload.Time
> var gasCount = msg.payload.Gas.Count;
> node.send({ topic: "time", payload: t});
> node.send({ topic: "gas/count", payload: gasCount });
> return null;// already sent
> 
> ```

What is for your use case the format of your messages that you need? See the influx node's builtin documentation for a description, and work it out for your use case. If you pass us back that information, we can give more detailed information on how to continue.

Edit: this topic is worth a read too, and might allow you to figure it out as well: [Help needed with MQTT to influx](https://discourse.nodered.org/t/help-needed-with-mqtt-to-influx/25421)

---

<div class="post-metadata">

**Author:** ![URBANsUNITED](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/urbansunited/32/20584_2.png) [@URBANsUNITED](https://discourse.nodered.org/u/URBANsUNITED)\
**Post date:** [25 April 2020 15:10 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/7 "2020-04-25T15:10:58Z")

</div>

> [@afelix](#):
>
> msg.payload = [{ "gas\_day": msg.payload.gas\_day, "gas\_month": msg.payload.gas\_month, "gas\_year": msg.payload.gas\_year, "water\_day": msg.payload.water\_day, "water\_month": msg.payload.water\_month, "water\_year": msg.payload.water\_year }]; return msg;

THANKS afelix!!!  
That was excactly what I wanted!!!

I knew that it was easy, but so easy... Great  
With the function code I was able to get all data into influxDB and than to Grafana.  
This is how it looks now.

 ![Bildschirmfoto 2020-04-25 um 17.34.41](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/2/b2f635cc2eab064c1223055f846abdcca9b6aee8.png)

PERFECT!!!

Also many thanks to @Steve-Mcl for explaining me a different my and pointing in the correct direction.  
I will read and learn more the next weeks to make all better and better.

Many thanks again

URBANsUNITED

---

<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:** [9 May 2020 15:11 UTC](https://discourse.nodered.org/t/mqtt-node-red-influxdb/25439/8 "2020-05-09T15:11:02Z")

</div>

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