Msg.payload to number for InfluxDB for Grafana

Hi all, I am struggling to convert the msg.payload to be used as a number when writing to InfluxDB.

I currently send data to MQTT and my payload/content is just numbers, example 65
and under the 'change node' I have set msg.payload defaults.

It writes to InfluxDB well, however, when reading the data from Grafana, it requires it to be a number for more details.

Is there something I need to define in the 'change node'? Thanks in advance!

When I use InfluxDB the node is expecting an object. If there is no timestamp in the object it uses the time that the message was sent

Example function in function node

msg.payload = {
    name: "Temp1",
    DegC: msg.payload,
};
return msg;

Welcome kelvins!

I did a test flow some time ago, looks like this:


It queries the network interface and writes the bytes transmitted and received in a InfluxDB. I use version 2.0
The function for rx looks like:

var stdout = msg.payload;
var regex_rx = /\s*RX packets\s*\d*\s*bytes\s*(\d*)/;
const m_rx = stdout.match(regex_rx);    
msg.payload = Number(m_rx[1]);
return msg;

This payload is then sent to the database.
The configuration of the Influx node is
image

Feed the message from the MQTT node into a debug node and post what it shows.

If it is just a number but as a string, so for example the debug may show msg.payload as String "12.3" then if in the MQTT node you tell it to output a Parsed JSON object then it will parse that string and convert it to a number. Then the debug node should show it as a number.

1 Like

Thank you for your input!

Thank you for your input and clear example!

Colin! This was it! Thank you so much! This worked for me!!

Excellent. It works because a string containing a number is valid JSON, so the node parses it and converts it into a javascript object, which in this case is just a number.

1 Like

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