MQTT events on location update

I’m tracking my own location in two ways: Google Location Sharing and OwnTracks. OwnTracks eats quite a bit of battery when set to move mode, but it does work over MQTT, which makes my Node-RED happy. But I’m curious about Google Location Sharing, and would like to see how well it works.

My goal is to set up an automation that pushes location change events to the MQTT publish service, and have Node-RED on the other side comparing the delays, frequencies, and accuracy of the two methods. Unfortunately, I seem to be stuck.

I’ve think I can use the state trigger on the device tracker to call mqtt.publish, but I’m not sure how to construct the topic and payload. The end goal is to have it simply publish lat, lon, and a last updated timestamp of some sort.

I’ve looked into the eventstream, but that’s kinda overkill, places all messages with the retain-flag, and does not seem to publish updates, but rather sends data with a specific interval.

I’ve not received any reply, and I’ve not found anything to help me get a step further. Could anybody please take a look?

Use something better then.

Tasker or Ariela would be better than Owntracks.

Owntracks uses no battery on android (if you don’t set it to respond to every ‘move’), is more than accurate enough, and has native MQTT support. As far as I can tell, it has no downsides. Also, Owntracks isn’t the problem; the thing I’m stuck with is making the automation.

What exactly are you stuck on??

Creating an mqtt message?

That’s the most simple part.

But I don’t see why it’s needed. The device tracker handles all of that for you.

Yes, I’m stuck on creating a custom MQTT message.

The device tracker tracks devices well, by I want to do some more complicated stuff, which is where I run into the limitations of HA.

Essentially, I want the lat-lon to be made available over MQTT, without using the eventstream. You state it is simple, and I agree; it should be simple. I must be missing something very basic, hence my request for help.

I’m confused on how your more complicated stuff requires MQTT. The data is right there in the attributes, and you can literally do anything you want with it. Why are you trying to push it BACK to Home Assistant as MQTT?

I just want to know what you plan to do with that information, as I feel there is a much easier and appropriate way to handle this.

This is all the data that comes from GPS Logger, and is accessible via Node-Red.

This is all the data that comes from Owntracks, and is accessible via Node-Red

I’m trying to understand the benefit of sticking this back in Home Assistant as an MQTT message.

If you want to create an MQTT device tracker:

To publish on the mqtt bus:

So what part of this is where the confusion lies? When you call a service on Node-Red to Home Assistant, you can select JSON as the payload, and provide the information you need.

I’m not feeding it back into home assistant, I’m feeding it to another system over MQTT.

I have a functional device tracker in HA. I want an MQTT message when the data associated with this device tracker changes.

Sending an MQTT message in HA is easy but I want the message to contain lat-lon, or the values of variables of the device tracker. But I’m at a loss on how address those in a way that is compatible with the template.

Doesn’t the device tracker have the GPS location, which HAS the lat-lon?

Yes, and that’s what I want. How do I address this property for use in a template?

What do you mean how do you address this property?

It’s an attribute right?

It’s in the msg?

Yes.

I’m not grasping the way to address this attribute in the payload template.

I have provided examples of screenshots of the data that comes from my device trackers.

You will need to provide more details of what you don’t understand.

If your device tracker (FROM THE EVENT STATE NODE) provides a MSG, then you have access to EVERYTHING in that MSG.

What part is hard to understand? Can you help out by being clearer about what you see, and what you want to see, and what you think you cannot do?

I’m not sure how to be clearer, here.

The device tracker object has attributes. I want two of those attributes in an MQTT message. I do not know how to address these attributes in the MQTT template.

Object.attribute, with a slew of brackets, does not seem to work properly (returns empty payload), while other examples suggest it should.

As an example, let’s say I have a standard device tracker object named ‘john’, which has attributes ‘lat’ and ‘lon’. Those attributes I want to send over MQTT. What do I place in the MQTT template to accomplish that?

When you read the debug node it tells you everything is in msg.

Attributes lat and long would be accessed by msg.lat and msg.lon assuming they are directly in the msg and not the payload.

You’re being extremely vague. You could just show an example of what you see and I could tell you in half a second…

I’m not happy with you stating I’m being ‘extremely vague’, but I do see my issue is not coming across.

So, I’m going through the steps as they’re outlined in the automation editor in the settings (I’m liking this new UI-based approach).

As a test, I’ve created a new automation called ‘publish location’.
It has a single trigger, which has State as the trigger type. The entity is device_tracker.google_maps_0000000000000000000 (the string of zeroes is some form of a Google ID, I’m guessing). Nothing else is specified, and this seems to reliably trigger on every state change, which corresponds to a location update as HA sees it.

Now, under actions, I’ve told it to Call service, and it calls mqtt.publish. This also works; if I don’t set anything else, I get empty MQTT messages that seem to correspond with location updates.

For now, I’ve set Service data to

{
  "payload_template": "{{ states('device_tracker.google_maps_0000000000000000000')}} ",
  "qos": 0,
  "retain": false,
  "topic": "location/casper/ha"
}

Where payload_template is the crucial part. I want it to place the lat-lon in there, so I want it to address the attributes.
So, I thought to simply do:

"payload_template": "{{ states('device_tracker.google_maps_0000000000000000000.latitude')}} ",

But this does not give me a response, nor does it give me a way to put both lat and lon in there.

My question is, as before, simple:
How do I address the attributes in such a way that the payload_template can understand it, pick it up, plop it in the message, and shoot it towards the broker?

Not sure if this helps, but If you have a tracker entity, the following will give you the latitude:

{{ states.device_tracker.name_of_your_tracker_entity.attributes.latitude }}

And this gives you latitude, longitude, and a time stamp:

{"latitude":{{ states.device_tracker.name_of_your_tracker.attributes.latitude}}, 
"longitude":{{ states.device_tracker.name_of_your_tracker.attributes.longitude }},
 "time_stamp":{{ states.device_tracker.name_of_your_tracker.last_changed }}}

The result in my template editor for the above is: (I removed the actual coordinates)

{"latitude": xx.1053768, "longitude": xx.0453918, "time_stamp":2019-09-24 16:46:27.764333+00:00}

which might work as an mqtt payload.