Find My iPhone implementation in LUA script

Must be cleaned all topics go to the right subforum
Post Reply
mvzut
Posts: 280
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Marum, The Netherlands
Contact:

Find My iPhone implementation in LUA script

Post by mvzut » Tuesday 11 October 2016 7:02

BluesBro wrote: The error is now reduced to only:

2016-10-10 16:34:02.816 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:46: attempt to index field 'location' (a nil value)
Apparently the API call to the Find My iPhone service didn't result in location info. Can you add a print(result) just before the "output = json:decode(result)" line? Do you see your phone name in the (large) string that should now be printed every 5 minutes in your log? And do you see coordinates close to that name? Probably not, otherwise the location variable would not be nil, but maybe you find some hint in this string that could explain why.
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

mvzut
Posts: 280
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Marum, The Netherlands
Contact:

Find My iPhone implementation in LUA script

Post by mvzut » Tuesday 11 October 2016 7:24

patoo77 wrote:Nice work mate, always love to see what is being done using location positioning :)
The only downside I would see is huge battery drain, since polling for FindMyiPhone every 5 minute has to be huge in terms of battery ressources. Have you monitored how much battery it costs/hour?

I am always looking to improve the location on my own app, I have alrdy given a try to the "find my iphone" API a few years ago.
I wonder if your solution could be improved by coupling it with my own Pilot app: for example, trigger the "poll" only when the users enters a much wide radius (maybe a km around home). That way, less battery usage, and much better positioning.

Happy to speak with you all on that matter,
P
Hi,

Battery drainage is quite acceptable in my opinion. I haven't checked the exact %/hour but I'm guessing it's not more than 1-2 on my iPhone 6s.

I have adapted the script for myself to only poll when a dummy switch called "Position polling" is on, you could indeed use Pilot to operate that switch. But I could also use the script's own knowledge of the devices' location to determine a polling frequency.

By the way, the reason I added this "Position polling" switch is because I don't continuously use the system anymore myself. Like others have stated here: 5 minutes is too long to use it for e.g. switching on lights or operating the alarm. And even if I would implement an adaptive polling frequency, making it poll every minute when approaching home: I noticed the position is sometimes off or delayed. It remains a nice function, but I'm still looking for the best way to make use of it.
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

BluesBro
Posts: 77
Joined: Tuesday 13 August 2013 19:55
Target OS: -
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by BluesBro » Tuesday 11 October 2016 8:35

mvzut wrote:
BluesBro wrote: The error is now reduced to only:

2016-10-10 16:34:02.816 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:46: attempt to index field 'location' (a nil value)
Apparently the API call to the Find My iPhone service didn't result in location info. Can you add a print(result) just before the "output = json:decode(result)" line? Do you see your phone name in the (large) string that should now be printed every 5 minutes in your log? And do you see coordinates close to that name? Probably not, otherwise the location variable would not be nil, but maybe you find some hint in this string that could explain why.
When i run that (print) in putty it shows all my devices, macbook pro, ipad, imac, iphone and so on and the iphone shows coordinates. But the same log in domoticz oinly shows partially the same info. Mabye domoticz cuts the (print) after a certain length? Maby your script looks for the first device listed and its coordinates? Since the first device listed in my list is an imac its coordinates dont have value..?

BluesBro
Posts: 77
Joined: Tuesday 13 August 2013 19:55
Target OS: -
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by BluesBro » Tuesday 11 October 2016 9:11

I found the error... I have had several iphones throug the ages and all are listed in the (print). But it also lists the ones that dont exist anymore.
I have looked throu the icloud settings to see if i can find them and somehow delete them but no luck. I will check if changing the name on the physical device works...

BluesBro
Posts: 77
Joined: Tuesday 13 August 2013 19:55
Target OS: -
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by BluesBro » Tuesday 11 October 2016 9:28

Now i get this error in Domoticz:

Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:54: bad argument #1 to 'match' (string expected, got nil)

