Page 1 of 2

TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 22 August 2013 15:00
by spaanplaat
I wanted Domoticz to know when I am home and execute some events. In my example I will explain what I did to let the living room light switch on when it is past sunset and switch off the lights when I go to bed but only when I am home. Since I am no guru in programming I wanted to do this as simple as possible and some of the wiki info was not completely clear to me so excuse me if I dumb down some steps. The location trigger I’m using is Wi-Fi (since this is always turned on on my smartphone) but you could implement your own triggers. I hope this will help some people with new ideas for Domoticz.

What I needed:
Android phone with NFC (for NFC tag sleep) and NFC Task Launcher (NFCTL) or Tasker to create a location task and NFC tag task. I think any other smartphone with similar software should work as well.
Wi-Fi turned on on smartphone.
Dummy switch ’home’ and ’sleep’.


What I did:
I created a location task in NFCTL (you do not need NFC tags for NFCTL to work).
When connected to my home Wi-Fi run a certain URL in the background.
You can run an URL to switch a switch on or off with Domoticz. This way you can have certain software switch some appliances on or off with certain triggers (location, nfc etc.).
First I added a dummy switch which I called ‘Thuis’ (Home). The IDX number of the switch can be found on the devices page. In my example I use IDX 40.
I must note that the URL trigger only worked when I used the basic authentication method.


The URL I used is:

Code: Select all

http://(username:password)@(ip-address)/json.htm?type=command&param=switchlight&idx=(switch ID number)&switchcmd=On&level=0
So for example if your username is ‘test’ and your password is ‘welcome’, the IP-address is ‘192.168.1.1’ and the dummy switch you want to switch has the IDX ‘40’ the URL to switch it on will be as followed.

http://test:welcome@192.168.1.1/json.htm?type=command&param=switchlight&idx40&switchcmd=On&level=0

If you are using a DNS service you must route the correct port and add the port number in the url. For example domoticz.mydns.com:8080 instead of the internal IP when you want to trigger a switch over the internet.
At this point you can test your URL. When you try this in your browser you should receive the message { "status" : "OK", "title" : "SwitchLight" }. If you want to turn the switch off, simply change On to Off in the URL (case sensitive). This way you can turn all your switches on and off.

Next I created the Wi-Fi location task called ‘Thuis’ (home) in NFCTL. Simply add a task when connected to your home Wi-Fi, open URL in background and then use the example URL with your own credentials.
If you did it right NFCTL should give you the status last task: Thuis. If you check the dummy switch in Domoticz it should be on.

Now simply create a task when you are away from home. Mine is called ‘Niet thuis’ (not at home). Here you use the same URL but with Off instead of On.
Again if you did this right the dummy switch should turn off when you disconnect your Wi-Fi. Keep in mind when you do this you are not connected to your network so internal IP-addresses will not work. You must route the correct port and external IP or DNS.

Test the task simply by turning your Wi-Fi on your smartphone on and off to see if the switch turns on and off correctly.

Once you have this running you can create events upon switching on your dummy switch.

I also have an NFC tag on my bedside which I use to set my phone’s alarm and sounds settings to silent when I go to sleep. You can then create a second dummy switch in Domoticz which I called ‘Slaap’ (Sleep). In the NFC tag I added the task to open a URL in the background like the one we used before but this time for the dummy switch with the corresponding IDX.

Some NFC tags can store multiple tasks so you can also create a ’Wakker’ (Awake) task. The tags I use can do this so I add the second task to restore the sound settings to normal and open the URL witch turns the dummy switch off. This way when I go to bed I swipe my phone over the tag to execute the ‘Slaap’ task and when I am awake I swipe it again to execute the ‘Wakker’ task.

Now you can have an event when the dummy switch is turned on.
I am still playing around with the possibilities but what I have so far is the following blocky example.

There are 3 blocky events in the same example.
The first one is used to turn the living room light on if I’m home and not asleep and it is past sunset.
The second one turns all the lights off (I created a group with all the lights) if I’m home and when I go to sleep. This way the light will only turn on when it is dark and turn off when I swipe my phone over the NFC tag.
The third one simply switches off the living room light when I leave the house.
In the winter when I wake up before sunrise this way the living room light turns on when I am awake but turns off when I leave the house.

