Page 4 of 14

Find My iPhone implementation in LUA script

Posted: Wednesday 14 September 2016 19:47
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

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 14 September 2016 20:20
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..

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 14 September 2016 22:11
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)?

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 14 September 2016 23:08
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.

Re: Find My iPhone implementation in LUA script

Posted: Wednesday 14 September 2016 23:10
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.

Re: Find My iPhone implementation in LUA script

Posted: Thursday 15 September 2016 6:31
by Egregius
Afraid of PHP? :lol:
Those lua lovers must think their installation will explode of they install php :mrgreen:

Re: Find My iPhone implementation in LUA script

Posted: Thursday 15 September 2016 7:46
by G3rard
No i am not
Already using php for my Sonos integration and some other stuff.

Re: Find My iPhone implementation in LUA script

Posted: Thursday 15 September 2016 13:24
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

Re: Find My iPhone implementation in LUA script

Posted: Thursday 15 September 2016 13:36
by G3rard
Check my post above from yesterday at 23:08. Maybe that fixes your error as well.

Re: Find My iPhone implementation in LUA script

Posted: Thursday 15 September 2016 15:58
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))

Re: Find My iPhone implementation in LUA script

Posted: Thursday 15 September 2016 16:12
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 ?

Re: Find My iPhone implementation in LUA script

Posted: Thursday 15 September 2016 17:12
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?

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 9:38
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.

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 9:45
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.

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 15:44
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!

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 15:49
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 ?

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 15:53
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.

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 16:15
by bobross
:o :o :o :o :o yes,.... I reversed it........... :o thanks !!!

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 16:43
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.

Re: Find My iPhone implementation in LUA script

Posted: Friday 16 September 2016 16:46
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