# Store data in InfluxDB show in Chronograf

**URL:** <https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339>\
**Category:** General\
**Tags:** database\
**Created:** [19 July 2022 21:39 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339 "2022-07-19T21:39:20Z")\
**Posts on this page:** 15\
**Page:** 2

<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 July 2022 18:10 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/21 "2022-07-25T18:10:49Z")

</div>

Look again at what i said you should use in the function.

---

<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 July 2022 20:12 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/22 "2022-07-25T20:12:02Z")

</div>

> [@maru](#):
>
> Do you have a function to get the right format for Influxdb?

I posted the function you need, except for the measurement. You have added the measurement and not set the payload as I showed.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [25 July 2022 20:17 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/23 "2022-07-25T20:17:18Z")

</div>

> [@maru](#):
>
> Do you have a function to get the right format for Influxdb?

I pass all of my outputs to a single connection via link nodes. In front of the output, I have this in a function node:

```auto
/**
 * Validate input suitable for the influxdb-out node.
 * We will ALWAYS use the "array containing two objects" payload
 * OR the singe Object payload (if no tags being used).
 * See the Description tab for more details.
 */

// check measurement field is set - if not exit with error
if ( ! msg.measurement ) {
    node.error('msg.measurement is missing')
    node.send([null,msg])
    return
}

let fields,tags

// if payload is an object, assume that it contains fieldName:fieldValue pairs
if ( msg.payload!== null && msg.payload.constructor.name === 'Object' ) {
    fields = msg.payload
} else if ( msg.payload!== null && msg.payload.constructor.name === 'Array' ) {
    node.error('msg.payload cannot be an array. It must be an object containing fieldName:fieldValue pairs or a single value (which would be written to the `value` field name).')
    node.send([null,msg])
    return
} else {
    // Otherwise, we always use 'value' as the default field name
    fields = {'value': msg.payload}
}

const lstFields = Object.keys(fields)

// check to make sure that there is a value field name - if not, continue but with a warning
if ( ! lstFields.includes('value') ) {
    // Lets us turn off the warning if we know what we are doing :-)
    if ( msg.noValueField !== true )
        node.warn('Default field name "vaue" not present, was that deliberate? Set msg.noValueField=true or use the `value` field name to avoid this msg')
}

// check to make sure that all field values are numeric - if not, exit with a warning
let allNumeric = true
lstFields.forEach( key => {
    // I use On/Off for simple switch values in MQTT but these are not useful
    // in InfluxDB, so translate them to ON=1 and OFF=0 (ignoring case).
    try {
        if ( fields[key].toLowerCase() === 'on' ) fields[key] = 1
        if ( fields[key].toLowerCase() === 'off' ) fields[key] = 0
    } catch (e) {}

    // then check to make sure the field is actually a number
    if ( parseFloat(fields[key]) !== fields[key] ) {
        node.error(`Field msg.payload.${key} is not numeric. Only use numbers for field values, text should go in tags.`)
        allNumeric = false
        node.send([null,msg])
        return
    }
})
if ( allNumeric === false ) {
    return
}

// check to make sure that if msg.tags is present, it is an object - if not, exit with a warning
if ( msg.tags ) {
    if ( !(msg.tags!== null && msg.tags.constructor.name === 'Object') ) {
        node.error('msg.tags is not an object - it must contain tagName:tagValue pairs')
        node.send([null,msg])
        return
    }
    tags = msg.tags
}

// Format the output to go to the InfluxDB out node
if ( msg.tags ) {
    msg.payload = [
        fields,
        tags,
    ]
} else {
    msg.payload = fields
}

return msg

```

It makes sure that I only pass valid data.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/a/8af5dab83e164ae3387f0518e4f9ac9ea82fedab.png)

And here is a function I use to convert from MQTT data:

```auto
const splitTopic = msg.topic.split('/')

let location = splitTopic[2]

let type = splitTopic[1]
if ( splitTopic[3] === 'switch' ) type = splitTopic[3]
if ( splitTopic[3] === 'temperature_measured' ) type = 'temperature_trv'

if ( splitTopic[2] )

try {
    msg.payload = JSON.parse(msg.payload)
} catch (e) {
    // node.warn(`Failed to parse incoming MQTT payload. "${msg.topic}", ${e.message}`)
    // return
}

msg.measurement = 'environment'

msg.tags = {
    'location': location,
    'type': type,
}

return msg;

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c427063f85f02beb00dca8b9c371cc8762586921.png)

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [25 July 2022 21:18 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/24 "2022-07-25T21:18:08Z")

</div>

> [@Colin](#):
>
> ```auto
> msg.payload = 
> {
> temp: msg.payload.air_temperature.value
> hum: msg.payload.air_humidity.value
> }
> return msg
> 
> ```

Yes, correct I see the difference now 🙂

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/3/c341b51ce71a73150d901838f9ed012f7f1c8de7.png)

But when iI feed it in Influxdb I get this error:  
No measurement specified

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [25 July 2022 22:02 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/25 "2022-07-25T22:02:08Z")

</div>

👍Thank you for sharing this information. Nice to see how you set this up. I definitely want to try to apply this to my flow too

---

<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:** [26 July 2022 05:49 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/26 "2022-07-26T05:49:40Z")

</div>

> [@maru](#):
>
> No measurement specified

That is because you have taken the measurement seeing out again. It is important that you actually read and understand posts made to try to help you. If you won't read them carefully, and ask if you do not understand, then there is little point trying to help you.

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [26 July 2022 08:23 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/27 "2022-07-26T08:23:43Z")

</div>

But that was why I asked in previous post " _I have trouble with "measurement". Where and how should it come in the function code?"_:

Anyway, I see that I don't need a measurement in the code cause I can use the measurement entry in the Influxdb node and then there are no errors and I get two fields Temp en Hum in the database 👍

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/a/ba275b1e996b6e9054af57c3460abe613f672a47.png)

But I would also like to know how it should be solved in the code

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [26 July 2022 09:59 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/28 "2022-07-26T09:59:09Z")

</div>

> [@maru](#):
>
> But I would also like to know how it should be solved in the code

My shared code shows you all of that.

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [26 July 2022 13:26 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/29 "2022-07-26T13:26:14Z")

</div>

Yes, I see it in the code. msg. measurement = 'environment'...and I used double quotes 👍

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [27 July 2022 09:47 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/30 "2022-07-27T09:47:24Z")

</div>

I have connected your flow's converter and validate to mine. The payload of my flow is extracted from the MQTT node \> converted from base64 \> split.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/f/bf47da65008df36709cf23facee13f9d1223049e.png)

The converter and validator seem to work, but:

- how can I change location and type?
- why the timeout error?

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/e/8e7b60530f246e752734c932f0d211d840eb0d94.png)

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 July 2022 10:44 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/31 "2022-07-27T10:44:07Z")

</div>

> [@maru](#):
>
> how can I change location and type?

They are just tags. Change them to whatever you want. location and type are tag names. you don't have to use those. As a general rule with InfluxDB, put numbers into values (my examples only have a single value called "value" but you can have multiple if needed) and text "meta-data" should generally be in tags.

---

<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:** [27 July 2022 11:04 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/32 "2022-07-27T11:04:19Z")

</div>

A week ago I suggested that this is how you should format the data, using the device id as a tag, but you seemed to be insisting that you did not want to do that, but wanted the device id as the measurement.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [27 July 2022 12:46 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/33 "2022-07-27T12:46:42Z")

</div>

That is absolutely correct. Device id is meta-data and should be in a tag such as "deviceId". That lets you report by device as needed.

A measurement is like a table in other db's and you would rarely want to create a table for every device, that would be terribly inefficient.

---

<div class="post-metadata">

**Author:** ![maru](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/maru/32/99601_2.png) [@maru](https://discourse.nodered.org/u/maru)\
**Post date:** [27 July 2022 13:40 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/34 "2022-07-27T13:40:52Z")

</div>

Dear @TotallyInformation and @Colin

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/d/3df225ce503aa152441c91e12fd95a1b02662431.png)  
_set location and type:_

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/2/a2bf2d48132e82734626278cd0ab0bad3592f3a6.png)

_Data in Influxdb_

I now have 2 working solutions, so I have enough possibilities to work out further and to focus on graphs in Chronograph. The time out request error has also disappeared (disable Verify server certificate in the properties tab of the Influxdb node)

Thank you both very much for your advices. 👍

---

<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:** [10 August 2022 13:40 UTC](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339/35 "2022-08-10T13:40:53Z")

</div>

Closed on 10 August 2022 13:40 UTC

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

[Previous page](https://discourse.nodered.org/t/store-data-in-influxdb-show-in-chronograf/65339.md?page=1)
