# Function to process incoming msg from MQTT before passing to Influxdb batch out

**URL:** https://discourse.nodered.org/t/function-to-process-incoming-msg-from-mqtt-before-passing-to-influxdb-batch-out/85418
**Category:** General
**Tags:** docker, function-node
**Created:** [11 February 2024 15:05 UTC](https://discourse.nodered.org/t/function-to-process-incoming-msg-from-mqtt-before-passing-to-influxdb-batch-out/85418 "2024-02-11T15:05:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ricm](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@ricm](https://discourse.nodered.org/u/ricm)
#### Post date: [11 February 2024 15:05 UTC](https://discourse.nodered.org/t/function-to-process-incoming-msg-from-mqtt-before-passing-to-influxdb-batch-out/85418/1 "2024-02-11T15:05:06Z")

</div>

The overall shape of the project follows the familiar route:  
`ESP32 collects sensor data --> passes readings to MQTT broker via a JSON document`  
The Broker sits on a raspberry pi with all applications running in containers under docker:  
` MQTT Broker --> Node-Red --> InfluxDB V1.8 --> Grafana`  
I have everything working for a single reading, but when it comes to a batch of readings, the error reported is that the values are 'undefined'  
The MQTT IN Node settings are:

```auto
  Server: raspberrypi
  Action: subscribe to single topic
  Topic: some_prefix/boiler
  Output: A parsed JSON object

```

The incoming message is received with the above MQTT In Node. Here's the debug output:

```auto
  some_prefix/boiler : msg.payload : Object
  object
    temp_dhw_flow: 71.6
    temp_dhw_return: 46.6
    temp_ch_flow: 62.7
    temp_ch_return: 51.2
    pres_boiler: 1.23
    leak_boiler: 407.9

```

The message is then passed to a function that strips out the prefix:

```auto
   var t=msg.topic;
   t = t.replace('/some_prefix/', '');
   t = t.replace('some_prefix/', '');
   while(t.search("/")!=-1){
     t = t.replace('/', '-');
   }
   msg.topic=t;
   msg.measurement=t;
   return msg;

```

A debug node connected to the output of this function correctly shows:

```auto
  boiler : msg.payload : Object <-- (note the prefix has been stripped)
  object
    temp_dhw_flow: 71.6
    temp_dhw_return: 46.6
    temp_ch_flow: 62.7
    temp_ch_return: 51.2
    pres_boiler: 1.23
    leak_boiler: 407.9

```

The message is then passed to a further function to extract the values prior to sending to the connected Influxdb batch out node:

```auto
msg.payload = [
    {
        measurement: "boiler",
        timestamp: new Date(),
        fields: {
            tempDHW_Flow: msg.payload.tempDHW_Flow,
            tempDHW_Return: msg.payload.tempDHW_Return,
            tempCH_Flow: msg.payload.tempCH_Flow,
            tempCH_Return: msg.payload.tempCH_Return,
            presBoiler: msg.payload.presBoiler,
            leakBoiler: msg.payload.leakBoiler
         },
        tags:{
            location:"garage",
            /*sensor: "board 1"*/
        }
    }
];
return msg;

```

I have almost exactly the same construct working for a single reading (tempDHW\_Flow) but the above yields the following in a debug node connected to this second function (also presented as an error in a Catch All node):

```auto
boiler : msg.payload : array[1]
array[1]
0: object
 measurement: "boiler"
 timestamp: "2024-02-11T15:25:29.589Z"
 fields: object
    tempDHW_Flow: undefined
    tempDHW_Return: undefined
    tempCH_Flow: undefined
    tempCH_Return: undefined
    presBoiler: undefined
    leakBoiler: undefined
 tags: object
    location: "garage"

```

Consequently no data is passed to the InfluxDB batch Out Node.

I've spent some time trying to research this problem, without success. I have successfully written to InfluxDB previously using the same Batch out Node, so the problem is almost certainly the failure of the function given the form of the input data.

Suggestions welcome.  
Thanks in advance,  
Ric

---

<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: [11 February 2024 15:20 UTC](https://discourse.nodered.org/t/function-to-process-incoming-msg-from-mqtt-before-passing-to-influxdb-batch-out/85418/2 "2024-02-11T15:20:59Z")

</div>

Is there a question here?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [11 February 2024 15:44 UTC](https://discourse.nodered.org/t/function-to-process-incoming-msg-from-mqtt-before-passing-to-influxdb-batch-out/85418/3 "2024-02-11T15:44:50Z")

</div>

> [@ricm](#):
>
> A debug node connected to the output of this function correctly shows:
> 
> ```auto
> boiler : msg.payload : Object <-- (note the prefix has been stripped)
> object
> temp_dhw_flow: 71.6
> 
> ```

## `temp_dhw_flow` ☝
  

> [@ricm](#):
>
> The message is then passed to a further function to extract the values prior to sending to the connected Influxdb batch out node:
> 
> ```auto
> msg.payload = [
> {
> measurement: "boiler",
> timestamp: new Date(),
> fields: {
> tempDHW_Flow: msg.payload.tempDHW_Flow,
> 
> ```

## `tempDHW_Flow` ☝

These are different.

---

<div class="post-metadata">

### Author: ![ricm](https://avatars.discourse-cdn.com/v4/letter/r/8e8cbc/32.png) [@ricm](https://discourse.nodered.org/u/ricm)
#### Post date: [11 February 2024 16:22 UTC](https://discourse.nodered.org/t/function-to-process-incoming-msg-from-mqtt-before-passing-to-influxdb-batch-out/85418/4 "2024-02-11T16:22:06Z")

</div>

Hi and thanks,

I had piggy-backed some other code where I sent a JSON document to an AsyncWebServer running on the ESP32. The JSON document fed readings to a number of HighCharts graphs. I hadn't noticed I changed the names (to avoid ambiguity elsewhere) and blindly re-used the same JSON document for the MQTT broker. A very simple change to the 2nd function solved this.

If only we could see the obvious...

As a footnote: I've had only a few days exposure to Node-Red and think it's just amazing. Javascript and JSON are all new to me, but I am truly impressed by the power and flexibility of Node-Red.

Thank you again. Appreciated.  
Ric
