Help for newbie

Apologies for the simple question. I know it will probably have a basic answer. I tried looking over the forum but I am sure I am not searching for the right thing.
I want to show a phone's position on a map. The map node requires this format:

{"name":"Joe", "lat":51, "lon":-1.05}

I am able to get the information I need with this code:


var lat = { payload: msg.payload.iPhone[1].locationInfo.latitude };
var long = { payload: msg.payload.iPhone[1].locationInfo.longitude };
var alt = { payload: msg.payload.iPhone[1].locationInfo.altitude };

return msg;

What I am failing to find out and thus learn how to do is how to write the return message so that the map node gets the information it needs. Any help would be appreciated. Glad for a point in the right direction. I have no coding experience.

var lat = msg.payload.iPhone[1].locationInfo.latitude;
var lon = msg.payload.iPhone[1].locationInfo.longitude;
var alt = msg.payload.iPhone[1].locationInfo.altitude;
msg.payload = {
  "name":"Joe", 
  "lat":lat , 
  "lon":lon,
  "alt": alt 
}
return msg;
1 Like

@Steve-Mcl Thank you very much. Will try soon.

Getting this on the debug window:

Screen Shot 2021-03-03 at 6.38.29 PM

Seems like I am getting really close but getting no icon on map yet. Just a quick question, do you think that because the lat and lon are objects has anything to do with it?.

I understand better now how to get information into the payload.

Code:

var lat = { payload: msg.payload.iPhone[1].locationInfo.latitude };
var lon = { payload: msg.payload.iPhone[1].locationInfo.longitude };

msg.payload = {
  "name": "circle",
    "lat": lat,
    "lon": lon,
    "icon": "bullseye",
    "iconColor": '#931C29',
}

return msg;

Yes.

Expand them, see where the real value is, then adjust the function to take them properties into the payload instead of their parent object.

scratch that ↑

Why did you put the values into objects?....

I gave you the working code...

Right!!. I missed this completely. Got excited about your answer and neglected to read carefully. Sorry. Thank you so much for your help and your time. It works perfectly now.

@Steve-Mcl I hope I am not taking advantage of your generosity. It is all working great except for displaying the battery level of an Apple Watch. I get this error:

TypeError: Cannot read property '2' of undefined

I am able to display battery level of phones which path is like this:

payload.iPhone[1].batteryLevel

But the watch's path is like this:

payload["Apple Watch"][2].batteryLevel

The gauge node for the watch looks this this now:

because the node for the iPhone looks like this and it is working:

show me a debug output containing these values (expand all properties)

26 devices on the Apple Find Me node. expanded the one for the watch. Assume you don't want everything.

Not sure why it doesnt work.

Instead, you can just move the values somewhere more accessible.

add a change node between set it to copy the values of interest to somewhere more sensible...

image

then use {{batteryLevel}} and {{displayName}} in your ui items

Worked!. Thank you so much again for all your help. Know enough to get started but not enough to get out of trouble. Like to learn by doing but get stuck sometimes. Much appreciated.

I recommend watching this playlist: Node-RED Essentials. The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

1 Like

Wise advice. Will watch this weekend. Thank you again

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