Find My iPhone implementation in LUA script

Moderator: leecollings

mvzut
Posts: 443
Joined: Thursday 12 November 2015 10:55
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: The Netherlands
Contact:

Find My iPhone implementation in LUA script

Post by mvzut »

deejc wrote:
mvzut wrote: If your position doesn't change no device will update and you won't see anything in the log. Are you really sure the script is not running? You can check by putting a "print('test')" command in the top, directly after "commandArray = {}". If you don't see this in your log, and you named the script correctly (i.e. script_time_something.lua) then I'm afraid I'm out of advice to give...
Blimey.. thats good, it is working, well it prints the test at any rate.
i expected to see some lat,long output in the log, guess i'll find out tomorrow when i leave :D
thanks a lot
If you want messages in the log every time your devices are polled you can easily do that (e.g. remove the -- in front of the print command later in my script), but I disabled it by default because once it works I don't see the added value.

EDIT: ok our posts crossed, I guess you found it Image
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
User avatar
deejc
Posts: 168
Joined: Tuesday 22 September 2015 18:50
Target OS: Raspberry Pi / ODroid
Domoticz version: 3.5641
Location: UK
Contact:

Re: Find My iPhone implementation in LUA script

Post by deejc »

seems to be working correctly, my wifes phone is away and mine is home, when i switch the switches to on/off the script correctly detects the phones away / home location and switches the switch to the correct mode and send the notification !! very good way of doing it.
i am also using homebridge and i can setup a trigger/scene to do it, so ill monitor battery life using both and see if it is detrimental..
Domoticz 3.5641 on RPI (Raspbian GNU/Linux 8)
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 »

mvzut wrote: I tried to solve it with the 'if prev_distance == nil then prev_distance = 1000 end" line, to give it an initial value that is large enough so that the text device gets updated, but it doesn't even get to that point apparently. You could change the contents of the text field into something like "(1000 km)" by hand, but I don't find that a very elegant solution. I think I found a better solution now:

Please change these lines

Code: Select all

prev_dist = tonumber(string.sub(string.match(otherdevices['Position ' .. user], '%(.*%)'), 2,-5))
if prev_distance == nil then prev_distance = 1000 end
into this

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
prev_dist = tonumber(string.sub(prev_distance_str, 2,-5))
I think it was just that you are using variable "prev_dist" but then checking "prev_distance" not to be nil...:) In any case, would it be a good idea to put the script to github or somewhere that would provide automatic versioning and maybe a better platform to develop (even by other Domoticz users)?
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by G3rard »

G3rard wrote: Thanks for this script!

I had to change the line

Code: Select all

prev_dist = tonumber(string.sub(string.match(otherdevices['Position ' .. user], '%(.*%)'), 2,-5))
to

Code: Select all

prev_dist = tonumber(string.sub(string.match(otherdevices_svalues['Position ' .. user], '%(.*%)'), 2,-5))
to get this working.
mvzut wrote: Strange, here it (also) works without using svalues. Makes you wonder how two Domoticz systems can behave so differently? Are you perhaps on a stable (older) version?
I am on 3.5075, so that's a bit older now.
I just tried it again without svalues but that gave me the following error again

Code: Select all

bad argument #1 to 'sub' | string expected, got nil 
So probably something that was changed in the Domoticz code since the version I am using. So for people on the stable version 3.4834 maybe that should use svalues as well when facing errors like above.
Not using Domoticz anymore
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by G3rard »

Egregius wrote:
G3rard wrote:
Egregius wrote:Do you know you can (ab)use the same service to sent text messages to ios? They even make sound when the phone is in do not disturb mode or silent.
Nice, do you have some more information available on this?
Only in PHP :lol:
It's included in my pass2php script (http://egregius.be/tag/domoticz/ and https://github.com/Egregius/PHP-Custom- ... r-Domoticz)
Once operational it's enough to have a line like this:

Code: Select all

ios('test message with high priority');
Then the message is sent as if it would be sent from the findmyiphone service, because of that it always makes sound. Ideal for those really important messages like movement in the living room while you're sleeping and the phone is on do not disturb mode. Mine is always in that mode during the night.
Thanks, I will have a look at your code to see if I can use that in LUA code.
Not using Domoticz anymore
User avatar
Egregius
Posts: 2582
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Find My iPhone implementation in LUA script

Post by Egregius »

Afraid of PHP? :lol:
Those lua lovers must think their installation will explode of they install php :mrgreen:
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by G3rard »

No i am not
Already using php for my Sonos integration and some other stuff.
Not using Domoticz anymore
rpwessels
Posts: 8
Joined: Tuesday 16 August 2016 11:41
Target OS: Linux
Domoticz version: V3.5641
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by rpwessels »

Hello.

I am trying this script to work, but get this error in my log.

