Adding GPS values to msg.payload

63 views
Skip to first unread message

Dannaz Perth

unread,
Apr 24, 2018, 12:11:34 PM4/24/18
to Node-RED
Hello Chaps.

Been a while. I'm hoping one of you wonderful persons can help me solve something i have spent the last 7 hours on :(. I have looked deep and tried everything but cannot figure it out.

Here goes.
I have a GPS device publishing its Lat&Lon coordinates no problems. i have isolated and split the Lat& long values from the Object.
I have a piece of code that correctly publishes lat and long to my Mqtt broker. so all good here.
At the moment, to get things moving i'm  using 2 fake Lat & lon values.
My issue is i know need to replace the fake Lat & Lon values with the real ones.

Here is an example of code used to finalize the message

var lat = "34.736729";
var lng = "-92.34977";
var device_name = "gps";
var token = "my secret token";

var payload = {};

payload["position"] = {"value": "1", "context": {"lat": lat, "lng": lng}};
msg.payload = payload;
return msg;


These are my attepmt to extract the Lat&Lon values from the GPS Mqtt message


msg.payload = msg.payload.lat;
return msg;

msg.payload = msg.payload.lon;
return msg;

Here is the code.


[{"id":"7996b6c2.7bb1a8","type":"function","z":"6693f5d1.6ac5e4","name":"Extract Lat","func":"msg.payload = msg.payload.lat;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":60,"wires":[["8efcc485.be22d","e20e8324.cdfcd8"]]},{"id":"5d68612c.c1fe08","type":"json","z":"6693f5d1.6ac5e4","name":"","property":"payload","action":"","pretty":false,"x":270,"y":60,"wires":[["7996b6c2.7bb1a8"]]},{"id":"8583df88.3f5b78","type":"json","z":"6693f5d1.6ac5e4","name":"","property":"payload","action":"","pretty":false,"x":270,"y":100,"wires":[["86a4f4a8.cf08"]]},{"id":"86a4f4a8.cf08","type":"function","z":"6693f5d1.6ac5e4","name":"Extract Long","func":"msg.payload = msg.payload.lon;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":100,"wires":[["8efcc485.be22d","e20e8324.cdfcd8"]]},{"id":"e20e8324.cdfcd8","type":"function","z":"6693f5d1.6ac5e4","name":"create_payload","func":"var lat = \"34.736729\";\nvar lng = \"-92.34977\";\nvar device_name = \"gps\";\nvar token = \"mysecrettoken\";\n\nvar payload = {};\n\npayload[\"position\"] = {\"value\": \"1\", \"context\": {\"lat\": lat, \"lng\": lng}};\nmsg.payload = payload;\nreturn msg;","outputs":1,"noerr":0,"x":660,"y":80,"wires":[[]]}]

Any help would be really great, i have learnt so much in the last 6 months, sometimes i just hit a massive brick wall and my ability to think over it stops dead!

Thank you in advance Chapsies







Colin Law

unread,
Apr 24, 2018, 12:26:04 PM4/24/18
to node...@googlegroups.com
I am afraid I don't understand what you are trying to do, and what the
code you have posted does. You posted some code that 'finalises' the
message, what does that mean?

The other code is supposed to extract the lat and lon from the mqtt
message, but you haven't told us what is the format of the message
coming from mqtt or what exactly you want to do with it.

Please try and explain what you need help with again.

Colin
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/6b4aa06e-d547-4945-bf12-31b1b85b09c5%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

AIOT MAKER

unread,
Apr 24, 2018, 12:50:33 PM4/24/18
to Node-RED
My guess..

If you want to extract (isolate in msg.payload) the  lat and lon values, without any change on the function node you have shown, then it could work adding these two  function nodes (or eventually a change node) with the code:

msg.payload = msg.payload.position.context.lat;
return msg;

msg.payload = msg.payload.position.context.lng;
return msg;


It is always useful to check the debug panel to get the path of the property in the msg object. This is the value you got when you click the small icon   >_



Testing flow:

