Publish NaN on MQTT

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.

Here my node:

[{"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

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.

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

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 ?

1 Like

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.

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.

1 Like

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

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

From what I read you are getting this:

{"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)

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.

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.

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

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

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

edit

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

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.

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.

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

No problems.

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

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....

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

[{"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":""}]

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.)