# MQTT transformation before influxdb

**URL:** https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919
**Category:** General
**Created:** [2 March 2021 22:18 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919 "2021-03-02T22:18:20Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [2 March 2021 22:18 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/1 "2021-03-02T22:18:20Z")

</div>

Dear all,

I have a small problem and I am turning in round without solution. I am receiving MQTT topic and I have to divided them by 10 before to sent them to an influxdb database.  
Simple but ...it is not working .Probably a problem of type of variable.

Here the function that I am trying to adapt

```auto
var msg2 = null;
var value = Number(msg.payload)/10; // convert it to a number in case it is a string
if (!isNaN(value)) {
    msg2 = {payload: {"OT_item24": value}};
}
return msg2;

```

if somebody have an idea to help me...

have a good evening

---

<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: [2 March 2021 22:21 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/2 "2021-03-02T22:21:34Z")

</div>

Connect a debug before the function and after function. Show us what goes in and what comes out.

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [2 March 2021 22:26 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/3 "2021-03-02T22:26:58Z")

</div>

my Mqtt topic is OT\_item24.

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

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [2 March 2021 22:31 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/4 "2021-03-02T22:31:19Z")

</div>

and here my node : ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/31d11e8f38bd45edbfab833a31ed2d3942227f9c.png)

I am feeling , I am very near but no....snif snif

---

<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: [2 March 2021 22:31 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/5 "2021-03-02T22:31:22Z")

</div>

The divide by 10 function is working. What's your issue?

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [2 March 2021 22:33 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/6 "2021-03-02T22:33:11Z")

</div>

no, I do not find the value on influxdb when I request OT\_item24 by grafana. I am thinking it is because the value is not reconize as number

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [2 March 2021 22:37 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/7 "2021-03-02T22:37:55Z")

</div>

if I do without transformation is working nice

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

---

<div class="post-metadata">

### Author: ![LosinIt](https://avatars.discourse-cdn.com/v4/letter/l/439d5e/32.png) [@LosinIt](https://discourse.nodered.org/u/LosinIt)
#### Post date: [3 March 2021 00:43 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/8 "2021-03-03T00:43:27Z")

</div>

I don't think your function is populating the new message completely/properly but I'm new with this. Here is an example that works perfectly for me.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/5/15c7e0934735d53e561697ecd8547a511683ba0b.png)

HTH

---

<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: [3 March 2021 07:12 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/9 "2021-03-03T07:12:04Z")

</div>

All you need is  
`msg.payload = Number(msg.payload)/10`  
`return msg`

In addition, if you set the mqtt node output to Parsed JSON object then it will automatically convert it to a number for you.

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [3 March 2021 11:15 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/10 "2021-03-03T11:15:18Z")

</div>

Thanks for your reply but when I do like that I have a debug after function : Error: A 400 Bad Request error occurred: {"error":"partial write: field type conflict: input field "value" on measurement "OT\_item24" is type float, already exists as type string dropped=1"}

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [3 March 2021 11:29 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/11 "2021-03-03T11:29:06Z")

</div>

Thanks , I have tried your method but with it , I obtained the debug message :Error: A 400 Bad Request error occurred: {"error":"unable to parse 'OT\_item24 device=undefined,value=18.8': invalid boolean"}

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [3 March 2021 11:37 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/12 "2021-03-03T11:37:44Z")

</div>

From MQTT node the output is string :

OT\_item24 : msg.payload : string[3]  
"186"

---

<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: [3 March 2021 11:54 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/13 "2021-03-03T11:54:27Z")

</div>

> [@bert2713d](#):
>
> Error: A 400 Bad Request error occurred: {"error":"partial write: field type conflict: input field "value" on measurement "OT\_item24" is type float, already exists as type string dropped=1"

The clue is in the error message. By previously connecting the mqtt node directly the influx node, and without telling the mqtt node to parse the string it receives as a JSON string, you have sent a string to that field instead of a number. Now you can only send it strings. To fix that you will have to delete (drop) that measurement from the database. Or change the measurement name to a new name.

---

<div class="post-metadata">

### Author: ![bert2713d](https://avatars.discourse-cdn.com/v4/letter/b/db5fbb/32.png) [@bert2713d](https://discourse.nodered.org/u/bert2713d)
#### Post date: [3 March 2021 13:01 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/14 "2021-03-03T13:01:34Z")

</div>

> [@Colin](#):
>
> `msg.payload = Number(msg.payload)/10`  
> `return msg`

So gracias ! it is working perfectly. have a nice week

---

<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: [2 May 2021 13:01 UTC](https://discourse.nodered.org/t/mqtt-transformation-before-influxdb/41919/15 "2021-05-02T13:01:35Z")

</div>

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