[{"id":"125c4ac9.259475","type":"function","z":"8adada5a.8de1e8","name":"Extract Lat","func":"msg.payload = msg.payload.lat;\nreturn msg;","outputs":1,"noerr":0,"x":503.75,"y":245,"wires":[["63840b29.b23a34"]]},{"id":"4c0af6fa.939498","type":"function","z":"8adada5a.8de1e8","name":"Extract Long","func":"msg.payload = msg.payload.lon;\nreturn msg;","outputs":1,"noerr":0,"x":503.75,"y":285,"wires":[["63840b29.b23a34"]]},{"id":"63840b29.b23a34","type":"function","z":"8adada5a.8de1e8","name":"create_payload","func":"var lat = \"34.736729\";\nvar lng = \"-92.34977\";\nvar device_name = \"gps\";\nvar token = \"mysecrettoken\";\n\nvar payload = {};\n\npayload[\"position\"] = {\"value\": \"1\", \"context\": {\"lat\": lat, \"lng\": lng}};\nmsg.payload = payload;\nreturn msg;\n\n","outputs":1,"noerr":0,"x":713.75,"y":265,"wires":[["7846bf6d.3ee8b","ba86eced.1240d"]]},{"id":"862dc1af.b9929","type":"debug","z":"8adada5a.8de1e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1050.1251411437988,"y":270.000036239624,"wires":[]},{"id":"606b5806.9a1be8","type":"inject","z":"8adada5a.8de1e8","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":242.62500762939453,"y":256.25,"wires":[["125c4ac9.259475","4c0af6fa.939498"]]},{"id":"7846bf6d.3ee8b","type":"function","z":"8adada5a.8de1e8","name":"","func":"msg.payload = msg.payload.position.context.lat;\nreturn msg;\n\n","outputs":1,"noerr":0,"x":887.6251373291016,"y":268.7500114440918,"wires":[["862dc1af.b9929"]]},{"id":"ba86eced.1240d","type":"function","z":"8adada5a.8de1e8","name":"","func":"msg.payload = msg.payload.position.context.lng;\nreturn msg;","outputs":1,"noerr":0,"x":890,"y":316.25,"wires":[["896c897f.e089e8"]]},{"id":"896c897f.e089e8","type":"debug","z":"8adada5a.8de1e8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":1050,"y":320,"wires":[]}]

Dannaz Perth

unread,
Apr 24, 2018, 12:50:55 PM4/24/18
to Node-RED
Hi Collin.

I have an MQTT node pulling GPS coordinates.

These are the values i get from the object

_type: "location"
tid: "da"
acc: 19
batt: 62
conn: "w"
lat: -32.2913553
lon: 115.7274551
t: "u"
tst: 1524588360


I need to take the Lat & Lon values from this message and populate them into the final piece of code that sends the values to my Mqtt broker.

var lat = "34.736729";     /// Need to put the values here/ Replace?
var lng = "-92.34977";    /// Need to put the values here/Replace ?
var device_name = "gps";
var token = "5adf4073642ab6662a48b6c3";


var payload = {};

payload["position"] = {"value": "1", "context": {"lat": lat, "lng": lng}};     
msg.payload = payload;
return msg;



At the moment my code is sending dummy Lat& Lon values to my broker. I need to replace the dummy values with the real values.
I hope that makes sense ?

Thanks mate









Dannaz Perth

unread,
Apr 24, 2018, 1:11:34 PM4/24/18
to Node-RED
Hi AIOT.

Your suggestion returns the dummy values again.
I have isolated and extracted the Lat&Lon from the original message pulled from the Mqtt Node.
My goal is to now insert these "Real" values into the function nodes and to my broker.

The function node can be changed to make it work, my request for help is. how do i take my proper returned GPS values and replace the dummy values with them ?

Cheers mate







On Wednesday, 25 April 2018 00:11:34 UTC+8, Dannaz Perth wrote:

Dannaz Perth

unread,
Apr 24, 2018, 1:18:57 PM4/24/18
to Node-RED






On Wednesday, 25 April 2018 00:11:34 UTC+8, Dannaz Perth wrote:
Auto Generated Inline Image 1

Dannaz Perth

unread,
Apr 24, 2018, 1:19:45 PM4/24/18
to Node-RED





On Wednesday, 25 April 2018 00:11:34 UTC+8, Dannaz Perth wrote:
Auto Generated Inline Image 1

Mark Setrem

unread,
Apr 24, 2018, 1:22:41 PM4/24/18
to Node-RED

It’s not clear what your function does.
You create a blank object and then the next line try to treat it as an array

It would be easy for you to after each line write the output of that line to debug using node.warn ( see logging events https://nodered.org/docs/writing-functions#handling-errors)

You should then be able to determine which line isn’t doing what you think it is.

Nick O'Leary

unread,
Apr 24, 2018, 1:36:30 PM4/24/18
to node...@googlegroups.com
Dannaz,

From what you've shared, you should be able to do:

var lat = msg.payload.lat;
var lng = msg.payload.lon;


I also recommend reading through https://nodered.org/docs/user-guide/messages to help you understand how to work with messages.

Nick


--
http://nodered.org

Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send an email to node...@googlegroups.com.

Dannaz Perth

unread,
Apr 24, 2018, 1:55:57 PM4/24/18
to Node-RED
Hi Nick.

Thanks for getting back mate.

What you suggested was my very first approach.

var lat = msg.payload.lat;
var lng = msg.payload.lon;
var device_name = "gps";
var token = "Secret Token";

var payload = {};

payload["position"] = {"value": "1", "context": {"lat": lat, "lng": lon}};

msg.payload = payload;
return msg;


It is returning some errors.
4/25/2018, 1:53:34 AMnode: create_payloadfunction : (error)
"ReferenceError: lon is not defined (line 8, col 69)"
4/25/2018, 1:53:34 AMnode: create_payloadfunction : (error)
"TypeError: Cannot read property 'lat' of undefined"



I have done some serious reading. I'm a proper novice at this. TBH, I'm truly stuck.

This is what im using to extract from the Mqtt node, after a Json node.

msg.payload = msg.payload.lat;
return msg;

msg.payload = msg.payload.lng;
return msg;

Any help would be great ! :)





On Wednesday, 25 April 2018 00:11:34 UTC+8, Dannaz Perth wrote:

Nick O'Leary

unread,
Apr 24, 2018, 1:59:03 PM4/24/18
to node...@googlegroups.com
When you say :

> This is what im using to extract from the Mqtt node, after a Json node.

> msg.payload = msg.payload.lat;
> return msg;

> msg.payload = msg.payload.lng;
> return msg;

What do you mean? Are these two separate functions? What does your flow look like?

Nick


--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.

Dannaz Perth

unread,
Apr 24, 2018, 2:12:45 PM4/24/18
to Node-RED

Here is the code.

The 2 x Jsons connect to the incoming Mqtt Node
The create_payload connects to the outgoing Mqtt node


[{"id":"7996b6c2.7bb1a8","type":"function","z":"6693f5d1.6ac5e4","name":"Extract Lat","func":"msg.payload = msg.payload.lat;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":60,"wires":[["10fd0ddc.b0880a"]]},{"id":"5d68612c.c1fe08","type":"json","z":"6693f5d1.6ac5e4","name":"","property":"payload","action":"","pretty":false,"x":270,"y":60,"wires":[["7996b6c2.7bb1a8"]]},{"id":"8583df88.3f5b78","type":"json","z":"6693f5d1.6ac5e4","name":"","property":"payload","action":"","pretty":false,"x":270,"y":100,"wires":[["86a4f4a8.cf08"]]},{"id":"10fd0ddc.b0880a","type":"function","z":"6693f5d1.6ac5e4","name":"create_payload","func":"var lat = msg.payload.lat;\nvar lng = msg.payload.lon;\nvar device_name = \"gps\";\nvar token = \"secret\";\n\nvar payload = {};\n\npayload[\"position\"] = {\"value\": \"1\", \"context\": {\"lat\": lat, \"lng\": lng}};\nmsg.payload = payload;\nreturn msg;","outputs":1,"noerr":0,"x":740,"y":80,"wires":[["78f0aa36.64f574"]]},{"id":"86a4f4a8.cf08","type":"function","z":"6693f5d1.6ac5e4","name":"Extract Long","func":"msg.payload = msg.payload.lng;\nreturn msg;","outputs":1,"noerr":0,"x":450,"y":100,"wires":[["10fd0ddc.b0880a"]]}]






On Wednesday, 25 April 2018 00:11:34 UTC+8, Dannaz Perth wrote:

Zenofmud

unread,
Apr 24, 2018, 2:43:31 PM4/24/18
to node...@googlegroups.com
Is the json the same in your two path’s?

What is the json (put a debug node on the output of the json node to display it)?

 There may be an easier way to do what you want. For example, the two function nodes processing the json could be change node instead.

Also, the ‘create_payload’ function will be run twice - once for the latitude msg and once for the longitude msg. I don’t think you want to do that.

Nick O'Leary

unread,
Apr 24, 2018, 2:50:20 PM4/24/18
to Node-RED Mailing List
Dannaz,

in general, its a bad idea to split a flow into two separate branches as you then need a bunch of logic to bring them back together. IN this instance, those two branches aren't really helping matters.

Delete both of your Extract... functions and one of the JSON nodes. Then wire your remaining JSON node straight to the create_payload node:



This means, that once the msg reaches create_payload its payload will have all the properties of the message you want and my original suggestion should work.

One small caveat - there seems to be some inconsistence between msg.payload.lon and mst.payload.lng ... so you'll need to double check exactly which one your received message has and make sure you extract that.

Nick



--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

steve rickus

unread,
Apr 24, 2018, 4:55:38 PM4/24/18
to Node-RED
Dannaz,

The errors you saw were because of a typo (the variable you used is called "lng", not "lon"), so you were close to correct. But your question comes up frequently, and is easily answered, IF you use those two little buttons shown in the debug sidebar. When you expand your msg.payload object, and hover the mouse over the value you want, you will see the first button (looks like ">_") -- clicking that will copy the "path" to the value onto your clipboard. Then you can just paste it into your function code, where you have the test value.

in your example, this path would be the string "payload.lat" -- which is a bit confusing because the copied path will start with the last part of the object you are debugging ("msg.payload"). If you set the debug node to show the "Complete msg object", the path copied would be "msg.payload.lat". Nick et al, copying the path starting at the root of the object displayed would be a simple but (imo) more intuitive behavior.

If the input message has all the data you showed in your previous picture, and all you are trying to do is restructure the data, I would avoid any coding and just put the structure you want in a template node. Then use "mustache" syntax (the double curly braces) to substitute your values wherever you need them:

{
    "position": {
        "value": "1",
        "context": {
            "lat": {{payload.lat}},
            "lng": {{payload.lon}}
        }
    }
}

Notice that in the template node, the "msg." prefix is not needed. Make sure to set this template node to "Output as Parsed JSON", and you will get the right output...

Also, as Nick mentioned, your inputs and outputs seem to be mixing the terms "lon" and "lng" -- so make sure the downstream node is expecting the right one.
--
Steve

Dannaz Perth

unread,
Apr 25, 2018, 12:41:46 AM4/25/18
to Node-RED

Hello Nick.

I literally had to sleep last night as it was just before 3:00AM. i did send a thank then but it appears it didn't post to you.

With that said. Thank you so much for your help and advise mate:) I learned a great deal again last night. More in that last hour than i have in the last 5 weeks. Top Man :)

Mr Steve Rickus.

Thanks for your advise also mate. over the last year you and Nick have pretty much been my go to chaps.
I'm gonna read and digest your comments this week and practice a little.

Like you said there is, there is heaps you can do with minimal coding.  As much as i intend to learn, i don't think ill ever have the capacity to remember a whole new language.

Thanks again

Have a good week :)

Dan




On Wednesday, 25 April 2018 00:11:34 UTC+8, Dannaz Perth wrote:
Reply all
Reply to author
Forward
0 new messages