MQTT events on location update

Well, let’s start with this:

OK, so you have Node-Red. Cool. I have shown you examples of the data that gets output there, and provided information on how you access that.

So now you’re mixing automations in HA? I thought you wanted this in Node Red?

states.device_tracker.devicetracked.attributes.latitude
states.device_tracker.devicetracked.attributes.longitude

This is so different than what node-red gives you, which is what you originally mentioned, and I replied with examples of.

In Node-Red, you would access msg.data.new_state.attributes.latitude and msg.data.new_state.attributes.longitude

I might have misunderstood the OP:s initial question, I thought he just wanted to publish an mqtt message from HA with lat, lon and a time stamp on state change of his tracker.

1 Like

I don’t really know what he’s looking for, which is why I stated he was being vague.

Lay out what you have, what you want, and how you think you want to get there. It’s simple.

Mixing terminology and automation logic is confusing everyone. He kept saying that he wanted that data, I kept stating this was completely possible in node-red as alluded to in the first post, but then kept saying he didn’t know how to access the attributes.

It’s a circle of neverending confusion in this thread. If he had stated he was trying to use the HA automation and templating there, I would have known he wasn’t using node-red, but to state you have node-red and want to process data there…is just slightly confusing.

As @flamingm0e mentions, it’s not 100% clear what you want, but if understood you correct, the below will push the device tracker’s lat, lon and a time stamp through MQTT from a Home Assistant automation.

  action:
  - service: mqtt.publish
    data_template:
      topic: "my_location"
      payload: >-
        {
          "latitude": "{{ states.device_tracker.name_of_your_tracker.attributes.latitude }}",
          "longitude": "{{ states.device_tracker.name_of_your_tracker.attributes.longitude }}",
          "time": "{{ states.device_tracker.name_of_your_tracker.last_changed }}"
        }

I use a similar automation to push location to MQTT and then do some processing in a python script.

1 Like

This is exactly what I needed! Turns out I was not aware I had to put the word ‘attributes’ in there, which may be an oversight on my part. Thanks!

Wait, you’re thinking I’m asking for help with node red? Why would I ask that here?

Because you mentioned it in the first post, and when I continued to talk about node-red, you didn’t say anything! WTF. Seriously?

I mention node red on a HA forum, so my problem must be node red? Wouldn’t I then put the question on a node red forum? Why would I be talking about a state trigger and the device tracker?

Anyway, I assumed we were both talking about constructing a message on the HA side, and we weren’t. I’ll try to be more explicit next time.

Because people (like me) use node red for their automations??

There is a home assistant palette…it has ways to call services against your home assistant instance. When you mentioned node red, I thought you were trying to construct the message from a STATE TRIGGER in node red from your HA instance.

The device tracker can be used in node red, because node red can see everything your HA is doing…

I literally do ALL of my automations through node red and use home assistant for the state machine only.