We have moved at community.getvera.com

Author Topic: Luup script to see if you are home using Find My iPhone  (Read 133940 times)

Offline alexzen

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #30 on: August 05, 2013, 11:19:10 am »
Hello

Firstly a big thank you for sharing!

But the code does not work for me, is that someone managed to make it work?

I do a test on a variable areyouhome vswitch but it never switches.

Thank you in advance for your answers!

Offline duiffie

  • Full Member
  • ***
  • Posts: 126
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #31 on: August 05, 2013, 02:31:28 pm »
To overcome all errors and to be sure it'll work with a scene, do the following:

1. install the DataMine app that has the json-dm library, you can uninstall it afterwards if you don't need it (the library won't be removed)
2. create a new vswitch device and turn it to OFF
3. create a new scene and:
     - in the devices tab, turn your vswitch device (step 2 above) to ON
     - paste the code from post #1 into the luup tab
     - add the -k option to both the curl commands (see post #24 in this topic)
     - insert your home location (lon,lat), device name  and icloud username/password
4. confirm the changes to the scene and then save all your changes (the red button)

If you are at home and run the scene manually now, the vswitch device should turn ON. If you want to turn it OFF when you're not at home (and maybe do more stuff) you need to add the following code to the luup tab of the above scene. Place it right above the last ('return areyouhome') line:

Code: [Select]
if areyouhome == false then
        --- set the virtual switch to off to indicate you're not at home
        luup.call_action("urn:micasaverde-com:serviceId:HomeAutomationGateway1","RunScene",{ SceneNum="<number>" }, 0)
end

change <number> to the number of another scene in which you turn the vswitch to OFF (and turn off the lights too for example)

Now you can add a schedule to the scene created in step #3 to run that scene automatically every x minutes.

If the above doesn't work please provide some log errors.
« Last Edit: August 05, 2013, 03:31:03 pm by duiffie »
Vera2 (1.5.622)

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #32 on: August 05, 2013, 03:25:21 pm »
Duiffie, all works except for not being at home. I can seem to get that part working. I even changed the longtitude, switched findmyiphone off, but the button does not go to off via my scene
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline duiffie

  • Full Member
  • ***
  • Posts: 126
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #33 on: August 05, 2013, 03:35:14 pm »
what happens if you run the second scene manually? is your vswitch turned off then? If that works, check again if that scene number is exactly the same as the one used in the luup code of the first scene.
« Last Edit: August 05, 2013, 03:39:37 pm by duiffie »
Vera2 (1.5.622)

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #34 on: August 05, 2013, 03:39:43 pm »
It looks like it does not look at my location. Running the scene set the switch to on, although the longtitude does not make sense.

To answer your previous question, manually the scene sets the device to off and scene no is correct
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline duiffie

  • Full Member
  • ***
  • Posts: 126
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #35 on: August 05, 2013, 03:43:50 pm »
Please check your logs while you run the scene, maybe you'll find some errors there
Vera2 (1.5.622)

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #36 on: August 05, 2013, 03:45:27 pm »
I was just about to:

01   08/05/13 21:44:13.909   ctrl_chr[31;1mLuaInterface::CallFunction_Scene Scene  65 failed [string "function event_1()..."]:286: attempt to concatenate global 'stage2server' (a nil value)ctrl_chr[0m <0x2f886680>

scene 65 is the initial scene
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline duiffie

  • Full Member
  • ***
  • Posts: 126
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #37 on: August 05, 2013, 03:54:43 pm »
Check if you added the curl -k option. If so, try installing the DataMine app again. Then run the scene, without uninstalling the DataMine app.
« Last Edit: August 05, 2013, 03:57:34 pm by duiffie »
Vera2 (1.5.622)

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #38 on: August 05, 2013, 04:00:12 pm »
that was the trick putting the -k .........!! Thx for the support. I hope someone will find the time to create a plugin
« Last Edit: August 05, 2013, 04:03:46 pm by Crismaison »
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline duiffie

  • Full Member
  • ***
  • Posts: 126
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #39 on: August 05, 2013, 04:01:17 pm »
you're welcome :)  remember: -k/--insecure      Allow connections to SSL sites without certs
« Last Edit: August 05, 2013, 04:09:21 pm by duiffie »
Vera2 (1.5.622)

Offline alexzen

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #40 on: August 06, 2013, 04:17:04 am »
Thank you!!!!

Instead of a "return areyouhome" I put directly change the vswitch with:

if areyouhome == false then
luup.variable_set ("urn:upnp-org:serviceId:VSwitch1", "Status",0,<yourvswitchnumber>)
else
luup.variable_set ("urn:upnp-org:serviceId:VSwitch1", "Status",1,<yourvswitchnumber>)
end

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #41 on: August 06, 2013, 04:23:36 am »
@ alexzen -> nice !

Anyone a suggestion for 2 Iphones? I can run 2 scenes with 2 virtual switches, but isn't there a prettier solution
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline alexzen

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #42 on: August 06, 2013, 09:55:43 am »
My script with my 2 iPhones :) :

--- Your iCloud username (it's an email address)
username = "****"
--- Your iCloud password
password = "****"
--- Your device name as it appears in Find My iPhone - note the non-standard apostrophe that may be used in your device name
deviceAlex = "iPhone de Alexandre"
deviceCamille = "iPhone de Camille"
--- The latitude and longitude of your house (use Google Maps or similar to find this)
homelatitude = ***
homelongitude = ***
--- This skew value will be used for a rudimentary math comparison; if your current longitude or latitude are more than this number different from your home longitude/latitude, you will be considered not home
skew = 0.003
 
json = require('json-dm')
stage1command = "curl -s -k -X POST -D - -o /dev/null -L -u '" .. username .. ":" .. password .. "' -H 'Content-Type: application/json; charset=utf-8' -H 'X-Apple-Find-Api-Ver: 2.0' -H 'X-Apple-Authscheme: UserIdGuest' -H 'X-Apple-Realm-Support: 1.0' -H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' -H 'X-Client-Name: iPad' -H 'X-Client-UUID: 0cf3dc501ff812adb0b202baed4f37274b210853' -H 'Accept-Language: en-us' -H 'Connection: keep-alive' https://fmipmobile.icloud.com/fmipservice/device/" .. username .."/initClient"
 
local handle = io.popen(stage1command)
local result = handle:read("*a")
handle:close()
 
stage2server = string.match(result, "X%-Apple%-MMe%-Host%:%s(.*%.icloud%.com)")
 
stage2command = "curl -s -k -X POST -L -u '" .. username .. ":" .. password .. "' -H 'Content-Type: application/json; charset=utf-8' -H 'X-Apple-Find-Api-Ver: 2.0' -H 'X-Apple-Authscheme: UserIdGuest' -H 'X-Apple-Realm-Support: 1.0' -H 'User-agent: Find iPhone/1.3 MeKit (iPad: iPhone OS/4.2.1)' -H 'X-Client-Name: iPad' -H 'X-Client-UUID: 0cf3dc501ff812adb0b202baed4f37274b210853' -H 'Accept-Language: en-us' -H 'Connection: keep-alive' https://" .. stage2server .. "/fmipservice/device/" .. username .."/initClient"
 
local handle = io.popen(stage2command)
local result = handle:read("*a")
handle:close()
 
output = json.decode(result)
areyouhomeAlex = false
areyouhomeCamille = false
for key,value in pairs(output.content) do
if value.name == deviceAlex then
if (homelongitude - skew < value.location.longitude and homelongitude + skew > value.location.longitude and homelatitude - skew < value.location.latitude and homelatitude + skew > value.location.latitude) then
areyouhomeAlex = true
end
end
if value.name == deviceCamille then
if (homelongitude - skew < value.location.longitude and homelongitude + skew > value.location.longitude and homelatitude - skew < value.location.latitude and homelatitude + skew > value.location.latitude) then
areyouhomeCamille = true
end
end
end

if areyouhomeAlex == false then
-- change with your vswitch !!!!
luup.variable_set(VAR_VSWI,"Status",0,VSWI_ALEX)
elseif areyouhomeAlex == true then
change with your vswitch !!!!
luup.variable_set(VAR_VSWI,"Status",1,VSWI_ALEX)
end

if areyouhomeCamille == false then
luup.variable_set(VAR_VSWI,"Status",0,VSWI_CAM)
elseif areyouhomeAlex == true then
luup.variable_set(VAR_VSWI,"Status",1,VSWI_CAM)
end

Offline Crismaison

  • Sr. Member
  • ****
  • Posts: 451
  • Karma: +2/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #43 on: August 06, 2013, 10:00:39 am »
Great, just told my GF to install the FindMyIphone app, in case she looses her Iphone  ;D

What would also be cool to see on a map to see in the Vera where the iphone is (no connection to the statement above), but I was just checking some GPS trackers to put in the car or give to my childeren.


@Alexzen, really great, just to remarks, but please correct me if I am wrong

if areyouhomeAlex == false then
-- change with your vswitch !!!!
luup.variable_set(VAR_VSWI,"Status",0,VSWI_ALEX)
elseif areyouhomeAlex == true then
change with your vswitch !!!! -- change with your vswitch !!!
luup.variable_set(VAR_VSWI,"Status",1,VSWI_ALEX)
end


if areyouhomeCamille == false then
luup.variable_set(VAR_VSWI,"Status",0,VSWI_CAM)
elseif areyouhomeAlex == true then elseif areyouhomeCamille == true then
luup.variable_set(VAR_VSWI,"Status",1,VSWI_CAM)
end
« Last Edit: August 06, 2013, 02:53:46 pm by Crismaison »
Vera lite - Everspring smoke & flood detectors - Fibaro doorsensors - 2 Foscams - Greenwave 6 node smartplug - Several Switches -Netatmo - PLEG - Twilio - DropBoxuploader - FindmyIphone user

Offline alexzen

  • Newbie
  • *
  • Posts: 9
  • Karma: +0/-0
Re: Luup script to see if you are home using Find My iPhone
« Reply #44 on: August 07, 2013, 03:49:46 am »
exact !!

But i test in a scene with 5 minutes of schedule ... my battery is out :( 30% off in one night. I think use my wifi connection to set my presence at home.
« Last Edit: August 07, 2013, 04:50:28 am by alexzen »