Find My iPhone implementation in LUA script

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

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Wednesday 01 March 2017 15:54

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 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 » Monday 06 March 2017 10:02

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: 751
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Monday 06 March 2017 11:49

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 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 » Monday 06 March 2017 12:32

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 2553 times

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

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Monday 06 March 2017 12:40

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 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 » Monday 06 March 2017 14:26

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 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 » Monday 06 March 2017 18:33

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: 66
Joined: Sunday 20 September 2015 17:37
Target OS: Raspberry Pi
Domoticz version: 2.3072
Location: Sweden
Contact:

Re: Find My iPhone implementation in LUA script

Post by cattoo » Monday 06 March 2017 20:03

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: 19
Joined: Thursday 28 January 2016 11:32
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by htca » Wednesday 08 March 2017 14:10

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: 751
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Wednesday 08 March 2017 14:24

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: 751
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Wednesday 08 March 2017 14:27

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: 19
Joined: Thursday 28 January 2016 11:32
Target OS: Raspberry Pi
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by htca » Wednesday 08 March 2017 14:34

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: 751
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Wednesday 08 March 2017 14:37

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: 751
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Wednesday 08 March 2017 14:43

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 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 » Wednesday 08 March 2017 16:31

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: 751
Joined: Friday 02 October 2015 12:12
Target OS: Raspberry Pi
Domoticz version: beta
Location: Finland
Contact:

Re: Find My iPhone implementation in LUA script

Post by Nautilus » Thursday 09 March 2017 9:46

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 9:33
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by pega20 » Thursday 09 March 2017 15:01

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 = "Pega20@gmail.com"; password = "12345"; device name = "iPhone Pega"};

I must use

users = {
            Peg = {username = "Pega20@gmail.com"; 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 16:38
Target OS: Raspberry Pi
Domoticz version: 3.8153
Location: Zwolle, Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by Wilco » Thursday 23 March 2017 17:38

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: 509
Joined: Tuesday 03 November 2015 12:01
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by htilburgs » Sunday 02 April 2017 13:27

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: 310
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Marum, The Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by mvzut » Friday 07 April 2017 2:58

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 2B - RFXtrx433 - Aeon Z-Stick gen5 - Opentherm Gateway - P1 smart meter - Netatmo - Philips Hue - ELV Max! - ESP8266 DIY water meter - Sonos Connect & PLAY:1 - Kodi - Wall mounted tablet + Imperihome - MANY switches/sensors

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest