Get HA to ask your iPhone where it is and put info in MQTT /Owntrack device_tracker

I have used sosumi to create a php program that gets the info from iFind and puts it into a MQTT feed under owntracks for HA to pickup. This might be useful to find the location / battery of your iphone when you / HA commands instead of relying on owntrack ios app or icloud device tracker. I originally put it into a command line sensor but the 1 minute scan interval kills my iphone battery. I am going to setup how many times it requests through automation based on location / battery level / time of day or request update through Alexa voice command / front end.

There isn’t much to share in terms of programming as I mainly copied and pasted. The formatting for the mqtt message took a bit and I ended up with this using phpMQTT.php. I posted the feed on mqtt to owntracks/my/device

$message = "{\"t\":\"u\",\"tst\":$tst,\"acc\":$acc,\"alt\":50,\"lon\":$long,\"_type\":\"location\",\"lat\":$lat,\"tid\":\"tt\",\"batt\":$bat}";

with these variables from sosumi

$lat = $loc["latitude"];
$long = $loc["longitude"];
$bat = $loc["batteryLevel"]*100;
$acc = $loc["accuracy"];
$tst = strtotime($loc["timestamp"]);
$tid = $loc["name"];
1 Like

@RobDYI do you have a complete solution on github I can look at? I’ve tried the iOS app (not reliable enough), icloud/icloud2 (polls too often) and wanted to give this a try.

If you get sosumi to work, here is the php script to retrieve iphone location and put it in mqtt as owntrack. You can run this script when you want to update location etc.

<?php
date_default_timezone_set('America/Los_Angeles');
include("class.sosumi.php");
require("phpMQTT.php");
$iTunesUsername = "[email protected]";
$iTunesPassword = "mypassword";
$deviceID = "ylCHk8qadfgfdgdfgfgarbagedYVNSUzmWV";  //iphone 8
$playSound = true;
$ssm = new Sosumi($iTunesUsername, $iTunesPassword);
$loc = $ssm->locate($deviceID);
$lat = $loc["latitude"];
$long = $loc["longitude"];
$bat = ceil($loc["batteryLevel"]*100);
$acc = $loc["accuracy"];
$tst = strtotime($loc["timestamp"]);
$tid = $loc["name"];
$tst2 = date("D M j g:i a" ); 
  $host = "192.168.2.150"; 
  $port = 1883;
  $clientid = "userid";
  $username = "hass"; 
  $password = "hass"; 
  $message = "{\"t\":\"u\",\"tst\":$tst,\"acc\":$acc,\"alt\":50,\"lon\":$long,\"_type\":\"location\",\"lat\":$lat,\"tid\":\"pp\",\"batt\":$bat}";

  $mqtt = new phpMQTT($host, $port, $clientid); 
  if ($mqtt->connect(true,NULL,$username,$password)) {
    $mqtt->publish("owntracks/person/ifind",$message, 0);
    $mqtt->close();
  }else{
    echo "Fail or time out<br />";
  }
1 Like

Sweet, much appreciated!

@RobDYI what is $deviceID represented by? I tried “Val’s”, the IMEI, Serial# etc.

EDIT: Did some googling and tried print_r($ssm->devices) but nothing is returned :frowning: guess this doesn’t work anymore.

EDIT: Found working version here:

run this php script (it works) and it will tell you all your device ids etc

<?php
include("class.sosumi.php");
$iTunesUsername = "uuuuuuu";
$iTunesPassword = "ppppppppppp";
$ssm = new Sosumi($iTunesUsername, $iTunesPassword);
print_r($ssm->devices); // Uncomment this line to view all your device id's

The class.sosumi.php from the tylerhall repo doesn’t return anything, I get errors and it loops forever. The updated class.sosumi.php from the repo above returns the device info. Not sure what’s different (I didn’t diff the two.) When I run the above PHP you provided with the original class file, I get this:

root@freenas:~/sosumi# php sosumi.other.php
Array
(
)

When I run it with the updated class file, I get this:

root@freenas:~/sosumi# php sosumi.other.php
PHP Notice:  Undefined variable: post in /root/sosumi/class.sosumi.php on line 41
Array
(
    [redacted] => SosumiDevice Object
        (
            [isLocating] => 1
            [locationTimestamp] => 2018-07-31 19:34:45
            [locationType] => GPS
            [horizontalAccuracy] => 10
            [locationFinished] => 1
            [longitude] => redacted
            [latitude] => redacted
            [deviceModel] => redacted
            [deviceStatus] => 203
            [id] => redacted
            [name] => Val's
            [deviceClass] => iPhone
            [chargingStatus] => Unknown
            [batteryLevel] => 0
        )

)

@RobDYI appreciate all the help!

I think the first php script I provided should work as it works for me. I first use

<?php
include("class.sosumi.php");
$iTunesUsername = "uuuuuuu";
$iTunesPassword = "ppppppppppp";
$ssm = new Sosumi($iTunesUsername, $iTunesPassword);
print_r($ssm->devices); // Uncomment this line to view all your device id's

and then I get the device id I want to put into owntrack. I then copy that id to this script.

    <?php
    date_default_timezone_set('America/Los_Angeles');
    include("class.sosumi.php");
    require("phpMQTT.php");


    $iTunesUsername = "[email protected]";
    $iTunesPassword = "mypassword";
    $deviceID = "ylCHk8qadfgfdgdfgfgarbagedYVNSUzmWV";  //iphone 8


    $playSound = true;
    $ssm = new Sosumi($iTunesUsername, $iTunesPassword);
    $loc = $ssm->locate($deviceID);
    $lat = $loc["latitude"];
    $long = $loc["longitude"];
    $bat = ceil($loc["batteryLevel"]*100);
    $acc = $loc["accuracy"];
    $tst = strtotime($loc["timestamp"]);
    $tid = $loc["name"];
    $tst2 = date("D M j g:i a" ); 
      $host = "192.168.2.150"; 
      $port = 1883;
      $clientid = "userid";
      $username = "hass"; 
      $password = "hass"; 
      $message = "{\"t\":\"u\",\"tst\":$tst,\"acc\":$acc,\"alt\":50,\"lon\":$long,\"_type\":\"location\",\"lat\":$lat,\"tid\":\"pp\",\"batt\":$bat}";

  $mqtt = new phpMQTT($host, $port, $clientid); 
  if ($mqtt->connect(true,NULL,$username,$password)) {
    $mqtt->publish("owntracks/person/ifind",$message, 0);
    $mqtt->close();
  }else{
    echo "Fail or time out<br />";
  }

which simply take the log and lat info and puts it into mqtt. Make sure you have the phpMQTT.php in the same directory

Also, make sure you put owntrack into configuration.yaml

device_tracker:
  - platform: owntracks
1 Like

Thanks Rob - will give this a whirl. I noticed with the iCloud component it can easily put my wife 30+ minutes behind myself (Android/Zanzito) so I plan to schedule a command line automation to run every 15 minutes for update. I assume this is similar to how you’re polling for location on your side?

Yes, I have it when its picked up on nmap component it doesn’t poll, when its charging it polls every 10 mins and when it not_home and not charging, it polls every 30 mins.

1 Like

Does this still work even if you have 2FA enabled? Looks like the tylerhall/sosumi project has gone five years without an update! I was thinking this might work well as a compansion to bluetooth tracking.

Yes, it will work with 2FA.