Find My iPhone implementation in LUA script

Moderator: leecollings

Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

cattoo wrote:The file is located here
json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")(
And I've checked and the JSON file is there.
Hard to say anything else based on the info available - other than json decoding would seem to be the issue and that is where I'd put my troubleshooting efforts in the first place (i.e. make sure decoding works) :)

It should be either some issue with JSON.lua (you can download a new version e.g. here http://regex.info/code/JSON.lua) or with the "result" not being a valid json format.

edit: I assume the missing closing parenthesis is just a copy/paste error ==> json = (loadfile "/home/pi/domoticz/scripts/lua/JSON.lua")(
pega20
Posts: 6
Joined: Monday 06 March 2017 8:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 »

Hi
Thats the error i got. I didn't add or remove any lines from the script. Any suggestions?

2017-03-06 08:51:01.382 Error: EventSystem: in script_time_checkphones: [string "-- Script to check the location of multiple i..."]:40: attempt to concatenate global 'stage2server' (a nil value)
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

pega20 wrote:Hi
Thats the error i got. I didn't add or remove any lines from the script. Any suggestions?

2017-03-06 08:51:01.382 Error: EventSystem: in script_time_checkphones: [string "-- Script to check the location of multiple i..."]:40: attempt to concatenate global 'stage2server' (a nil value)
The first post has not been updated and Apple changed the login procedure a while back. The a couple of pages back in this thread, e.g. https://www.domoticz.com/forum/viewtopi ... 40#p114362

:)
pega20
Posts: 6
Joined: Monday 06 March 2017 8:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 »

Nautilus wrote:
pega20 wrote:Hi
Thats the error i got. I didn't add or remove any lines from the script. Any suggestions?

2017-03-06 08:51:01.382 Error: EventSystem: in script_time_checkphones: [string "-- Script to check the location of multiple i..."]:40: attempt to concatenate global 'stage2server' (a nil value)
The first post has not been updated and Apple changed the login procedure a while back. The a couple of pages back in this thread, e.g. https://www.domoticz.com/forum/viewtopi ... 40#p114362

:)
Thanks
I tested the last one you linked to. Now I got a new error:

2017-03-06 11:19:02.164 Error: EventSystem: in script_time_checkphones: [string "-- Script to check the location of multiple i..."]:47: bad argument #1 to 'match' (string expected, got nil)
Nytt punktgrafikkbilde.jpg
Nytt punktgrafikkbilde.jpg (316.86 KiB) Viewed 4988 times
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

What do you think it could be? :)

There should be a dummy text sensor with name 'Position User' (where User is the real name of the user you've provided at the beginning of the script). It looks like there isn't one....
pega20
Posts: 6
Joined: Monday 06 March 2017 8:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 »

Nautilus wrote:What do you think it could be? :)

There should be a dummy text sensor with name 'Position User' (where User is the real name of the user you've provided at the beginning of the script). It looks like there isn't one....

I forgot that I had deleted the dummytext ;-)

Thanks
pega20
Posts: 6
Joined: Monday 06 March 2017 8:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 »

No error messages but does not get any values in dummy text device. Have tried print (result) after local result = act: read ("* a"). Nothing appears in the log file. someone ?
Running domoticz on asustor.
cattoo
Posts: 60
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi / ODroid
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Find My iPhone implementation in LUA script

Post by cattoo »

Now is everything working for me, but ill have 7 enteries within a minute that report that my iPhone is "home". What can ill do so its getting one entry instead of 7. Ive had the lua set as "ALL" & "TIME, but it was the same.
htca
Posts: 21
Joined: Thursday 28 January 2016 10:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by htca »

I switched off 2-way authentication and get this error:

Code: Select all

2017-03-08 13:08:01.879 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:44: attempt to concatenate field '?' (a nil value)
My line 44:

Code: Select all

table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['iPhone Battery ' .. user] .. '|0|' .. bat})
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