And this error in Putty:

lua: script_time_checkphones.lua:54: attempt to index global 'otherdevices' (a nil value)
stack traceback:
script_time_checkphones.lua:54: in main chunk
[C]: ?


Print (lat,long,position) gives me:
10.364905075923
63.431380007175
Ilsviktunnelen, 7018 Trondheim, Norway

So location works but still errors..?

Edit:
Got it! The device name update took some minutes before it changed... :-)

BluesBro
Posts: 77
Joined: Tuesday 13 August 2013 19:55
Target OS: -
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by BluesBro » Tuesday 11 October 2016 9:41

Suggestion: Why not curl google maps embedded like this:
http://www.domoticz.com/forum/viewtopic ... 705#p81542

You would need an API, but thats easy.

perese
Posts: 7
Joined: Friday 21 October 2016 11:43
Target OS: NAS (Synology & others)
Domoticz version:
Location: Sweden
Contact:

Re: Find My iPhone implementation in LUA script

Post by perese » Friday 21 October 2016 11:55

Hi,
New to this forum, please bare with me.

Trying to implement this script (not a fully script hacker, but understands and can follow scripts) but get a common error as above. Not sure if it because of Synology implementation of Domoticz or if I'm not understood the "text switch" creation completely.

I Get this error in the console log:
2016-10-21 11:45:07.620 Error: EventSystem: in checkiphones: [string "-- Script to check the location of multiple i..."]:54: bad argument #1 to 'match' (string expected, got nil)

and the row 54 is the uncommented one
--prev_distance_str = string.match(otherdevices['Position-Peter'], '%(.*%)') or '(1000 km)'
prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
--prev_distance_str = string.match(otherdevices_svalues['Position ' .. user], '%(.*%)') or '(1000 km)'

As you see I've tested both with a explicit naming (yes, tried without "white space" in the name) of the text switch and also by using the _svalues approach.
The logging capabilities are the one built in to the system, have not seen any other logs that are avail with the Syno implementation (perhaps there is??)

Can anyone confirm that a text switch is created by RMB on the Dummy switch and selecting the Text option of device? The switch/device appears in the utility tab and not in the Switches tab in Domoticz after created.

Any other tips or ideas?

Thanks,
Peter

lukev
Posts: 56
Joined: Friday 21 October 2016 10:42
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by lukev » Friday 21 October 2016 12:24

Peter, I can confirm the script can work on a Synology with domoticz installed. It does on mine ;-)

perese
Posts: 7
Joined: Friday 21 October 2016 11:43
Target OS: NAS (Synology & others)
Domoticz version:
Location: Sweden
Contact:

Re: Find My iPhone implementation in LUA script

Post by perese » Friday 21 October 2016 13:59

lukev wrote:Peter, I can confirm the script can work on a Synology with domoticz installed. It does on mine ;-)
Thanks for the confirm. Since your seems to work, did you create the "text switch" as I described?

lukev
Posts: 56
Joined: Friday 21 October 2016 10:42
Target OS: NAS (Synology & others)
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by lukev » Friday 21 October 2016 14:02

perese wrote:Thanks for the confirm. Since your seems to work, did you create the "text switch" as I described?
Yes, my switch is created like you describe here:
perese wrote:Can anyone confirm that a text switch is created by RMB on the Dummy switch and selecting the Text option of device? The switch/device appears in the utility tab and not in the Switches tab in Domoticz after created.
It is in the utility-tab of my domoticz interface, visible in the devices als type: "general" and subtype: "text"

perese
Posts: 7
Joined: Friday 21 October 2016 11:43
Target OS: NAS (Synology & others)
Domoticz version:
Location: Sweden
Contact:

Re: Find My iPhone implementation in LUA script

Post by perese » Monday 24 October 2016 16:34

After a debugging and in same time do understand LUA and Domoticz event system and configuration I think I found a solution.

I cannot say why that needed for me but after changing

Line 54 from

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
to

