Find My iPhone implementation in LUA script

Must be cleaned all topics go to the right subforum
mvzut
Posts: 272
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 » Wednesday 14 September 2016 13:25

mcmikev wrote:Just discovered that It is not the switch name but device name of the iphone :shock: :shock:

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))
Last edited by mvzut on Wednesday 14 September 2016 13:38, edited 1 time in total.
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

mcmikev
Posts: 118
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Find My iPhone implementation in LUA script

Post by mcmikev » Wednesday 14 September 2016 13:32

Tried this, but same results however.

mvzut
Posts: 272
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 » Wednesday 14 September 2016 13:34

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)?
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

cattoo
Posts: 55
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 » Wednesday 14 September 2016 13:35

mvzut wrote:
mcmikev wrote:Just discovered that It is not the switch name but device name of the iphone :shock: :shock:

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)

mcmikev
Posts: 118
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Find My iPhone implementation in LUA script

Post by mcmikev » Wednesday 14 September 2016 13:38

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, '%(.*%)')

mvzut
Posts: 272
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 » Wednesday 14 September 2016 13:40

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!
Last edited by mvzut on Wednesday 14 September 2016 13:43, edited 1 time in total.
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

mcmikev
Posts: 118
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Find My iPhone implementation in LUA script

Post by mcmikev » Wednesday 14 September 2016 13:43

That did it. Working now !!

mvzut
Posts: 272
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 » Wednesday 14 September 2016 13:45

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 ;-)
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

mcmikev
Posts: 118
Joined: Tuesday 26 May 2015 8:11
Target OS: Raspberry Pi
Domoticz version: beta
Location: right here
Contact:

Re: Find My iPhone implementation in LUA script

Post by mcmikev » Wednesday 14 September 2016 13:48

That is true. I really was thinking that I was doing it wrong ;-)

I like this idea and how you can use this.

Thanks!!!

cattoo
Posts: 55
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 » Wednesday 14 September 2016 14:05

Is the original script updated? So i could let go for the moment and fix everything after work?

mvzut
Posts: 272
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 » Wednesday 14 September 2016 14:10

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!
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

cattoo
Posts: 55
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 » Wednesday 14 September 2016 14:36

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

mvzut
Posts: 272
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 » Wednesday 14 September 2016 15:57

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.
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

cattoo
Posts: 55
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 » Wednesday 14 September 2016 16:12

Thank you, completely missed "Position Person1" text switch. My bad! Hopefully the "home" switch will activate when i get home :D

Egregius
Posts: 2318
Joined: Thursday 09 April 2015 12:19
Target OS: Raspberry Pi
Domoticz version: BetaRPi2
Location: Beitem, BE
Contact:

Re: Find My iPhone implementation in LUA script

Post by Egregius » Wednesday 14 September 2016 17:30

Yes, 5 minutes later :P

mvzut
Posts: 272
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 » Wednesday 14 September 2016 17:45

Egregius wrote:Yes, 5 minutes later :P
To be precise: somewhere between 0:01 and 4:59 minutes later
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

User avatar
deejc
Posts: 173
Joined: Tuesday 22 September 2015 18:50
Target OS: Raspberry Pi
Domoticz version: 3.5641
Location: UK
Contact:

Re: Find My iPhone implementation in LUA script

Post by deejc » Wednesday 14 September 2016 18:32

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 ?
Domoticz 3.5641 on RPI (Raspbian GNU/Linux 8)

mvzut
Posts: 272
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 14 September 2016 19:27

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...
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

User avatar
deejc
Posts: 173
Joined: Tuesday 22 September 2015 18:50
Target OS: Raspberry Pi
Domoticz version: 3.5641
Location: UK
Contact:

Re: Find My iPhone implementation in LUA script

Post by deejc » Wednesday 14 September 2016 19:38

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
Domoticz 3.5641 on RPI (Raspbian GNU/Linux 8)

User avatar
deejc
Posts: 173
Joined: Tuesday 22 September 2015 18:50
Target OS: Raspberry Pi
Domoticz version: 3.5641
Location: UK
Contact:

Re: Find My iPhone implementation in LUA script

Post by deejc » Wednesday 14 September 2016 19:46

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.
Domoticz 3.5641 on RPI (Raspbian GNU/Linux 8)

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests