Saving data received from MQTT to InfluxDB using Node-Red

My NodeMCU are sending data using MQTT to the broker. Then this flow get the topic and the data and save on InfluxDB using its HTTP API.


In function we have this code:

var tokens = msg.topic.split("/");
var dest = tokens[tokens.length-1];
msg.url ="http://localhost:8086/write?db=greenhouse";
msg.payload = tokens[2] + ",device=" + tokens[1] + " value=" + msg.payload;
return msg;

Here is a graphics sample:



  1. Thanks for making this code available. In my case I had to make a few changes (trial-and-error) to make it work.

    My version goes like this:

    var tokens = msg.topic.split(“/”);
    var dest = tokens[tokens.length-1];
    msg.url =”http://localhost:8086/write?db=insertDatabaseHere”;
    msg.payload = tokens[1] + “,device=” + tokens[0] + ” value=” + msg.payload;
    return msg;

    The change is that instead of referring to tokens[2] and tokens[1], I’m instead using tokens[1] and tokens[0].
    I’m also not sure what the purpose of the “dest” variable is.

    1. Depends on how your data is sent to MQTT. “dest” variable is a copy and paste unused thing. 🙂
      Is very nice to see that this was interesting for some one.
      I’m reworking all the workflow for save the MQTT’s data. I will post here when I will have something good to show.

      1. Right, in my case the topic is in the format “room/measurement” (e.g., “lvr/temp”, “bsmt/rh”) so I just need to extract those values from the topic.

        Like you, I have the data going from MQTT through Node-Red into InfluxDB, and then plotted on Grafana. All four of these packages running on the same Raspberry Pi 2. The temperature and humidity data comes from ESP8266 units.

        My next project is to find a way to manage the InfluxDB data so that I don’t fill up or kill the Raspberry Pi’s SD card by writing to it so often.

        I am a total novice coder (can do basic Python and Arduino scripts) so your contribution was very helpful to me!

Deixe uma resposta

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *