Store data in InfluxDB show in Chronograf

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

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

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:

/**
 * 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.

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

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;

Yes, correct I see the difference now :slight_smile:

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

:+1: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

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.

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 :+1:

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

My shared code shows you all of that.

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

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.

The converter and validator seem to work, but:

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

image

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.

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.

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.

Dear @TotallyInformation and @Colin


set location and type:

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. :+1:

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