GPSlogger extract Lat and Lon to compare to GEOfence

I have reached my level of understanding. I believe I need a funcion to do this task, but I have no idea how to write it. Could someone please help with the below?

NB: There may be a lot simpler way to achieve this…

https://m.imgur.com/a4KSRe4?r

I have an app on my phone GPSlogger (Android)

I am trying to compare its GPS location with Node Red Geofence Locations.

Below is the Full msg I can see. I would like to extract the GSP Data to compare it to the Inside or Outside geofence locations.

Thanks

Hans

{“topic”:“device_tracker.hans_gpslogger”,“payload”:“home”,“data”:{“entity_id”:“device_tracker.hans_gpslogger”,“old_state”:{“entity_id”:“device_tracker.hans_gpslogger”,“state”:“home”,“attributes”:{“source_type”:“gps”,“latitude”:-23.962159,“longitude”:135.0735714,“gps_accuracy”:22,“battery”:35,“speed”:0,“direction”:0,“altitude”:52.400001525878906,“provider”:“network”,“activity”:"",“friendly_name”:“hans gpslogger”},“last_changed”:“2018-06-24T12:15:31.055553+00:00”,“last_updated”:“2018-06-24T12:29:57.684507+00:00”},“new_state”:{“entity_id”:“device_tracker.hans_gpslogger”,“state”:“home”,“attributes”:{“source_type”:“gps”,“latitude”:-23.962159,“longitude”:135.0735714,“gps_accuracy”:22,“battery”:35,“speed”:0,“direction”:0,“altitude”:52.400001525878906,“provider”:“network”,“activity”:"",“friendly_name”:“hans gpslogger”},“last_changed”:“2018-06-24T12:15:31.055553+00:00”,“last_updated”:“2018-06-24T12:31:00.039979+00:00”}},"_msgid":“b5ee0937.997978”}

Take a look at node-red-node-geofence

I have, however, the geofence does not seem to take the geologger data (as posted above) the payload is either home or not home.

I believe I need to have the payload be converted to

msg.location.lat=msg.data.new_state.attributes.latitude
msg.location.lon=msg.data.new_state.attributes.longitude

But my java skills are too limited.

You can use the debug sidebar window to help you to know how to extract the required data.

First thing is to feed your data into a debug node and trigger the flow.

I did this using the attached flow. By the way, in this forum you need to surround code with 3 backticks in order to allow one to extract the code (or JSON data) for analysis.

[{"id":"5f94bdd.434a644","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"e4f9b076.c2873","type":"inject","z":"5f94bdd.434a644","name":"","topic":"","payload":"{\"topic\":\"device_tracker.hans_gpslogger\",\"payload\":\"home\",\"data\":{\"entity_id\":\"device_tracker.hans_gpslogger\",\"old_state\":{\"entity_id\":\"device_tracker.hans_gpslogger\",\"state\":\"home\",\"attributes\":{\"source_type\":\"gps\",\"latitude\":-23.962159,\"longitude\":135.0735714,\"gps_accuracy\":22,\"battery\":35,\"speed\":0,\"direction\":0,\"altitude\":52.400001525878906,\"provider\":\"network\",\"activity\":\"\",\"friendly_name\":\"hans gpslogger\"},\"last_changed\":\"2018-06-24T12:15:31.055553+00:00\",\"last_updated\":\"2018-06-24T12:29:57.684507+00:00\"},\"new_state\":{\"entity_id\":\"device_tracker.hans_gpslogger\",\"state\":\"home\",\"attributes\":{\"source_type\":\"gps\",\"latitude\":-23.962159,\"longitude\":135.0735714,\"gps_accuracy\":22,\"battery\":35,\"speed\":0,\"direction\":0,\"altitude\":52.400001525878906,\"provider\":\"network\",\"activity\":\"\",\"friendly_name\":\"hans gpslogger\"},\"last_changed\":\"2018-06-24T12:15:31.055553+00:00\",\"last_updated\":\"2018-06-24T12:31:00.039979+00:00\"}},\"_msgid\":\"b5ee0937.997978\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":174.10000610351562,"y":178,"wires":[["ff9590a9.83b88"]]},{"id":"ff9590a9.83b88","type":"debug","z":"5f94bdd.434a644","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":352.100004196167,"y":176.00000190734863,"wires":[]},{"id":"14602b01.647bb5","type":"inject","z":"5f94bdd.434a644","name":"","topic":"","payload":"{\"topic\":\"device_tracker.hans_gpslogger\",\"payload\":\"home\",\"data\":{\"entity_id\":\"device_tracker.hans_gpslogger\",\"old_state\":{\"entity_id\":\"device_tracker.hans_gpslogger\",\"state\":\"home\",\"attributes\":{\"source_type\":\"gps\",\"latitude\":-23.962159,\"longitude\":135.0735714,\"gps_accuracy\":22,\"battery\":35,\"speed\":0,\"direction\":0,\"altitude\":52.400001525878906,\"provider\":\"network\",\"activity\":\"\",\"friendly_name\":\"hans gpslogger\"},\"last_changed\":\"2018-06-24T12:15:31.055553+00:00\",\"last_updated\":\"2018-06-24T12:29:57.684507+00:00\"},\"new_state\":{\"entity_id\":\"device_tracker.hans_gpslogger\",\"state\":\"home\",\"attributes\":{\"source_type\":\"gps\",\"latitude\":-23.962159,\"longitude\":135.0735714,\"gps_accuracy\":22,\"battery\":35,\"speed\":0,\"direction\":0,\"altitude\":52.400001525878906,\"provider\":\"network\",\"activity\":\"\",\"friendly_name\":\"hans gpslogger\"},\"last_changed\":\"2018-06-24T12:15:31.055553+00:00\",\"last_updated\":\"2018-06-24T12:31:00.039979+00:00\"}},\"_msgid\":\"b5ee0937.997978\"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":168,"y":280,"wires":[["987d1958.0e0978"]]},{"id":"987d1958.0e0978","type":"change","z":"5f94bdd.434a644","name":"","rules":[{"t":"set","p":"oldlatitude","pt":"msg","to":"payload.data.old_state.attributes.latitude","tot":"msg"},{"t":"set","p":"oldlongitude","pt":"msg","to":"payload.data.old_state.attributes.longitude","tot":"msg"},{"t":"set","p":"newlatitude","pt":"msg","to":"payload.data.new_state.attributes.latitude","tot":"msg"},{"t":"set","p":"newlongitude","pt":"msg","to":"payload.data.new_state.attributes.longitude","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":326.1000061035156,"y":280,"wires":[["7730e280.b8b43c"]]},{"id":"7730e280.b8b43c","type":"debug","z":"5f94bdd.434a644","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":539,"y":282,"wires":[]}]

We can see the result in the debug side panel. The small icon >_ that appears when you hover your mouse in the lines of the debug indicates that you have latitude and longitude in four different places in the JSON structure:

payload.data.old_state.attributes.latitude
payload.data.old_state.attributes.longitude

payload.data.new_state.attributes.latitude
payload.data.new_state.attributes.longitude

You can use above paths to extract the data into the msg object, like I did in the flow attached above. Actually there is no need to write code for this, just using a change node will do the trick.

r-002

Andrei

Thanks for the reply.

Do you have an example of the change node?

Give me 10 mins, I think Im getting it…

Hans

OK…

Your example flow works perfectly.

For example, when I copy the path from your flow I get

payload.data.old_state.attributes.latitude

However, when I copy it in my flow (from GPS Logger) is get

data.old_state.attributes.latitude

(No payload at the start)

When I copy your change node, there is no change with either line when I try my flow.

[{"id":"ee28ef7a.93de","type":"tab","label":"Flow 6","disabled":false,"info":""},{"id":"59355a5a.a5cc44","type":"server-state-changed","z":"ee28ef7a.93de","name":"GPS Logger","server":"e61b9780.2cad38","entityidfilter":"device_tracker.hans_gpslogger","entityidfiltertype":"substring","haltifstate":"","x":130,"y":180,"wires":[["ed65e80f.c1a248","6189fc1a.62f844"]]},{"id":"1d13be23.de2bb2","type":"debug","z":"ee28ef7a.93de","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":340,"y":240,"wires":[]},{"id":"ed65e80f.c1a248","type":"change","z":"ee28ef7a.93de","name":"4 rules","rules":[{"t":"set","p":"oldlatitude","pt":"msg","to":"payload.data.old_state.attributes.latitude","tot":"msg"},{"t":"set","p":"oldlongitude","pt":"msg","to":"payload.data.old_state.attributes.latitude","tot":"msg"},{"t":"set","p":"lat","pt":"msg","to":"payload.data.new_state.attributes.latitude","tot":"msg"},{"t":"set","p":"lon","pt":"msg","to":"payload.data.new_state.attributes.longitude","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":110,"y":300,"wires":[["1d13be23.de2bb2"]]},{"id":"6189fc1a.62f844","type":"debug","z":"ee28ef7a.93de","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":330,"y":120,"wires":[]},{"id":"e61b9780.2cad38","type":"server","z":"","name":"Home Assistant","url":"http://192.168.1.38:8123","pass":"s3cr3t"}]

The difference in paths from the debug is due to how you have them configured.
I’m guessing your debug node is set to “Output msg.payload” where as the other will be configured to “Output complete msg object”

Hi, indeed I have explained how to extract data from the JSON but it is necessary to suit the extracted data to the specifics of your flow. I have chosen random property names like msg.oldlatitude, msg.newlatitude, etc.. probably your downstream nodes are expecting a different kind of payload (perhaps something as simple as msg.payload). I was unable to import your latest flow as you are using a node that is unknown to my node-red.

r-005

I’m actually thinking that there is an issue with my version of node red.

I am using “Output complete msg object” but the path does not include the payload part.

I might try a restart and see if I can get it working. The explanations have been very good and it should be straightforward.

Ill do some investigating.

Thanks

If you are showing the complete msg object, and the copied path does not include “payload”, then your incoming data is not under msg.payload – which is not typical, but still valid. It just depends upon what data structure the custom node outputs.