# Problem with number converting from mqtt retained messages

**URL:** https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077
**Category:** General
**Created:** [31 January 2020 10:33 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077 "2020-01-31T10:33:54Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Ragnarre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ragnarre/32/16620_2.png) [@Ragnarre](https://discourse.nodered.org/u/Ragnarre)
#### Post date: [31 January 2020 10:33 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/1 "2020-01-31T10:33:55Z")

</div>

Hello. Im having difficulties converting data i think. node named "juhtimine" does not output numbers as i think it should. I have no previous experiences with javascript.

Heres the flow, i hope real programmers understand what i want to do in this flow.

```auto
[{"id":"417c5761.10e258","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"93185b2c.d53f18","type":"mqtt in","z":"417c5761.10e258","name":"Avamise temp","topic":"automaatika/MisTempAvab_KeskmineTuba","qos":"2","datatype":"json","broker":"d4c6732f.76e66","x":110,"y":300,"wires":[["383a4d55.ad099a"]]},{"id":"83b8cce1.4807f","type":"mqtt in","z":"417c5761.10e258","name":"Sulgemise temp","topic":"automaatika/MisTempSulgeb_KeskmineTuba","qos":"2","datatype":"auto","broker":"d4c6732f.76e66","x":120,"y":220,"wires":[["341f6b74.208cf4"]]},{"id":"5bd5877e.9a4e88","type":"mqtt in","z":"417c5761.10e258","name":"Autom.liigutamineON/OFF","topic":"automaatika/onoff_keskminetuba","qos":"2","datatype":"utf8","broker":"d4c6732f.76e66","x":150,"y":260,"wires":[["721c684a.b1712"]]},{"id":"cbab4b9a.574e9","type":"debug","z":"417c5761.10e258","name":"automaatika","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":570,"y":280,"wires":[]},{"id":"341f6b74.208cf4","type":"function","z":"417c5761.10e258","name":"Sulgemistemp","func":"var x = (msg.payload);\nvar y = {sulgemisetemp: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":300,"y":220,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"383a4d55.ad099a","type":"function","z":"417c5761.10e258","name":"Avamistemp","func":"var x = (msg.payload);\nvar y = {avamistemp: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":270,"y":300,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"dd1b0553.141df","type":"mqtt in","z":"417c5761.10e258","name":"Temp","topic":"keskmine_tuba/temp","qos":"2","datatype":"json","broker":"d4c6732f.76e66","x":90,"y":60,"wires":[["3acff23d.60ffe6"]]},{"id":"3acff23d.60ffe6","type":"function","z":"417c5761.10e258","name":"Temp","func":"var x = (msg.payload);\nvar y = {temp: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":270,"y":60,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"b98337a5.8a2928","type":"function","z":"417c5761.10e258","name":"juhtimine","func":"//if(msg.payload.lyliti == \"ON\"){\nvar temp = (msg.payload.temp);\nvar a = (msg.payload.lyliti);\nvar b = (msg.payload.sulgemistemp);\nvar c = (msg.payload.avamistemp);\nreturn {payload : b} //for debug purposes\n\n if(temp <= b && a == \"ON\"){\nreturn {payload : 0}\n} \n if(temp >= c && a == \"ON\"){\nreturn {payload : 1}\n} \n","outputs":1,"noerr":0,"x":560,"y":220,"wires":[["6803468.c8df5b8","6d7f7ab7.95854c"]]},{"id":"721c684a.b1712","type":"function","z":"417c5761.10e258","name":"lyliti","func":"var x = (msg.payload);\nvar y = {lyliti: x};\nreturn {payload : y}","outputs":1,"noerr":0,"x":330,"y":260,"wires":[["cbab4b9a.574e9","b98337a5.8a2928"]]},{"id":"6803468.c8df5b8","type":"debug","z":"417c5761.10e258","name":"juhtimine","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":760,"y":260,"wires":[]},{"id":"6d7f7ab7.95854c","type":"mqtt out","z":"417c5761.10e258","name":"akna juhtimine","topic":"suur_tuba/juhtimine","qos":"","retain":"","broker":"d4c6732f.76e66","x":780,"y":220,"wires":[],"info":"# ## 1"},{"id":"bdbec942.f3295","type":"comment","z":"417c5761.10e258","name":"these 3 mqtt subscriptions are retained messages from openhab, messages will receive here but \"juhtimine\" wil output error \"NaN\" after outputting variablesl ","info":"","x":560,"y":180,"wires":[]},{"id":"d4c6732f.76e66","type":"mqtt-broker","z":"","name":"localhost","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
```

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [31 January 2020 11:03 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/2 "2020-01-31T11:03:17Z")

</div>

You should go read [https://cookbook.nodered.org/basic/join-streams](https://cookbook.nodered.org/basic/join-streams) and look at your flow again.

---

<div class="post-metadata">

### Author: ![Ragnarre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ragnarre/32/16620_2.png) [@Ragnarre](https://discourse.nodered.org/u/Ragnarre)
#### Post date: [31 January 2020 15:38 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/3 "2020-01-31T15:38:48Z")

</div>

I did but got same output.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [31 January 2020 15:41 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/4 "2020-01-31T15:41:52Z")

</div>

Show the flow where you are now joining the messages.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [31 January 2020 16:29 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/5 "2020-01-31T16:29:23Z")

</div>

> [@Ragnarre](#):
>
> not output numbers

Hi and welcome.

1st it is necessary that you send screenshots from the output of your debug nodes because we cannot simulate your input (from mqtt)

What I can tell you now is all data you receive from mqtt is handled as strings. Mqtt data don`t carry any information of the type of data. Even if javascript tries the best to convert data types as nessesary it is better not to rely on its performance. When it fails, ie. using a malformed number in a string inside an equation it will result in NaN means "Not a Number".

You first have to analyse your input data and try to convert it i.e ` var temp = Number(msg.payload.temp);` This is likely to fail so we need to see your input data (content of msg.payload.temp.

---

<div class="post-metadata">

### Author: ![Ragnarre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ragnarre/32/16620_2.png) [@Ragnarre](https://discourse.nodered.org/u/Ragnarre)
#### Post date: [3 February 2020 19:50 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/6 "2020-02-03T19:50:42Z")

</div>

i have been playing around with "join", "change" node and other functions but havent found any solution because i dont really understand why last node dont see these values.  
complete msg object prints out these values...

`[
    {
        "id": "b98337a5.8a2928",
        "type": "function",
        "z": "417c5761.10e258",
        "name": "juhtimine",
        "func": "//if(msg.payload.lyliti == \"ON\"){\nvar temp = (msg.payload.temp);\nvar a = (msg.payload.lyliti);\nvar b = (msg.payload.sulgemisetemp);\nvar c = (msg.payload.avamistemp);\nreturn {a, b, c, temp}; //for debug purposes\n\n/*\nif(temp <= b){\nvar z = \"0\";\nreturn {payload : z}\n} \nif(temp >= c){\nvar kk = \"1\";\nreturn {payload : kk}\n} \n*/",
        "outputs": 1,
        "noerr": 0,
        "x": 1080,
        "y": 80,
        "wires": [
            [
                "6803468.c8df5b8"
            ]
        ]
    }
]`  
[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/3/d3c8a5c4e8012e03f19b832ffcb3f417e4570b3b.png) ](https://dl.dropboxusercontent.com/s/yfsmxo9127w7dxc/Screenshot%202020-02-03%2021.23.25.png?dl=0)

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [3 February 2020 20:10 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/7 "2020-02-03T20:10:20Z")

</div>

Each msg enters the function node seperate from the other msg's. So when the msg out of the 'temp' function enters the 'justimine' function node, none of the data from the other three functions exist at that time.

And when the msg from the 'liliti' function node gets to the 'justimine' function node, the data from the 'temp' function is not there to be processed.

Nodes do not remember what has come before. You eithor have to join the output of the four function nodes together before processing them in the 'justimine' function node or each function node could put thit's data in a flow variable and then the 'justimine' function node could get the data from the flow variables BUT you need to be careful that all the data is correct and not being used multiple times.

---

<div class="post-metadata">

### Author: ![Ragnarre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ragnarre/32/16620_2.png) [@Ragnarre](https://discourse.nodered.org/u/Ragnarre)
#### Post date: [3 February 2020 20:18 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/8 "2020-02-03T20:18:33Z")

</div>

"temp" will be automatically updated every 30 second in mqtt broker.  
other mqtt values are retained and will change if manually changed (input from smartphone app).  
so these other values are lost after each update? what could be solution?

i did try to join them with "join" node but i messed up. could you make example for me?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [3 February 2020 21:18 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/9 "2020-02-03T21:18:12Z")

</div>

What you have set in MQTT has no bearing on what happens in the flows (other than it injects messages into the flow. What you should do it attach an `inject` node where each `mqtt` node is. This way you will duplicate what is happening but much slower. Even if you had one inject triggering all the paths, _ **each msg will arrive separately** _ .

So you need to join the msgs into one msg that has all the information needed by the last function node.

As for an example, here you go

```auto
[{"id":"8ccddb9a.a55f38","type":"inject","z":"ac14500e.2c57d","name":"temperature","topic":"temperature","payload":"10","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":1760,"wires":[["47b769c5.cb0e28"]]},{"id":"47b769c5.cb0e28","type":"join","z":"ac14500e.2c57d","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":false,"timeout":"","count":"3","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":310,"y":1800,"wires":[["f9afb265.b11b7"]]},{"id":"f9afb265.b11b7","type":"debug","z":"ac14500e.2c57d","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":470,"y":1800,"wires":[]},{"id":"2d269127.4f04ce","type":"inject","z":"ac14500e.2c57d","name":"humidity","topic":"humidity","payload":"","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":1800,"wires":[["47b769c5.cb0e28"]]},{"id":"d6fbe805.0e4628","type":"inject","z":"ac14500e.2c57d","name":"pressure","topic":"pressure","payload":"999","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":1840,"wires":[["47b769c5.cb0e28"]]}]

```

---

<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 February 2020 21:37 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/10 "2020-02-03T21:37:38Z")

</div>

You may also want to have 'and after every additional message' checked in the Join node, it depends on your requirement.

---

<div class="post-metadata">

### Author: ![Christian-Me](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/christian-me/32/10774_2.png) [@Christian-Me](https://discourse.nodered.org/u/Christian-Me)
#### Post date: [3 February 2020 21:51 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/11 "2020-02-03T21:51:47Z")

</div>

In the function in the third column there is something wrong. The objects you create in the return statement is not formatted well

```auto
return {“a”:a,”b”:b,”c”:c, “temp”:temp}

```

Is perhaps giving you the desired result here. A property inside an object is always defined by the property name as a string, a “:” and then the value. Values devided by “,” is more an array surrounded by “[“ but this would give you 4 messages on the output.

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [3 February 2020 22:25 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/12 "2020-02-03T22:25:19Z")

</div>

plus you are returning that as the msg - not the msg.payload so the debug is configured incorrectly if you want to see the complete message. You should really

`return {payload: {"a":a, "b":b, "c":c, "temp":temp})`

---

<div class="post-metadata">

### Author: ![Ragnarre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ragnarre/32/16620_2.png) [@Ragnarre](https://discourse.nodered.org/u/Ragnarre)
#### Post date: [4 February 2020 11:37 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/13 "2020-02-04T11:37:08Z")

</div>

return {a, b, c, temp}; was there for debugging purposes. please dont concentrate on that.  
real code that i need to get working is that:

var temp = (msg.payload.temp);  
var a = (msg.payload.lyliti);  
var b = (msg.payload.sulgemisetemp);  
var c = (msg.payload.avamistemp);

if(temp \<= b){  
var z = "0";  
return {payload : z}  
}  
if(temp \>= c){  
var kk = "1";  
return {payload : kk}  
}

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [4 February 2020 13:22 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/14 "2020-02-04T13:22:19Z")

</div>

Your function will only work if each msg that goes into the function contains a payload that is an object like

```auto
payload {
    "temp": some_value,
    "lyliti": : some_value,
    "sulgemisetemp": some_value,
    "avamistemp": some_value
}

```

1. Is that what is going into the 'function' node???
2. have you implimented the `join` node yet?

---

<div class="post-metadata">

### Author: ![Ragnarre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ragnarre/32/16620_2.png) [@Ragnarre](https://discourse.nodered.org/u/Ragnarre)
#### Post date: [4 February 2020 13:30 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/15 "2020-02-04T13:30:43Z")

</div>

arent these function nodes do the same like "join" node? i did not get join node to work.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [4 February 2020 13:42 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/16 "2020-02-04T13:42:46Z")

</div>

> [@Ragnarre](#):
>
> arent these function nodes do the same like "join" node?

Answer: No.

As I have said several times, a function node process EACH msg that arrives seperately.

> [@Ragnarre](#):
>
> i did not get join node to work.

what did not work? you haven't shown what you tried.

---

<div class="post-metadata">

### Author: ![Ragnarre](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ragnarre/32/16620_2.png) [@Ragnarre](https://discourse.nodered.org/u/Ragnarre)
#### Post date: [4 February 2020 14:28 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/17 "2020-02-04T14:28:16Z")

</div>

[![](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/d/ed09d8a279856964f3250e1fe62831e073b32a26.png) ](https://dl.dropboxusercontent.com/s/we3ts222pk7q47k/Screenshot%202020-02-04%2016.24.23.png?dl=0)

i really messed up everything while trying things out, could anyone do it for me? i could not find answers for my problem while googling.

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [4 February 2020 16:26 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/18 "2020-02-04T16:26:01Z")

</div>

1. What settings did you use in the join node?
2. put a `debug` node (set to display the complete msg object) on the output of each of the `mqtt-in` nodes
3. show the results of the debug nodes
4. export theflow and attach it to a reply.

---

<div class="post-metadata">

### Author: ![hotNipi](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hotnipi/32/383_2.png) [@hotNipi](https://discourse.nodered.org/u/hotNipi)
#### Post date: [4 February 2020 16:36 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/19 "2020-02-04T16:36:56Z")

</div>

There is a great video series I think you'll need to watch to get things going  
[https://www.youtube.com/results?search\_query=Node-RED+Essentials](https://www.youtube.com/results?search_query=Node-RED+Essentials)

Join node usage is covered here

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [4 February 2020 17:02 UTC](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077/20 "2020-02-04T17:02:24Z")

</div>

The reason you’re seeing these errors is because your properties contain slashes which are interpreted as divisor rather than part of the name. I saw this same issue explained to someone else a couple days ago, let me dig up the post for you. You can follow the instructions there to get rid of this error, then continue with the next part: following the instructions of those posting above me.

> [@How to create function on rpi node red with two gpio - if gpio1 == 1 && gpio2 ==0 then gpio3=1](https://discourse.nodered.org/t/how-to-create-function-on-rpi-node-red-with-two-gpio-if-gpio1-1-gpio2-0-then-gpio3-1/20406/27):
>
> Ah the joy of javascript.... your combined payloads look like this [image] all good so far Then your function does this if (msg.payload.pi/35 == 1 && msg.payload.pi/37 == 1) { which is a natural thing to do... but javascript doesn't like / in property names so it will be treating it like a divide :-)... so instead you need to do if (msg.payload["pi/35"] == 1 && msg.payload["pi/37"] == 1) {

[Next page](https://discourse.nodered.org/t/problem-with-number-converting-from-mqtt-retained-messages/21077.md?page=2)
