Page 3 of 14
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:25
by mvzut
mcmikev wrote:Just discovered that It is not the switch name but device name of the iphone
Changed that and now I get more
But get error from line 53
2016-09-14 12:16:19.411 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:53: bad argument #1 to 'sub' (string expected, got nil)
Ah, that's another thing that apparently needs some polishing. I wanted to only update the text devices when their location has changed significantly (I use the "radius" variable for that again). I get the "old" distance from the text device itself. However, new text devices automatically contain the text "Hello world!", so you won't find any distance in there, and the script stops because LUA doesn't want to work with empty strings. 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))
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:32
by mcmikev
Tried this, but same results however.
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:34
by mvzut
mcmikev wrote:Tried this, but same results however.
What is the error code now? In which line (ideally give me the contents of that line)?
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:35
by cattoo
mvzut wrote:mcmikev wrote:Just discovered that It is not the switch name but device name of the iphone
Changed that and now I get more
But get error from line 53
2016-09-14 12:16:19.411 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:53: bad argument #1 to 'sub' (string expected, got nil)
Ah, that's another thing that apparently needs some polishing. I wanted to only update the text devices when their location has changed significantly (I use the "radius" variable for that again). I get the "old" distance from the text device itself. However, new text devices automatically contain the text "Hello world!", so you won't find any distance in there, and the script stops because LUA doesn't want to work with empty strings. 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(a, '%(.*%)')
if prev_distance_str == nil then prev_distance_str = '(1000 km)' end
prev_dist = tonumber(string.sub(prev_distance_str, 2,-5))
Changed to that, but ill still got error
Error: EventSystem: in Check_iphone: [string "-- Script to check the location of multiple i..."]:54: bad argument #1 to 'match' (string expected, got nil)
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:38
by mcmikev
Same here.
Error :
2016-09-14 13:38:05.117 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:53: bad argument #1 to 'match' (string expected, got nil)
Line:
prev_distance_str = string.match(a, '%(.*%)')
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:40
by mvzut
mcmikev wrote:Same here.
Error :
2016-09-14 13:38:05.117 Error: EventSystem: in /home/pi/domoticz/scripts/lua/script_time_checkphones.lua: /home/pi/domoticz/scripts/lua/script_time_checkphones.lua:53: bad argument #1 to 'match' (string expected, got nil)
Line:
prev_distance_str = string.match(a, '%(.*%)')
Ouch, I feel like a fool, I used a temporary variable "a" to test the new code, but forgot to change that again. I have updated my post (five messages ago), plus the original post, for the correct syntax. Sorry!
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:43
by mcmikev
That did it. Working now !!
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:45
by mvzut
mcmikev wrote:That did it. Working now !!
Great! Sorry that it took some fiddling, but let's look at it from the positive side: your problems helped to improve the script, and hopefully prevent others from having the same issues
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 13:48
by mcmikev
That is true. I really was thinking that I was doing it wrong
I like this idea and how you can use this.
Thanks!!!
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 14:05
by cattoo
Is the original script updated? So i could let go for the moment and fix everything after work?
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 14:10
by mvzut
cattoo wrote:Is the original script updated? So i could let go for the moment and fix everything after work?
Yes, I updated it. Please let me know if it works now!
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 14:36
by cattoo
Okey, i took the script now from the first post, and i still got errors..
Code: Select all
Error: EventSystem: in Check_iphone: [string "-- Script to check the location of multiple i..."]:53: bad argument #1 to 'match' (string expected, got nil)
2016-09-14 14:35:30.046 H
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 15:57
by mvzut
cattoo wrote:Okey, i took the script now from the first post, and i still got errors..
Code: Select all
Error: EventSystem: in Check_iphone: [string "-- Script to check the location of multiple i..."]:53: bad argument #1 to 'match' (string expected, got nil)
2016-09-14 14:35:30.046 H
That probably means you don't have a text device called "Position Person1" for each person (replacing Person1 by the names that you put in the "user" array for every user, i.e. the names you replaced "Person1" etc with). Could that be the case? I could build in a check to avoid that the script stops because it can't find the text device (I probably will indeed add that) but that doesn't help you directly: I guess you want the text devices to display the current position, right?
If you DO have text devices named in exactly the right way, there is one last thing you could try: change
Code: Select all
prev_distance_str = string.match(otherdevices['Position ' .. user], '%(.*%)') or '(1000 km)'
into
Code: Select all
prev_distance_str = string.match(otherdevices_svalues['Position ' .. user], '%(.*%)') or '(1000 km)'
That seemed to help for at least one user.
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 16:12
by cattoo
Thank you, completely missed "Position Person1" text switch. My bad! Hopefully the "home" switch will activate when i get home
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 17:30
by Egregius
Yes, 5 minutes later
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 17:45
by mvzut
Egregius wrote:Yes, 5 minutes later
To be precise: somewhere between 0:01 and 4:59 minutes later
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 18:32
by deejc
hi,
i got this to work 1 time, but it wont fire any more.
restarted domoticz etc, resaved an event to reset the events system but it does not fire, nothing in the log etc.. very strange.. any ideas if i can force it to run to see if it logs anything ?
Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 19:27
by mvzut
deejc wrote:hi,
i got this to work 1 time, but it wont fire any more.
restarted domoticz etc, resaved an event to reset the events system but it does not fire, nothing in the log etc.. very strange.. any ideas if i can force it to run to see if it logs anything ?
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...
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 19:38
by deejc
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
thanks a lot
Re: Find My iPhone implementation in LUA script
Posted: Wednesday 14 September 2016 19:46
by deejc
i actually read the thread start to finish and found the line
Code: Select all
print('iPhone ' .. user .. ': ' .. math.floor(distance*100+0.5)/100 .. ' km from home')
so i enabled it and i see the output!
working perfectly, nice one, i came from Vera and i know the plugin this mimics, that plugin has some logic that shortens or lengthens the ping time based on the distance away from home, that would be great addition.