Code: Select all

prev_distance_str = string.match(otherdevices_idx['Position ' .. user], '%(.*%)') or '(1000 km)'
It suddenly worked. Maybe it is the version I'm at or something else, but the original code did not work.

Thanks lukev for your confirmation, helped a lot in my searches :)

mvzut
Posts: 280
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Marum, The Netherlands
Contact:

Find My iPhone implementation in LUA script

Post by mvzut » Monday 24 October 2016 23:56

perese wrote:After a debugging and in same time do understand LUA and Domoticz event system and configuration I think I found a solution.

I cannot say why that needed for me but after changing

Line 54 from

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
to

Code: Select all

prev_distance_str = string.match(otherdevices_idx['Position ' .. user], '%(.*%)') or '(1000 km)'
It suddenly worked. Maybe it is the version I'm at or something else, but the original code did not work.

Thanks lukev for your confirmation, helped a lot in my searches :)
The line you changed looks for text between parentheses in the text device named "Position <user>", where <user> is obviously replaced with your user name. If it can't find text between parentheses, it will return the value "(1000 km)". That way the next steps in the script will still function, even if the text device still contains the initial text "Hello World!", and since "prev_distance" is given such a high value, the later check

Code: Select all

if math.abs(prev_distance - distance) > radius then
is certainly true so that it will update the value of the text device.

Your change makes the script look for text between parentheses in the IDX of the device, which is a number, so it won't find such text. Therefore, in your new script "prev_distance_str" will always get the value"(1000 km)", so the text field will always be updated. If this causes things to start working for you that's great, but please note that it's maybe not ideal: The text field is now updated every minute, even if the value didn't change, which causes an unnecessarily large sensor log.

What could be the case is that you somehow can't read the values of your text device(s) with an "otherdevices[...]" command, I believe more people have reported this in this forum. Could be related to the Domoticz version. You could try to replace "otherdevices[...]" with "otherdevices_svalues[...]". Put a print(prev_distance_str) line after the string.match line to check if it actually gets a value different than "(1000 km)".
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

bink
Posts: 19
Joined: Friday 21 August 2015 13:41
Target OS: Linux
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by bink » Tuesday 25 October 2016 17:26

Does anybody use this script with an account that uses two factor authentication? I did create an app-specific password and used that one in the script however that does not seem to make a difference. The result of the stage1command is:

Code: Select all

2016-10-25 17:25:01.206 LUA: start stage2command
2016-10-25 17:25:01.206 LUA: HTTP/1.1 401 Unauthorized 
X-Responding-Instance: fmipservice:32400202:mr11p24ic-ztav081611:8002:16G72:e97dd33 
X-Responding-Server: mr11p24ic-ztav081611_002 
X-Responding-Partition: p24 
Content-Length: 50 
Date: Tue, 25 Oct 2016 15:25:00 GMT 
Server: Apple-iCloud 
Strict-Transport-Security: max-age=31536000; includeSubDomains 
Set-Cookie: NSC_q24-gnjqtfswjdf=ffffffff12ba469945525d5f4f58455e445a4a422972;path=/;secure;httponly 

2016-10-25 17:25:01.206 Error: EventSystem: in /root/domoticz/scripts/lua/script_time_checkphones.lua: /root/domoticz/scripts/lua/script_time_checkphones.lua:41: attempt to concatenate global 'stage2server' (a nil value)
Anybody an idea how to get this working?

mvzut
Posts: 280
Joined: Thursday 12 November 2015 11:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Marum, The Netherlands
Contact:

Find My iPhone implementation in LUA script

Post by mvzut » Wednesday 26 October 2016 17:57

I'm sorry but I can't help you with that. This way of accessing the Apple Find My iPhone API is not well documented, and most info I found was not very recent. Maybe Apple describes it in their development area, but I have no access there. I'm not even sure Apple wants us to access their servers in this way at all... So I'm afraid you're on your own here, unless someone on this forum happens to have more knowledge on this topic.

