# MQTT-\>(JSON)-\>?-\>InfluxDB-\>Grafana

**URL:** <https://discourse.nodered.org/t/mqtt-json-influxdb-grafana/7108>\
**Category:** General\
**Created:** [21 January 2019 18:45 UTC](https://discourse.nodered.org/t/mqtt-json-influxdb-grafana/7108 "2019-01-21T18:45:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BC84](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@BC84](https://discourse.nodered.org/u/BC84)\
**Post date:** [21 January 2019 18:45 UTC](https://discourse.nodered.org/t/mqtt-json-influxdb-grafana/7108/1 "2019-01-21T18:45:51Z")

</div>

Hi, I'm brand new to NodeRed, MQTT, InfluxDB and Grafana

I'm trying to visualize Data form a ESP8266. Onto the dashboard the data come in. But I've got an issue to transform the MQTT data into an proper format for InfluxDB. NodeRed time series graphs work fine, but I can't visualize the MQTT sensor data in Grafana.

What should I do? A transformation with the json block, and a custom function. But what shoult the function looks like? Everything I tried doesn't work.

---

<div class="post-metadata">

**Author:** ![JayDickson](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jaydickson/32/90592_2.png) [@JayDickson](https://discourse.nodered.org/u/JayDickson)\
**Post date:** [21 January 2019 19:24 UTC](https://discourse.nodered.org/t/mqtt-json-influxdb-grafana/7108/2 "2019-01-21T19:24:57Z")

</div>

I prefer to use the influx batch node over the single influxdb node because it works a bit more efficiently for large quantities of data. It likes data to be an array of objects. Here's an example of the format it likes:

```
var field1Data = 10
var field2Data = 12
var field3Data = 15
var tag1Data = "what is this?"
var tag2Data = "I'm a tag!"

    var payload=(
        {measurement: "measurementName",
            fields: {
                field1: field1Data,
                field2: field2Data,
                field3: field3Data
            },
            tags: {
                tag1: tag1Data,
                tag2: tag2Data
            },
            timestamp: new Date()
        })
    node.send({payload})

```

You would of course want to grab parts of your inbound message to populate your fields and tags as per your use case. This is set up in a way that you can use a join node at the end of the flow to combine a predetermined number of payloads into an array to send through influx batch.

---

<div class="post-metadata">

**Author:** ![Paul-Reed](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/paul-reed/32/66906_2.png) [@Paul-Reed](https://discourse.nodered.org/u/Paul-Reed)\
**Post date:** [21 January 2019 21:22 UTC](https://discourse.nodered.org/t/mqtt-json-influxdb-grafana/7108/3 "2019-01-21T21:22:42Z")

</div>

If the data is minimal, and you want to use the influx single node, this works for me -

```auto
if(msg.node == 18) {
    var temp = (msg[0])/100;
    var batt = (msg[1])/1000;
msg.payload = [{
    temp: temp,
    battery: batt
},
{
    device:"th18"
}];
msg.measurement = "iot";
return msg;
} else {
    return null;
    }

```

 ![emon](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/82235616d8082056359f704ae0a3f4e6d15e423e.png)  
...so, I've got 4 sensors, and the 'if' condition only runs the function if the incoming flow contains `msg.node == 18`  
`device:"th18"` is the tag  
`msg.measurement = "iot"` is the measurement

---

<div class="post-metadata">

**Author:** ![BC84](https://avatars.discourse-cdn.com/v4/letter/b/e99b99/32.png) [@BC84](https://discourse.nodered.org/u/BC84)\
**Post date:** [22 January 2019 18:07 UTC](https://discourse.nodered.org/t/mqtt-json-influxdb-grafana/7108/4 "2019-01-22T18:07:10Z")

</div>

Thanks! Now it's running. I also used the examples of nodeRed itself.