Even if you do not want any lights turned on or off you can still use the Wi-Fi / Dummy switch method to let your Domoticz know when you are home.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 22 August 2013 17:25
by willie-p
Very nice!
Your explanation is quite good. Do you know if there is a possibility to have this for the iphone?

kind regards,
Will

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 22 August 2013 18:56
by spaanplaat
As far as I know the iphones do not have nfc so perhaps you can use an alternate trigger method for the bedside? I don't know if there is an app like tasker on the iphone but I bet there is. With tasker you could create a gesture to execute an action. For example turn your phone upside down and then wave with it or something more subtle ;-)
You then create the option to open a certain url. It's no nfc swiping, but it should work. If you find such an app you can use it to trigger an action when connected to your home Wi-Fi.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Friday 23 August 2013 3:26
by CopyCatz
iPhone has geofencing, but no option to open a url. It might work if we build a specific iPhone app for domoticz which could use the geofencing api.
It might be easier to use the bluetooth option from here http://www.domoticz.com/forum/viewtopic ... ooth#p1680

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Monday 30 September 2013 12:36
by theolsen
I found a link to this on the micasaverde forum, it is a luup script that allows integration with Find My iPhone. I'm not a coder by any means but I wonder if some clever soul could make use of this for Domoticz.

https://gist.github.com/jasongill/5945055

-theolsen.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Wednesday 02 October 2013 11:51
by alfred_j_kwak
I just want to share this. I don't yet have activated dyndns, so my system is only in my local network. I made couple of scripts to monitor if my phone is inside my wlan.
It take while to understand why my tasker event work only when I came home, not when I left home. I was pretty sure that I have coded everything ok, but still no success. Then suddenly I saw the light. If I tried to trigger action in my home network after I have left my home network it really don't work... :)
I now really need scritp to launch event one minute before I'm out of my wlan radius.

-Jussi-

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 03 October 2013 8:48
by spaanplaat
alfred_j_kwak wrote:I just want to share this. I don't yet have activated dyndns, so my system is only in my local network. I made couple of scripts to monitor if my phone is inside my wlan.
It take while to understand why my tasker event work only when I came home, not when I left home. I was pretty sure that I have coded everything ok, but still no success. Then suddenly I saw the light. If I tried to trigger action in my home network after I have left my home network it really don't work... :)
I now really need scritp to launch event one minute before I'm out of my wlan radius.

-Jussi-
You can also use your external IP for this. DNS is not required but if you have a dynamic IP you have to periodically change the IP in the URL.
For example if your external IP is 80.80.50.201 you can use the following tasker url when leaving your home:

http://test:[email protected]:8080/j ... ff&level=0

Keep in mind that you have to forward your domoticz port in your router to be able to connect to domoticz outside your network.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 03 October 2013 16:33
by alfred_j_kwak
spaanplaat wrote:
You can also use your external IP for this. DNS is not required but if you have a dynamic IP you have to periodically change the IP in the URL.
For example if your external IP is 80.80.50.201 you can use the following tasker url when leaving your home:

http://test:[email protected]:8080/j ... ff&level=0

Keep in mind that you have to forward your domoticz port in your router to be able to connect to domoticz outside your network.
Unfortunately I have dynamic IP so I have to use dyndns. However system is now running and working as it should be. One thing to remember is use internal IP in Tasker when coming home, but dyndns url with password when going out.

Does anyone know if there is any possibility to do this same functionality with Windows phone? Mayby script that pings phone IP/MAC?

-Jussi-

EDIT: So ping is not suitable method to use with MACid. Maybe I should upgrade my router that I can connect MACs with IPs.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 03 October 2013 16:46
by Keptenkurk
I just installed the TouchDevelop app (free from Microsoft) on my WP7.8 Phone. It should be able to automate things but not to the extend possible with Tasker.
At first glance: it is able to access location and can send URL's so maybe...

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Sunday 06 July 2014 17:53
by hertzh1
Hi, I try to use this method to change switch state from internet, but it not work. I have redirected ports on my router, so I have acces from internet to my domoticz server (i can see it in my browser). I trying to change authentication metod from login page to basic, but still not work. If i use this method in my local network everything work OK. Meybe I must change something in my server settings. Please help me.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Sunday 06 July 2014 23:51
by xayide
I have made a script that checks my dd-wrt rputer if the MAC of myniphones are there or not. If they are it flips a virtual switch cslled HOME to on. If it doesnt find it it tries for 5 more minutes counting down and if still not found it flips the HOME switch to off. Worls pretty well I think. Tell me if you want me to post the script, but it foes a lot more so you will have to cut the iphone part.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Monday 07 July 2014 19:00
by hertzh1
I' m not have problem with do virtual switch using my wifi. The problem is to use json command from internet. I have Android phone and application HTTP Post, and a local profile application LAMA. This both applications works well, but json commands work only if I have connection to my local network (via Wifi). Becouse my house is like Faraday cage, and i must use 3, AP to have good connection i try to use json with 3G connection of my phone. And this is a problem domoticz server works well application domotica (on my phone) can see server on 3g connection but Json dosen't work on 3g connection. I have public IP and dont use any dns service (connect to my IP addres directly). I change authentication metod to basic, use user name and password in url, but i dont have connection, and any replay from server.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Monday 30 November 2015 9:38
by jake
hertzh1 wrote:I' m not have problem with do virtual switch using my wifi. The problem is to use json command from internet. I have Android phone and application HTTP Post, and a local profile application LAMA. This both applications works well, but json commands work only if I have connection to my local network (via Wifi). Becouse my house is like Faraday cage, and i must use 3, AP to have good connection i try to use json with 3G connection of my phone. And this is a problem domoticz server works well application domotica (on my phone) can see server on 3g connection but Json dosen't work on 3g connection. I have public IP and dont use any dns service (connect to my IP addres directly). I change authentication metod to basic, use user name and password in url, but i dont have connection, and any replay from server.
Hi, I have exactly the same problem. When trying to give the command through, the external IP address (access through the router), login is required. This works ago for the browser access, but the http command: http://username:password@external ip-address gives in Firefox a message box that username and password are not required, but the page shows a "401 not authorized" message. Internet Explorer doesn't even want to process the command when username and password are in the http phrase.

Somehow the string is not correct, but I did it according to the Wiki: http://<username:password@>domoticz-ip<:port>/json.htm?api-call

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Monday 30 November 2015 10:34
by ThinkPad
I used this script: http://domoticz.com/wiki/Presence_detection for months. It worked fine, pinging my phone to see if it is connected to the WiFi-network at my home.

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Monday 30 November 2015 12:47
by jake
ThinkPad wrote:I used this script: http://domoticz.com/wiki/Presence_detection for months. It worked fine, pinging my phone to see if it is connected to the WiFi-network at my home.
Yes, that probably works, but nowadays you can use the standard 'System Alive Checker' for that. I want my phone to send (push) the command to Domoticz, because I let Llama detect if the phone connects to my home network. If YES, then turn on the lights (under Domoticz conditions xyz) immediately. I can't wait for the x minute delay caused by the ping routine.

