iOS app to push to MQTT

Just moved my presence detection over to the iOS app after using Owntracks or a while. I am also using OT Recorder to log my locations points. Owntracks publishes the location data into an MQTT server which allows OT Recorder to capture that message and log the location.

Would be nice if the HA iOS app published to an MQTT server as well.

Why don’t you build something like that with an automation in HA?

Don’t know how to capture current lat/long from the iOS app.

I am using HA for a few months and just yesterday I found the “Templates” sections in the Developer Tools:
http://YOUR-IP-HERE:8123/dev-template

This is a great starting point to figure out the attributes of states. So this will give you an overview over all the stuff you can get from the device tracker object:

{{ states.device_tracker.<YOUR-DEVICE-ID> }}

If you just want the values of lat/long, you can use these:

{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.latitude }}
{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.longitude }}

But it might be better to get all of the information/attributes in the device tracker object:

{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes }}

I haven’t tested it yet, but this might help you:

automation:
  - alias: ios_to_mqtt
    trigger:
      platform: state
      entity_id: device_tracker.<YOUR-DEVICE-ID>
    action:
     - service: mqtt.publish
       data_template:
         topic: 'devices/<YOUR-DEVICE-ID>'
         retain: true
         payload: '{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes }}'

Please give feedback, if it is working properly, so others can use it. :slight_smile:

1 Like

Very cool. Thank you.

Tested it.

There was some spacing reformatting required but in general works. It publishes to the MQTT server. Problem is the format of the payload is different to the format required for OT recorder.

Owntracks format is {“batt”:61,“lon”:xxx.7804054036401,“acc”:65,“p”:100.5022277832031,“vac”:10,“lat”:xxx.337309852767826,“t”:“p”,“conn”:“w”,“tst”:1492664080,“alt”:25,"_type":“location”,“tid”:“sc”}

HA iOS app format is
{‘source_type’: ‘gps’, ‘battery’: 28, ‘friendly_name’: ‘sams iphone6’, ‘gps_accuracy’: 30, ‘longitude’: xxx.7807049416972, ‘latitude’: xxx.337278001011663}

Is there a way to just extract the latitude and longitude. I can set fixed values for the other attributes as I don’t believe OT recorder needs them. - We also need the time stamp in unix epoch time format.

Looked at the dev console you pointed to… figured out how to get attributes.
{{ states.device_tracker.sams_iphone6.attributes.latitude }}

yes! :slight_smile:

automation:
  - alias: ios_to_mqtt
    trigger:
      platform: state
      entity_id: device_tracker.<YOUR-DEVICE-ID>
    action:
     - service: mqtt.publish
       data_template:
         topic: 'devices/<YOUR-DEVICE-ID>'
         retain: true
         payload: '{
                     "batt":"{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.battery }}",
                     "lon": "{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.longitude }}",
                     "lat": "{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.latitude }}",
                     "acc": "{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.gps_accuracy }}",
                     "tst": "{{ as_timestamp(now()) }}"
                  }'

I found it here https://home-assistant.io/docs/configuration/templating/#sensor-states

1 Like

getting there on time stamp

{{as_timestamp(now())}} will produce 1492676847.352687

what I need is the first part before the decimal point. Haven’t figured that out yet. Any ideas ?

This should work out:

automation:
  - alias: ios_to_mqtt
    trigger:
      platform: state
      entity_id: device_tracker.<YOUR-DEVICE-ID>
    action:
     - service: mqtt.publish
       data_template:
         topic: 'devices/<YOUR-DEVICE-ID>'
         retain: true
         payload: '{
                     "batt":"{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.battery }}",
                     "lon": "{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.longitude }}",
                     "lat": "{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.latitude }}",
                     "acc": "{{ states.device_tracker.<YOUR-DEVICE-ID>.attributes.gps_accuracy }}",
                     "tst": "{{ as_timestamp(now()) | int }}"
                  }'
1 Like

If using cloudmqtt then there would no need to expose your HA on the internet to receive location updates.
I think this capability would be nice.

hm well … I think one of the great purposes of HomeAssistant is, that everyone can control where the gathered data should go. Personally, I don’t want CloudMQTT to have any of my location data. I can’t see any encryption of the data or am I wrong?

1 Like

Kind of a bit wrong.
Cloudmqtt is an aws hosted mqtt platform with a free tier.
https://www.cloudmqtt.com
You own all the security credentials etc

So CloudMQTT is using Mosquitto as their MQTT server and the connection can be secured via TLS/SSL, right?
But the content of your messages will be processed unencrypted on the AWS server. That might not be a big deal, but I definitely understand if one wants to leave as less (unencrypted) data on other servers as it is possible.

ParanoiaMode: on
:see_no_evil:

Yes its essentially a personal cloud instance of mosquitto, and messaging is TLS secured.
I agree that if you’re wearing your tin foil hat then you may prefer not to go there, but this is the same for all cloud services… you need to weight the cost/convenience/privacy/effort etc criteria to make a choice vs rolling your own.

1 Like

juka

Many thanks for you help

Got the following automation working nicely.

- alias: ios_to_mqtt
  trigger:
    platform: state
    entity_id: device_tracker.sams_iphone6
  action:
    - service: mqtt.publish
      data_template:
        topic: 'owntracks/phone/sams-iphone'
        retain: true
        payload: '
                    {"batt":{{ states.device_tracker.sams_iphone6.attributes.battery }},
                    "lon":{{ states.device_tracker.sams_iphone6.attributes.longitude }},
                    "acc":{{ states.device_tracker.sams_iphone6.attributes.gps_accuracy }},
                    "p":100.5022277832031,
                    "vac":10,
                    "lat":{{ states.device_tracker.sams_iphone6.attributes.latitude }},
                    "t":"b",
                    "conn":"w",
                    "tst": {{ as_timestamp(now()) | int }},
                    "alt":25,
                    "_type":"location",
                    "tid":"sc"}
                 '

Would be nice to get the hard coded variables above being provided by the iOS app but not essential.

Also wanted to note that I think the HA iOS app is less likely to report false locations - Owntracks seemed jump around to different locations even when stationary.

3 Likes

Perfect :ok_hand: Thanks for the update!

Exactly! That sums it up very well.

I just worked up this for my wife’s iPhone. I realized a few days ago that OwnTracks wasn’t updating properly on iOS 11. Luckily I found this post, and so far it looks like it’s doing the trick. I’ll remove OwnTracks from her phone and test the iOS updates when she goes out tonight. I should still be able to view her location in OwnTracks on my Android phone (which is useful if her vehicle has a problem and I need to get to her).