pega20 wrote:No error messages but does not get any values in dummy text device. Have tried print (result) after local result = act: read ("* a"). Nothing appears in the log file. someone ?
Running domoticz on asustor.
If you just run the curl command from command line does it give some error or retrieve the json output?
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

cattoo wrote:Now is everything working for me, but ill have 7 enteries within a minute that report that my iPhone is "home". What can ill do so its getting one entry instead of 7. Ive had the lua set as "ALL" & "TIME, but it was the same.
"time" is correct script type. Then it runs max once per minute. Depending which kind of condition you use, you can make it run in a longer interval. Also, I'd suggest adding some condition(s) to prevent the update of the switches if the information does not change. Especially for the "at home" on/off switch but also the text sensor.
htca
Posts: 21
Joined: Thursday 28 January 2016 10:32
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by htca »

htca wrote:I switched off 2-way authentication and get this error:

Code: Select all

2017-03-08 13:08:01.879 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:44: attempt to concatenate field '?' (a nil value)
My line 44:

Code: Select all

table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['iPhone Battery ' .. user] .. '|0|' .. bat})
I think I found it, it is related to the dummy devices. Which ones (and types) need to be made?
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

htca wrote:I switched off 2-way authentication and get this error:

Code: Select all

2017-03-08 13:08:01.879 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:44: attempt to concatenate field '?' (a nil value)
My line 44:

Code: Select all

table.insert(commandArray,{['UpdateDevice'] = otherdevices_idx['iPhone Battery ' .. user] .. '|0|' .. bat})
I haven't used that kind of syntax for adding commands to commandArray, but if it was working before, then it should be ok. I'd start with printing out the stuff that is happening on line 44. I.e. print(bat)
print(otherdevices_idx['iPhone Battery ' .. user])

If there is something weird going on then continue there (e.g. by testing the curl from command line etc).
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

htca wrote: I think I found it, it is related to the dummy devices. Which ones (and types) need to be made?
You need at least the on/off switch, text sensor and percentage sensor if you want to log the present/not present, address and battery level. But this of course depends if you want to log also something else or do not need all of these. Be aware of the naming. E.g. percentage sensor for battery level need to be named "iPhone Battery user" where user is the actual user value from the first array. Unless you change this "['iPhone Battery ' .. user]" to something else. Same story with on/off and custom sensor, look at the script for how they need to be named or adjust the script so that it follows your naming logic...:)
pega20
Posts: 6
Joined: Monday 06 March 2017 8:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 »

Nautilus wrote:
pega20 wrote:No error messages but does not get any values in dummy text device. Have tried print (result) after local result = act: read ("* a"). Nothing appears in the log file. someone ?
Running domoticz on asustor.
If you just run the curl command from command line does it give some error or retrieve the json output?


Her is what i got