2016-09-15 13:16:04.418 Error: EventSystem: ...o/domoticz/scripts/lua/script_time_checkphones_debug.lua:58: bad argument #1 to 'match' (string expected, got nil)

Line 58 is this line in my script: prev_dist = tonumber(string.sub(string.match(otherdevices_svalues['Position ' .. user], '%(.*%)'), 2,-5))

The names of the devices are correct and i receive the position of my device.

Who can help me.

Ricardo Wessels
Ubuntu16.04 LTS | Z-wave | Opentherm | P1 Smartmeter
User avatar
G3rard
Posts: 669
Joined: Wednesday 04 March 2015 22:15
Target OS: -
Domoticz version: No
Location: The Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by G3rard »

Check my post above from yesterday at 23:08. Maybe that fixes your error as well.
Not using Domoticz anymore
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 »

rpwessels wrote:Hello.

I am trying this script to work, but get this error in my log.

2016-09-15 13:16:04.418 Error: EventSystem: ...o/domoticz/scripts/lua/script_time_checkphones_debug.lua:58: bad argument #1 to 'match' (string expected, got nil)

Line 58 is this line in my script: prev_dist = tonumber(string.sub(string.match(otherdevices_svalues['Position ' .. user], '%(.*%)'), 2,-5))

The names of the devices are correct and i receive the position of my device.

Who can help me.

Ricardo Wessels
This is because the new text devices get a value "Hello world!" when they are first created. I changed the script to be robust against that, see original post. Basically you have to change your line 58 into

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
prev_distance = tonumber(string.sub(prev_distance_str, 2,-5))
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
bobross
Posts: 4
Joined: Monday 03 March 2014 11:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by bobross »

Hmmzz the scripts seems to work. In my log and position devices I get the location but.....

2016-09-15 16:05:04.880 LUA: iPhone xxxxxx: 6074.79 km from home
2016-09-15 16:05:08.934 LUA: iPhone xxxxxx: 6073.64 km from home

...i'm a long way from home.....that's strange because I'm at home right nog.

Long/Latitude are correct, timezone is Amsterdam, I didn't change the script otherwise.

What's wrong ?
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 »

bobross wrote:Hmmzz the scripts seems to work. In my log and position devices I get the location but.....

2016-09-15 16:05:04.880 LUA: iPhone xxxxxx: 6074.79 km from home
2016-09-15 16:05:08.934 LUA: iPhone xxxxxx: 6073.64 km from home

...i'm a long way from home.....that's strange because I'm at home right nog.

Long/Latitude are correct, timezone is Amsterdam, I didn't change the script otherwise.

What's wrong ?
That's strange, especially if the lat and lon are correct. The distance is calculated using this formula (please check if it is correctly copied into your script):

Code: Select all

