# Msg.payload to number for InfluxDB for Grafana

**URL:** https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164
**Category:** General
**Created:** [25 March 2021 06:23 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164 "2021-03-25T06:23:49Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![kelvins](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kelvins](https://discourse.nodered.org/u/kelvins)
#### Post date: [25 March 2021 06:23 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/1 "2021-03-25T06:23:49Z")

</div>

Hi all, I am struggling to convert the msg.payload to be used as a number when writing to InfluxDB.

I currently send data to MQTT and my payload/content is just numbers, example 65  
and under the 'change node' I have set msg.payload defaults.

It writes to InfluxDB well, however, when reading the data from Grafana, it requires it to be a number for more details.

Is there something I need to define in the 'change node'? Thanks in advance!

---

<div class="post-metadata">

### Author: ![mtoko](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mtoko/32/138_2.png) [@mtoko](https://discourse.nodered.org/u/mtoko)
#### Post date: [25 March 2021 08:25 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/2 "2021-03-25T08:25:06Z")

</div>

When I use InfluxDB the node is expecting an object. If there is no timestamp in the object it uses the time that the message was sent

Example function in function node

```auto
msg.payload = {
    name: "Temp1",
    DegC: msg.payload,
};
return msg;

```

---

<div class="post-metadata">

### Author: ![Simon01011](https://avatars.discourse-cdn.com/v4/letter/s/e68b1a/32.png) [@Simon01011](https://discourse.nodered.org/u/Simon01011)
#### Post date: [25 March 2021 08:30 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/3 "2021-03-25T08:30:01Z")

</div>

Welcome kelvins!

I did a test flow some time ago, looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/e/1ed3595870f5094347d5dc376431507d71a1d911.png)  
It queries the network interface and writes the bytes transmitted and received in a InfluxDB. I use version 2.0  
The function for rx looks like:

```auto
var stdout = msg.payload;
var regex_rx = /\s*RX packets\s*\d*\s*bytes\s*(\d*)/;
const m_rx = stdout.match(regex_rx);    
msg.payload = Number(m_rx[1]);
return msg;

```

This payload is then sent to the database.  
The configuration of the Influx node is  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/e/ee97060c22ea970b024ac00c94db04aabf10ead5.png)

---

<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: [25 March 2021 10:20 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/4 "2021-03-25T10:20:14Z")

</div>

Feed the message from the MQTT node into a debug node and post what it shows.

If it is just a number but as a string, so for example the debug may show msg.payload as String "12.3" then if in the MQTT node you tell it to output a Parsed JSON object then it will parse that string and convert it to a number. Then the debug node should show it as a number.

---

<div class="post-metadata">

### Author: ![kelvins](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kelvins](https://discourse.nodered.org/u/kelvins)
#### Post date: [25 March 2021 13:55 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/5 "2021-03-25T13:55:05Z")

</div>

Thank you for your input!

---

<div class="post-metadata">

### Author: ![kelvins](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kelvins](https://discourse.nodered.org/u/kelvins)
#### Post date: [25 March 2021 13:55 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/6 "2021-03-25T13:55:44Z")

</div>

Thank you for your input and clear example!

---

<div class="post-metadata">

### Author: ![kelvins](https://avatars.discourse-cdn.com/v4/letter/k/f19dbf/32.png) [@kelvins](https://discourse.nodered.org/u/kelvins)
#### Post date: [25 March 2021 13:56 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/7 "2021-03-25T13:56:14Z")

</div>

Colin! This was it! Thank you so much! This worked for me!!

---

<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: [25 March 2021 13:58 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/8 "2021-03-25T13:58:13Z")

</div>

Excellent. It works because a string containing a number is valid JSON, so the node parses it and converts it into a javascript object, which in this case is just a number.

---

<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: [8 April 2021 13:59 UTC](https://discourse.nodered.org/t/msg-payload-to-number-for-influxdb-for-grafana/43164/9 "2021-04-08T13:59:12Z")

</div>

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