A quick fix is obviously to let go of the two factor authentication, but I guess you activated that for a reason .
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

perese
Posts: 7
Joined: Friday 21 October 2016 11:43
Target OS: NAS (Synology & others)
Domoticz version:
Location: Sweden
Contact:

Re: Find My iPhone implementation in LUA script

Post by perese » Thursday 03 November 2016 11:49

mvzut wrote:
perese wrote:After a debugging and in same time do understand LUA and Domoticz event system and configuration I think I found a solution.

I cannot say why that needed for me but after changing

Line 54 from

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
to

Code: Select all

prev_distance_str = string.match(otherdevices_idx['Position ' .. user], '%(.*%)') or '(1000 km)'
It suddenly worked. Maybe it is the version I'm at or something else, but the original code did not work.

Thanks lukev for your confirmation, helped a lot in my searches :)
The line you changed looks for text between parentheses in the text device named "Position <user>", where <user> is obviously replaced with your user name. If it can't find text between parentheses, it will return the value "(1000 km)". That way the next steps in the script will still function, even if the text device still contains the initial text "Hello World!", and since "prev_distance" is given such a high value, the later check

Code: Select all

if math.abs(prev_distance - distance) > radius then
is certainly true so that it will update the value of the text device.

Your change makes the script look for text between parentheses in the IDX of the device, which is a number, so it won't find such text. Therefore, in your new script "prev_distance_str" will always get the value"(1000 km)", so the text field will always be updated. If this causes things to start working for you that's great, but please note that it's maybe not ideal: The text field is now updated every minute, even if the value didn't change, which causes an unnecessarily large sensor log.

What could be the case is that you somehow can't read the values of your text device(s) with an "otherdevices[...]" command, I believe more people have reported this in this forum. Could be related to the Domoticz version. You could try to replace "otherdevices[...]" with "otherdevices_svalues[...]". Put a print(prev_distance_str) line after the string.match line to check if it actually gets a value different than "(1000 km)".
Hi,
Thanks for the clarifications. I understood the coding, but not how the code read the devices and difference with/without the IDX tag. However, the text switch did not change and the print(prev_distance_str) command did not print anything without the IDX option and got a nil error. Will update the script shortly with your suggestion to see if it get a different value than "(1000 km)" with my change. Will of course not fill the log.. However, did try out the ..._svalues option but that did not work either...

/Peter

Jaron
Posts: 1
Joined: Wednesday 09 November 2016 16:31
Target OS: NAS (Synology & others)
Domoticz version: 3.4834
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by Jaron » Wednesday 09 November 2016 16:53

Hi! As many here I also got the error "script_time_checkphones.lua:54: bad argument #1 to 'match' (string expected, got nil)". Checked and double checked, tried all suggestions, but nothing works.

It seems to have something to do with the dummy text device ... I can get the text value of any other switch / device with the code: print(otherdevices['any device']) but the dummy text device returns nil (so no string), although it's value is "Hello world!" according to the hardware setup. I'm running Domoticz on a Syno (with both latest versions of Domoticz and the JSON.lua)

Conclusion: there's something wrong with the device I created or text devices are not supported ...
To rule out the first, can you please give a step-by-step on how you created the device?
Last edited by Jaron on Thursday 10 November 2016 15:59, edited 1 time in total.

VladGets
Posts: 3
Joined: Thursday 10 November 2016 15:27
Target OS: Windows
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by VladGets » Thursday 10 November 2016 15:30

Wow its really cool

Nautilus
Posts: 653
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 16 November 2016 20:20

Hmm, strange. The following has happened now a couple of times. If the phone is off (or unreachable) the dummy text sensor gets deleted. Cannot see any traces about this in the log either. How can this be - and has it happened to anyone else?

Nautilus
Posts: 653
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 07 December 2016 21:34