curl -s https://maps.googleapis.com/maps/api/ge ... on?latlng=" .. latitude .. "," .. longitude .. "&sensor=false
root@Hjemme:/volume1/.@root # <!DOCTYPE html>
<html lang=en>
<meta charset=utf-8>
<meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-wi dth">
<title>Error 400 (Bad Request)!!1</title>
<style>
*{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{backgrou nd:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height :180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/error s/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflo w:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (m ax-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0 }}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo _color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-reso lution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/ 2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:ur l(//www.google.com/images/branding/googlelo ... 0x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:ur l(//www.google.com/images/branding/googlelo ... 0x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:5 4px;width:150px}
</style>
<a href=//www.google.com/><span id=logo aria-label=Google></span></a>
<p><b>400.</b> <ins>That’s an error.</ins>
<p>Your client has issued a malformed or illegal request. <ins>That’s all we know.</ins>
Nautilus
Posts: 722
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi / ODroid
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus »

pega20 wrote:
Her is what i got
First of all - you cannot use the variables in the command line, you need to replace them with the actual values. Second of all, the first step would be to see what the iCloud request returns, it is the "getlocation = curl..." line. But as metioned replace '"..credentials.username..":"..credentials.password.."' with your actual values, when you run this from command line there is no way curl would know what these variables should translate to...:)

So it should look something like this:

Code: Select all

curl -s -X POST -L -u 'pega20:123456' -H 'Content-Type: application/json; charset=utf-8' -H 'X-Apple-Find-Api-Ver: 2.0' -H 'X-Apple-Authscheme: UserIdGuest' -H 'X-Apple-Realm-Support: 1.0' -H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' -H 'X-Client-Name: iPad' -H 'X-Client-UUID: DELETED' -H 'Accept-Language: en-us' -H 'Connection: keep-alive' 'https://fmipmobile.icloud.com/fmipservice/device/USERNAME/initClient'
(again, I don't know your actual username and password so pega20 / 123456 are there as an example).

Now, this should return a json string that contains a lot of information, most importantly your iPhone's coordinates (latitude & longitude) which you use later in the "address" function. It was the curl command from this function that you quoted above...
pega20
Posts: 6
Joined: Monday 06 March 2017 8:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 »

Nautilus wrote:
pega20 wrote:
Her is what i got
Now, this should return a json string that contains a lot of information, most importantly your iPhone's coordinates (latitude & longitude) which you use later in the "address" function. It was the curl command from this function that you quoted above...

Thank you for helping me.

I got all the information but not the battery when I run from the command line.


Looks like I can not log into iCloud with

users = {
            Peg = {username = "[email protected]"; password = "12345"; device name = "iPhone Pega"};

I must use

users = {
            Peg = {username = "[email protected]"; password = "12345"; device name = "Pega"};

Everything is in order now so once again thank you very much

Peg
Wilco
Posts: 18
Joined: Monday 20 March 2017 15:38
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by Wilco »

for the following error:

2017-03-06 08:51:01.382 Error: EventSystem: in script_time_checkphones: [string "-- Script to check the location of multiple i..."]:40: attempt to concatenate global 'stage2server' (a nil value)

change line 40 in

stage2server = 'fmipmobile.icloud.com'
User avatar
htilburgs
Posts: 464
Joined: Tuesday 03 November 2015 11:01
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by htilburgs »

Is there way to control which form of notification the script will send?
I can't find it.
if distance < radius then
if otherdevices['iPhone ' .. user] == 'Off' then
commandArray['iPhone ' .. user] = 'On'
table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' thuis'})
end
else
if otherdevices['iPhone ' .. user] == 'On' then
commandArray['iPhone ' .. user] = 'Off'
table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' weg'})
end
end
Now I'm getting a notification in Telegram and in e-mail.
I only like to receive a notification in Telegram, thats enough....
Hardware:
RPi3 - Aeon Labs Z-Stick GEN5 - Fibaro Dimmer 2 - Fibaro Roller Shutter 2 - Fibaro Smoke Sensor - Yeelight RGBW Led bulb - Yeelight Smart LED Light Strip - Neo Coolcam PIR Motion Sensor - Neo Coolcam PowerPlug - Nest Thermostat v3
mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by mvzut »

htilburgs wrote:Is there way to control which form of notification the script will send?
I can't find it.
if distance < radius then
if otherdevices['iPhone ' .. user] == 'Off' then
commandArray['iPhone ' .. user] = 'On'
table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' thuis'})
end
else
if otherdevices['iPhone ' .. user] == 'On' then
commandArray['iPhone ' .. user] = 'Off'
table.insert(commandArray, {['SendNotification'] = 'Presence update#' .. user .. ' weg'})
end
end
Now I'm getting a notification in Telegram and in e-mail.
I only like to receive a notification in Telegram, thats enough....
You can remove the line with the SendNotification command from the script, and add individual notifications to the virtual switches for each phone. In this way you have control over the notification services that are used.
Raspberry Pi 4 - RFXtrx433 - CC2531 Zigbee - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - 6 x Sonos - 4 x IP cameras - Wall mounted tablet + Dashticz - Google Home integration - MANY switches/sensors
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests