# MQTT to InfluxDB function

**URL:** <https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617>\
**Category:** General\
**Created:** [23 January 2021 17:14 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617 "2021-01-23T17:14:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [23 January 2021 17:14 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/1 "2021-01-23T17:14:29Z")

</div>

I just about went nuts trying to get the right incantation of a Regex for JavaScript to do a simple job of parsing out the device name from a MQTT **String** so I decided to share because I didn't find it here.

I've used Regex infrequently for decades but every damn language/system has its own take on what is a special character, quote or not, single or double quote if used...

The incoming **string** looks like this

`{"topic":"System_Info/cam-gaz/bitrate","payload":"5.5","qos":1,"retain":false,"_topic":"System_Info/cam-gaz/bitrate","_msgid":"b220fd5c.db4f9"}`

And the InfluxDB node needs this

`{"device":"cam-gaz","value":5.5,"units":"Mb/s"}`

To get the right info in the right form I use this function

```auto
var device = msg.topic.match("System_Info/([-0-9A-z]*)/");
var rate = parseFloat(msg.payload);

var newMsg = { "payload": {
        "device": device[1],
        "value": rate,
        "units": "Mb/s"
    } };
return newMsg;

```

I emphasize **string** because I got easier to parse input than with the JSON Object option in the MQTT node.

---

<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:** [23 January 2021 17:52 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/2 "2021-01-23T17:52:05Z")

</div>

Hi

I skipped over your topic as i dont know influx. Had you said "help splitting value from topic to make new msg" - it would have been open to a wider audience than "MQTT to InfuxDB"

Anyhoo, here is how I would do it...

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/762602f1f146938d5e6bbd167464c750d299ee1b.png)

```auto
var device = msg.topic.split("/")
var rate = parseFloat(msg.payload);
msg.payload = {
    "device": device[1],
    "value": rate,
    "units": "Mb/s"
}
return msg;

```

Points of interest...

1. I recommend you dont create new messages - always better to modify and pass on the original msg.
2. Split is way easier than regex

---

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [23 January 2021 19:21 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/3 "2021-01-23T19:21:45Z")

</div>

> [@Steve-Mcl](#):
>
> I skipped over your topic as i dont know influx. Had you said "help splitting value from topic to make new msg" - it would have been open to a wider audience than "MQTT to InfuxDB"

And yet here you are posting to it 😉

Split! Damn. Rookie error. Thanks for that.

---

<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:** [23 January 2021 19:22 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/4 "2021-01-23T19:22:55Z")

</div>

> [@LosinIt](#):
>
> And yet here you are posting to it

😂

I think you understand my point.

---

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [23 January 2021 19:32 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/5 "2021-01-23T19:32:18Z")

</div>

Licking my wounds... at least there's an example here like I couldn't find and _some_ strings will _need_ a regex.

---

<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:** [23 January 2021 21:18 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/6 "2021-01-23T21:18:13Z")

</div>

> [@LosinIt](#):
>
> I emphasize **string** because I got easier to parse input than with the JSON Object option in the MQTT node.

I have to point out that the terms are mixed up there. The string _is_ JSON. JSON is by definition a string. When you set the MQTT node to output parsed JSON that means that it parses the JSON string and outputs the result, which is a javascript object.

---

<div class="post-metadata">

**Author:** ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)\
**Post date:** [23 January 2021 21:27 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/7 "2021-01-23T21:27:17Z")

</div>

Uh...yeah, yeah...that's what I meant. 🙄

---

<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:** [24 March 2021 21:28 UTC](https://discourse.nodered.org/t/mqtt-to-influxdb-function/39617/8 "2021-03-24T21:28:12Z")

</div>

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