We should separate the 'let Domoticz know I am home' from the not working method by sending a json command while username / password are not accepted. As stated, the command works as expected when logged into the local network (that part therefore works with my Llama event), but not when I leave my local network, than I need the external IP address with login to work)

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Sunday 06 December 2015 21:48
by hertzh1
I have the same problem with domoticz json command everything working ok when I'm in my local network, but with GSM not. I have redirected ports in my router and I can connect to domoticz from outside with application and also with browser but not with json command.
Is there any method to use json commend outside local network, or maybe its using other then browser port or protocol.
I think pinging will be god idea to check if user is present (and other very useful function) but it is not implemented in domoticz server application now. :(

Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 18 February 2016 22:48
by jake
Does somebody have a solution to connect by using a JSON command with username and password?

Verstuurd vanaf mijn Transformer TF101 met Tapatalk

Re: RE: Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Thursday 18 February 2016 23:35
by RayAmsterdam
jake wrote:Does somebody have a solution to connect by using a JSON command with username and password?

Verstuurd vanaf mijn Transformer TF101 met Tapatalk
http://username:password@ip:port/json.htm.......

Re: RE: Re: RE: Re: TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Saturday 20 February 2016 23:29
by jake
RayAmsterdam wrote:
jake wrote:Does somebody have a solution to connect by using a JSON command with username and password?

Verstuurd vanaf mijn Transformer TF101 met Tapatalk
http://username:password@ip:port/json.htm.......
Unfortunately this doesn't work. Most browsers don't accept this type of link anymore. I can have the command working without problems via the internal network since authentication is then not necessary.

TUT > Let Domoticz know when you are home. (Cell Wi-Fi)

Posted: Sunday 11 September 2016 11:39
by mvzut
theolsen wrote:I found a link to this on the micasaverde forum, it is a luup script that allows integration with Find My iPhone. I'm not a coder by any means but I wonder if some clever soul could make use of this for Domoticz.

https://gist.github.com/jasongill/5945055

-theolsen.
I realize I'm quoting a very old message, but I wanted to share that this is exactly what I have been doing today: using Jason's excelent lua code to get location data from Apples "Find My iPhone" service. Here's my implementation of his code (put this in "~/domoticz/scripts/lua/script_time_checkphones.lua" or something):

Code: Select all

-- Script to check the location of multiple iPhones every 5 minutes

commandArray = {}
local m = os.date('%M')
if (m % 5 == 0) then

  json = require('json')

  -- Array of users to be checked
  users = {
            Person1 = {username = '[email protected]' ; password = 'password1' ; devicename = 'iPhone Person1};
            Person2   = {username = '[email protected]' ; password = 'password2' ; devicename = 'iPhone Person2'};
            Person3  = {username = '[email protected]' ; password = 'password3' ; devicename = 'iPhone Person3'}
          }
  -- The latitude and longitude of your house (use Google Maps or similar to find this)
  homelongitude = 1.23456789
  homelatitude = 9.87654321
  -- 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

  for user,credentials in pairs(users) do
    stage1command = "curl -s -X POST -D - -o /dev/null -L -u '" .. credentials.username .. ":" .. credentials.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/" .. credentials.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 -X POST -L -u '" .. credentials.username .. ":" .. credentials.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/" .. credentials.username .."/initClient"
    local handle = io.popen(stage2command)
    local result = handle:read("*a")
    handle:close()
    --print(result)

    output = json:decode(result)
    athome = false
    for key,value in pairs(output.content) do
      if value.name == credentials.devicename then
        lon = value.location.longitude
        lat = value.location.latitude
        --print('Position iPhone ' .. user .. ': ' .. lon .. '  ' .. lat)
        if homelongitude - skew < lon and homelongitude + skew > lon and homelatitude - skew < lat and homelatitude + skew > lat then
          athome = true
        end
        if athome then
          --print('iPhone ' .. user ..' is at home')
          if otherdevices['iPhone ' .. user] == 'Off' then commandArray['iPhone ' .. user] = 'On' end
        else
          --print('iPhone ' .. user ..' is not at home')
          if otherdevices['iPhone ' .. user] == 'On' then commandArray['iPhone ' .. user] = 'Off' end
        end
      end
    end
  end

end
You first have to download a JSON decoder, e.g. this one: http://regex.info/blog/lua/json and copy the file (json.lua) somewhere where it can be found (in my case this was /usr/local/share/lua/5.2/). I think you can also append a path after the "require" command.
Replace the information in the "users" array with your own credentials, you can add as many users as you want in new lines (pay attention to the correct placement of brackets, parentheses etc.). The device name has to be filled in exactly right (note the use of capitals etc.). Fill in your home location under homelongitude and homelatitude.
This script updates a device called "iPhone Person1" etc. for each user (where "Person1" is obviously replaced with what you filled in in the "users" array). These are dummy switches which should first be created with the Dummy hardware component.

If you want the script to check more or less often, change the "5" in the third line into something else (number is in minutes and should be between 1 and 59). I actually have no idea whether checking every 5 minutes drains the iPhones' batteries, I guess I'll find out in the coming days...

Use it as you like, change or extend it, whatever you wish. Please don't forget that most of the credits go to Jason Gill (thanks Jason!). If you have nice improvements or additions, please share so that others can benefit too! I am currently trying to add reverse address lookup via a Google Maps API, so that you can print the exact address of all iPhones in a dummy text device. Or is that a bit too much "Big Brother"? ;-)

P.S. Ultimately, it would of course be great if we could create a real hardware plugin from this, such as the one for the Vera system (http://forum.micasaverde.com/index.php?topic=16907.0. Even though this shouldn't be too hard with this working "proto" and the Vera plugin as examples, I'm afraid it's one or two bridges too far for me.