# Publish NaN on MQTT

**URL:** <https://discourse.nodered.org/t/publish-nan-on-mqtt/31301>\
**Category:** General\
**Created:** [8 August 2020 04:18 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301 "2020-08-08T04:18:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [8 August 2020 04:18 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/1 "2020-08-08T04:18:17Z")

</div>

Hi to Everybody,  
I'm slowly learning Node-Red and more than I use it more I lve it, but now I found a problem that I'm not able to solve.

I'm trying to convert a Latitude and Longitude from Numeric value to a Degree and decimal value.  
It's just some math.  
After my function calc the value I would like to publish a new topic on my MQTT server, it works but Node-Red publish my topic + a 'NaN'  
But it happened only on my Mosquito local server, if I try to publish on my cloud server (flespi) it works regurarly.

I cannot understand why.

 ![Immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/6/7630adbe0e5302554b13fe42dea1c3cac3da4eee.jpeg)

Here my node:

```auto
[{"id":"954e36fc.61cc28","type":"debug","z":"2dedba2a.ed27f6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":910,"y":180,"wires":[]},{"id":"4315bd8f.f3d934","type":"json","z":"2dedba2a.ed27f6","name":"","property":"payload","action":"","pretty":false,"x":450,"y":140,"wires":[["e03e1031.ca58d"]]},{"id":"e03e1031.ca58d","type":"function","z":"2dedba2a.ed27f6","name":"","func":"\n\nlat = msg.payload.latitude;\nvar convertLat = Math.abs(lat);\nvar LatDeg = Math.floor(convertLat);\nvar LatMin = ((convertLat - LatDeg) * 60).toFixed(3);\nvar LatCardinal = ((lat > 0) ? \"N\" : \"S\");\nnewlat = LatCardinal + \" \" + LatDeg + \"°\" + LatMin;\n\nlon = msg.payload.longitude;\nvar convertLon = Math.abs(lon);\nvar LonDeg = Math.floor(convertLon);\nvar LonMin = ((convertLon - LonDeg) * 60).toFixed(3);\nvar LonCardinal = ((lon > 0) ? \"E\" : \"W\");\nnewlon = LonCardinal + \" \" + LonDeg + \"°\" + LonMin;\n\nmsg.payload = { \n \"longitude\" : newlon,\n \"latitude\" : newlat\n}\nmsg.topic = \"vessels/self/navigation/NewPosition\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":640,"y":140,"wires":[["954e36fc.61cc28","c1fc63e4.3e709"]]},{"id":"c1fc63e4.3e709","type":"mqtt out","z":"2dedba2a.ed27f6","name":"NewPosition","topic":"","qos":"0","retain":"false","broker":"a6ffbe0b.cb31d","x":910,"y":120,"wires":[]},{"id":"b41ea6f.b7b2b58","type":"inject","z":"2dedba2a.ed27f6","name":"test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"vessels/self/navigation/position","payload":"{\"longitude\":174.32633333333334,\"latitude\":-35.72366666666667}","payloadType":"str","x":200,"y":140,"wires":[["4315bd8f.f3d934"]]},{"id":"a6ffbe0b.cb31d","type":"mqtt-broker","z":"","name":"Y2K","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

Thanks in advance  
Max

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 August 2020 04:34 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/2 "2020-08-08T04:34:59Z")

</div>

Welcome.

The inject node type is set to a/z, change that to json and remove the json node and then retry.  
The debug output should show the output as mqtt.

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [8 August 2020 04:58 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/3 "2020-08-08T04:58:46Z")

</div>

Hi,  
just tried but same result.  
The strange things is that it happen only in local... I think I have to search the problem on my local mqtt server.

Max

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [8 August 2020 05:11 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/4 "2020-08-08T05:11:48Z")

</div>

Is the input always the same, or do you get it from somewhere ?

`NaN` means "Not-a-Number" - this happens when you have a function that tries to perform a number comparison on input/output but the in/output type does not evaluate to a number.

Or do you have perhaps have another flow that is also writing to the same topic ?

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [8 August 2020 05:14 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/5 "2020-08-08T05:14:29Z")

</div>

The input is not always the same, it come from a SignalK node server.  
I use the Inject node to simulate, otherwise it is almost impossible for you to help me.  
But the data type is exactly the same.

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [8 August 2020 05:16 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/6 "2020-08-08T05:16:09Z")

</div>

I've just solve the problem sending back the new result to SignalK Node Server and from there to MQTT.  
It works....but I don't know why.  
I'm pretty sure that it is a problem on my local Mqtt server.

---

<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:** [8 August 2020 08:01 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/7 "2020-08-08T08:01:11Z")

</div>

Where exactly were you seeing NaN? In the debug node in the flow you posted?  
What local MQTT server are you using?

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [8 August 2020 22:38 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/8 "2020-08-08T22:38:11Z")

</div>

Hi Colin,  
I can see NaN only on the topic on my mqtt server, not in the debug.  
What Mqtt server do I use?  
This is a good question, yesterday when I wrote this post I was pretty sure that it is Mosquitto, but now I'm not so sure.  
It is on a Raspberry Pi3 with Openplotter, everything is already installed with the image that they provide, but I have no idea of what version I'm using.

I'm pretty sure that the problem is on my mqtt server, because if I publish the same topic on a cloud server (Flespi) I cannot see any NaN.

For this reason I publish the topic from Node-Red on SignalK node server.

Thanks  
Max

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 August 2020 22:59 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/9 "2020-08-08T22:59:58Z")

</div>

From what I read you are getting this:

```auto
{"longitude":"E 174°19.580","latitude":"S 35°43.420"}

```

Auckland?

Stripping away the _longitude_ and _latitude_ parts, it still isn't a number.

It has the degree sign in it.

And there are two entrys.  
(The E and the S parts)

> [@spidgrou](#):
>
> After my function calc the value I would like to publish a new topic on my MQTT server, it works but Node-Red publish my topic + a 'NaN'  
> But it happened only on my Mosquito local server, if I try to publish on my cloud server (flespi) it works regurarly.
> 
> I cannot understand wh

Can't say.

> I've just solve the problem sending back the new result to SignalK Node Server and from there to MQTT.  
> It works....but I don't know why.

I'm suspicious that the `SignalK Node Server` is doing something you don't yet know.  
So it is accepting the message if it has been through that.

Suggestion: Put a `debug` node to see what is going into that part of the flow and another to see what is coming out of it.

In the `debug` node set it to show the entire message.

---

<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:** [9 August 2020 07:58 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/10 "2020-08-09T07:58:13Z")

</div>

> [@spidgrou](#):
>
> I can see NaN only on the topic on my mqtt server, not in the debug.

Do you mean that you never see the same information back that you publish, or that sometimes it works and sometimes it doesn't?  
If you want to analyse this further then add an MQTT In node subscribed to the same topic feeding a debug node and see what you get. You should see the debug output from your function node then immediately the same information picked up, in the other debug node. Give the debug nodes names so it is easy to identify which is which in the debug output. If it isn't doing what it should then post a screenshot.

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [11 August 2020 05:14 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/11 "2020-08-11T05:14:25Z")

</div>

Colin,  
I think is a problem of my MQTT Server that comes with OpenPlotter and I cannot work on it.  
The same MQTT publish sent to another MQTT server works.  
I work with the SignalK and for me is the same.

Thanks for your help.  
Max

---

<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:** [11 August 2020 05:26 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/12 "2020-08-11T05:26:37Z")

</div>

> [@spidgrou](#):
>
> I think is a problem of my MQTT Server that comes with OpenPlotter

I suspect there may be another reason. Are you able to answer the questions I asked?

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [11 August 2020 05:56 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/13 "2020-08-11T05:56:38Z")

</div>

Colin,  
yes I can, it is only not to bother you. 😉  
Here the screenshot:

 ![Immagine](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/a/da524d39ccc5047f0ace715de9eff7c4d261accd.jpeg)

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [11 August 2020 06:14 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/14 "2020-08-11T06:14:19Z")

</div>

_edit_

this is the output of your own function. forget my post.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [11 August 2020 07:47 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/15 "2020-08-11T07:47:20Z")

</div>

To me that looks like somehow you are getting two replies to the one message you send.

So this is with the non-working MQTT broker then.

Can you open the `MQTT IN` node and show the settings it has?

Or, just for the sake of it: Post that flow.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [11 August 2020 08:56 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/17 "2020-08-11T08:56:06Z")

</div>

Oh, silly me.  
(Saw your post before you withdrew it)

I'll look at it if/when I get a chance today.  
Up to my neck in other things just now.

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [11 August 2020 09:09 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/18 "2020-08-11T09:09:55Z")

</div>

Yeah,  
I remove it, because I thought you ask another thing and I was going to check the definitions. I'm a real newbie.  
Thanks  
Max

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [11 August 2020 09:11 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/19 "2020-08-11T09:11:03Z")

</div>

No problems.

Believe me: There's nothing wrong with not knowing. I'm pretty good at that part. 😉

Um, the code at the top only has the `MQTT OUT` part. Not the `MQTT IN`.  
I know it is (probably) petty. But could you open the node and take a screen shot and show what it is doing?

Or post that `MQTT IN` node code....

---

<div class="post-metadata">

**Author:** ![spidgrou](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spidgrou/32/27005_2.png) [@spidgrou](https://discourse.nodered.org/u/spidgrou)\
**Post date:** [11 August 2020 10:12 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/20 "2020-08-11T10:12:59Z")

</div>

Here the node of the screenshot that I've public one hour ago

```auto
[{"id":"69f2f30b.8bfd1c","type":"debug","z":"63b05b24.a7ac54","name":"Publish","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1160,"y":180,"wires":[]},{"id":"e739c3f.162d74","type":"json","z":"63b05b24.a7ac54","name":"","property":"payload","action":"","pretty":false,"x":710,"y":140,"wires":[["8983dea4.522c5"]]},{"id":"8983dea4.522c5","type":"function","z":"63b05b24.a7ac54","name":"","func":"\n\nlat = msg.payload.latitude;\nvar convertLat = Math.abs(lat);\nvar LatDeg = Math.floor(convertLat);\nvar LatMin = ((convertLat - LatDeg) * 60).toFixed(3);\nvar LatCardinal = ((lat > 0) ? \"N\" : \"S\");\nnewlat = LatCardinal + \" \" + LatDeg + \"°\" + LatMin;\n\nlon = msg.payload.longitude;\nvar convertLon = Math.abs(lon);\nvar LonDeg = Math.floor(convertLon);\nvar LonMin = ((convertLon - LonDeg) * 60).toFixed(3);\nvar LonCardinal = ((lon > 0) ? \"E\" : \"W\");\nnewlon = LonCardinal + \" \" + LonDeg + \"°\" + LonMin;\n\nmsg.payload = { \n \"longitude\" : newlon,\n \"latitude\" : newlat\n}\nmsg.topic = \"vessels/self/navigation/NewPositionTest\"\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":140,"wires":[["69f2f30b.8bfd1c","75ea23e4.be5b1c"]]},{"id":"75ea23e4.be5b1c","type":"mqtt out","z":"63b05b24.a7ac54","name":"NewPosition","topic":"","qos":"0","retain":"false","broker":"a6ffbe0b.cb31d","x":1170,"y":120,"wires":[]},{"id":"e335a63e.d514f8","type":"inject","z":"63b05b24.a7ac54","name":"test","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"vessels/self/navigation/position","payload":"{\"longitude\":174.32633333333334,\"latitude\":-35.72366666666667}","payloadType":"str","x":460,"y":140,"wires":[["e739c3f.162d74"]]},{"id":"fe809ed0.c0201","type":"mqtt in","z":"63b05b24.a7ac54","name":"","topic":"vessels/self/navigation/NewPositionTest","qos":"2","datatype":"auto","broker":"a6ffbe0b.cb31d","x":560,"y":240,"wires":[["596bb7ba.002ad8"]]},{"id":"596bb7ba.002ad8","type":"debug","z":"63b05b24.a7ac54","name":"Subscribe","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1160,"y":260,"wires":[]},{"id":"a6ffbe0b.cb31d","type":"mqtt-broker","z":"","name":"Y2K","broker":"localhost","port":"1883","clientid":"NodeRedY2K","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [11 August 2020 10:20 UTC](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301/21 "2020-08-11T10:20:48Z")

</div>

Ok, so the _Y2K_ `MQTT` is really your local host.

Anyway..... The `MQTT IN` node.

Change its output to:  
`a parsed JSON object`

That may help.

But now I'm stumped how _another one_ works.  
(another `MQTT` broker.)

[Next page](https://discourse.nodered.org/t/publish-nan-on-mqtt/31301.md?page=2)