Nautilus wrote:Hmm, strange. The following has happened now a couple of times. If the phone is off (or unreachable) the dummy text sensor gets deleted. Cannot see any traces about this in the log either. How can this be - and has it happened to anyone else?
A family member is travelling again and thus the above (text sensor disappeared) happened again as well. This time I did some more investigating and found out that the disappeared idx's are still in the db, just not visible on the devices screen (nor sensors tab) in web interface. But when looking at the event system's "show current states" popup window they are still listed (with correct values from latest update).

I then went ahead and deleted the "disappeared" text sensors with json (http://url/json.htm?type=setused&idx=ID ... used=false) and after this created a new text sensor but the same happened again right away on the first update. "Show current states" shows also the newest still nicely and I'm able to print the "position_text" to log without problems. But the sensor disappears from "sensors", "dashboard", "devices" etc. What could be the issue?

So issue is not that phone would be off or unreachable as it is showing the location just fine. But for some reason when the location is abroad (= outside Finland in this case) this weird thing happens...

edit: after some more testing it is the iframe part that makes the magic happen. After removing that, also the abroad address works. This is the original code I was using to determine the position text:

Code: Select all

position_text = string.gsub(address, ', Finland', '') .. '<br>(' .. drivedistance .. ' / ' .. drivetime ..')<br><iframe width="300" height="260" frameborder="0" style="border:0" src="https://www.google.com/maps/embed/v1/place?key=APIKEY&q=' .. lat .. ',' .. lon .. '" allowfullscreen></iframe>'
Cannot see any reason why domestic addresses are shown nicely on the iframe, foreign addresses make the sensor disappear. Now that I know I can get it visible again by updating the sensor with some "valid" value, I can still see all the iframe's nicely in the sensor's log, even those that made is disappear from UI... :shock:

perese
Posts: 7
Joined: Friday 21 October 2016 11:43
Target OS: NAS (Synology & others)
Domoticz version:
Location: Sweden
Contact:

Re: Find My iPhone implementation in LUA script

Post by perese » Friday 09 December 2016 17:27

mvzut wrote:
perese wrote:After a debugging and in same time do understand LUA and Domoticz event system and configuration I think I found a solution.

I cannot say why that needed for me but after changing

Line 54 from

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
to

Code: Select all

prev_distance_str = string.match(otherdevices_idx['Position ' .. user], '%(.*%)') or '(1000 km)'
It suddenly worked. Maybe it is the version I'm at or something else, but the original code did not work.

Thanks lukev for your confirmation, helped a lot in my searches :)
The line you changed looks for text between parentheses in the text device named "Position <user>", where <user> is obviously replaced with your user name. If it can't find text between parentheses, it will return the value "(1000 km)". That way the next steps in the script will still function, even if the text device still contains the initial text "Hello World!", and since "prev_distance" is given such a high value, the later check

Code: Select all

if math.abs(prev_distance - distance) > radius then
is certainly true so that it will update the value of the text device.

Your change makes the script look for text between parentheses in the IDX of the device, which is a number, so it won't find such text. Therefore, in your new script "prev_distance_str" will always get the value"(1000 km)", so the text field will always be updated. If this causes things to start working for you that's great, but please note that it's maybe not ideal: The text field is now updated every minute, even if the value didn't change, which causes an unnecessarily large sensor log.

What could be the case is that you somehow can't read the values of your text device(s) with an "otherdevices[...]" command, I believe more people have reported this in this forum. Could be related to the Domoticz version. You could try to replace "otherdevices[...]" with "otherdevices_svalues[...]". Put a print(prev_distance_str) line after the string.match line to check if it actually gets a value different than "(1000 km)".
Today I had some time left over. Did put the

Code: Select all

print(prev_distance_str)
and as you said got the 1000 km. Then I reverted back to original coding as

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
and by magic and two version update it worked... I don't know what has happened but guess there was some impact with the version or similar. Anyway, thanks for your direction (and yes, the log was huge!)

/Peter

Post Reply

Who is online

Users browsing this forum: freekh and 4 guests