distance = math.sqrt(((lon - homelongitude) * 111.320 * math.cos(math.rad(lat)))^2 + ((lat - homelatitude) * 110.547)^2)
In other words: one degree is apprximately 110.547 km for latitude, and approximately 111.32 km times cos(lat) for longitude. Then I'm just using Pythagoras for the distance (a^2 + b^2 = c^2).
You say "long/latitude are correct". Do you mean of your house, or did you add print commands (e.g. print(lat .. ' ' .. lon')) to check if it reports your actual correct position? You're saying it does give the right address in the text devices? Is the distance between the brackets correct?
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
rpwessels
Posts: 8
Joined: Tuesday 16 August 2016 11:41
Target OS: Linux
Domoticz version: V3.5641
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by rpwessels »

mvzut wrote:
rpwessels wrote:Hello.

I am trying this script to work, but get this error in my log.

2016-09-15 13:16:04.418 Error: EventSystem: ...o/domoticz/scripts/lua/script_time_checkphones_debug.lua:58: bad argument #1 to 'match' (string expected, got nil)

Line 58 is this line in my script: prev_dist = tonumber(string.sub(string.match(otherdevices_svalues['Position ' .. user], '%(.*%)'), 2,-5))

The names of the devices are correct and i receive the position of my device.

Who can help me.

Ricardo Wessels
This is because the new text devices get a value "Hello world!" when they are first created. I changed the script to be robust against that, see original post. Basically you have to change your line 58 into

Code: Select all

prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
prev_distance = tonumber(string.sub(prev_distance_str, 2,-5))
Hello mvzut,

I adjusted the "Hello World" manual to "nil" and tried already your lines in my script, but i still get the same warning.

Also with the tips of G3rard i got the same warning in the log.
Ubuntu16.04 LTS | Z-wave | Opentherm | P1 Smartmeter
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 »

rpwessels wrote: Hello mvzut,

I adjusted the "Hello World" manual to "nil" and tried already your lines in my script, but i still get the same warning.

Also with the tips of G3rard i got the same warning in the log.
You don't need to change them into "nil". The new code should work with "Hello World!". In fact, changing them into "nil" may introduce new problems with the string.match command. So just give them any value except "nil" and try the new code. It may be good to copy the complete new code from the original post just to be sure.
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
wlevels
Posts: 16
Joined: Friday 16 September 2016 13:54
Target OS: Raspberry Pi / ODroid
Domoticz version: V4.9700
Location: The Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by wlevels »

I'm still getting errors on my Domoticz V3.4834 installation on my Raspberry.

I tried all the previous possible solutions, I added some print commands in the lua script and I can see my iphone gets tracked as expected, however line 55 keeps giving me an error:

Code: Select all

2016-09-16 14:50:03.684 LUA: 5.6282889499935
2016-09-16 14:50:03.684 LUA: 51.485975960002
2016-09-16 14:50:03.685 LUA: 4609.870379533
2016-09-16 14:50:05.629 LUA: Rode Beemden 27, 5708 AW Helmond, Netherlands
2016-09-16 14:50:05.630 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:55: bad argument #1 to 'match' (string expected, got nil) 
Line 55 is:

Code: Select all

        prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
Tried changing it to otherdevices_svalues but no joy.

Thanks!
bobross
Posts: 4
Joined: Monday 03 March 2014 11:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by bobross »

mvzut wrote: You say "long/latitude are correct". Do you mean of your house, or did you add print commands (e.g. print(lat .. ' ' .. lon')) to check if it reports your actual correct position? You're saying it does give the right address in the text devices? Is the distance between the brackets correct?
What command can I use to check the current position ?

In my position device I get the correct adress where my phones are, but it also says it's 6000km away from home.
Seems somehow my location settings of the raspberry pi are incorrect or so ?
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 »

bobross wrote:
mvzut wrote: You say "long/latitude are correct". Do you mean of your house, or did you add print commands (e.g. print(lat .. ' ' .. lon')) to check if it reports your actual correct position? You're saying it does give the right address in the text devices? Is the distance between the brackets correct?
What command can I use to check the current position ?

In my position device I get the correct adress where my phones are, but it also says it's 6000km away from home.
Seems somehow my location settings of the raspberry pi are incorrect or so ?
Could it be that you reversed longitude and latitude in your home location? It only looks at those two figures in the script for that, not at any system wide location settings of the Raspberry Pi.
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
bobross
Posts: 4
Joined: Monday 03 March 2014 11:40
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Find My iPhone implementation in LUA script

Post by bobross »

:o :o :o :o :o yes,.... I reversed it........... :o thanks !!!
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 »

wlevels wrote:I'm still getting errors on my Domoticz V3.4834 installation on my Raspberry.

I tried all the previous possible solutions, I added some print commands in the lua script and I can see my iphone gets tracked as expected, however line 55 keeps giving me an error:

Code: Select all

2016-09-16 14:50:03.684 LUA: 5.6282889499935
2016-09-16 14:50:03.684 LUA: 51.485975960002
2016-09-16 14:50:03.685 LUA: 4609.870379533
2016-09-16 14:50:05.629 LUA: Rode Beemden 27, 5708 AW Helmond, Netherlands
2016-09-16 14:50:05.630 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:55: bad argument #1 to 'match' (string expected, got nil) 
Line 55 is:

Code: Select all

        prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
Tried changing it to otherdevices_svalues but no joy.

Thanks!
I notice two things:

First of all, I assume the large numer (4609.870379533) is the result of a print(distance), right? You seem to be a very long way from home then... Are you sure you filled in the homelongitude and homelatitude correctly? Did you maybe reverse them by any chance?

Secondly, if this line gives an error, then this means otherdevices['Position ' .. user] is not a string. Are you sure you have dummy text devices for every user named "Position Person1", "Position Person2" etc.? Obviuosly Replacing "Person1" etc. with what you replaced "Person1" etc. with in the definition of the "user" array in the top of the script. So if your user array is

Code: Select all

  users = {
            Henk = {username = '[email protected]' ; password = 'datzegiklekkerniet' ; devicename = 'iPhone van Henk'};
            Ingrid = {username = '[email protected]' ; password = 'wachtwoord' ; devicename = 'iPhone van Ingrid'};
           }
Then your text devices should be named "Position Henk" and "Position Ingrid".

Hope this brings you a step further. I think you're almost there.
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
rpwessels
Posts: 8
Joined: Tuesday 16 August 2016 11:41
Target OS: Linux
Domoticz version: V3.5641
Location: Netherlands
Contact:

Re: Find My iPhone implementation in LUA script

Post by rpwessels »

Suddenly i saw that my version of Domoticz was very old. Every time when i ask to check for updates, the answer was that there where no updates. Now i performed the update manual via terminal and it is working now.

Thank you anyway for your help.

Gr. Ricardo
Ubuntu16.04 LTS | Z-wave | Opentherm | P1 Smartmeter
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest