Use iPhone presence status from Phillips Hue hub to tell when I arrive at or leave my home (geofencing)

Like many OpenHAB users, I wanted to create rules based on when my wife and I arrived or left our home. When I enabled the “Home Location” settings in my Hue iPhone app, I noticed a new item in the CLIP API Debugger. Then I realized I had an easy geofencing solution already in my pocket.

Here’s what the iPhone presence looks like in the Hue CLIP API Debugger (https://www.developers.meethue.com/documentation/getting-started).


"13": {
		"state": {
			"presence": false,
			"lastupdated": "2018-04-16T12:05:02"
		},
		"config": {
			"on": true,
			"reachable": true
		},
		"name": "iPhone",
		"type": "Geofence",
		"modelid": "HA_GEOFENCE",
		"manufacturername": "Philips",
		"swversion": "1.0",
		"uniqueid": "L_02_Z4AX6",
		"recycle": false
	},

With this info I created OpenHAB items like this:

String	HUE_Presence_Joseph_iPhone <present>	(gSkur)	{ http="<[HTTP://10.0.0.126/api/<API KEY>/sensors/13:3000:JS(getHuePresence.js)]" }

and getHuePresence.js

(function(i) {
    var json = JSON.parse(i);
    return ((json['state']['presence'])) == true ? "ON" : "OFF";
})(input)

The == true ? “ON” : “OFF”; is needed due to a problem with using “true” in OpenHAB rules (Item item_name changed from true to false does not work).

So far, I have just played with voice & text notifications using the new binding to control amazon echo devices (https://github.com/openhab/openhab2-addons/issues/3083).

import org.joda.datetime.DateTime

rule "Hue Joseph Presence OFF" 

when
Item HUE_Presence_Joseph_iPhone changed from ON to OFF

then
	sendMail("4153850297@vtext.com", "HUE_Presence_Joseph_iPhone", "Item HUE_Presence_Joseph_iPhone changed from ON to OFF.")
	logInfo("HUE", "HUE_Presence_Joseph_iPhone: " + HUE_Presence_Joseph_iPhone.state)

end

rule "Hue Joseph Presence ON" 
when
Item HUE_Presence_Joseph_iPhone changed from OFF to ON
then
sendMail("4153850297@vtext.com", "Security Master Bedroom", "Item HUE_Presence_Joseph_iPhone changed from OFF to ON.")
logInfo("HUE", "HUE_Presence_Joseph_iPhone: " + HUE_Presence_Joseph_iPhone.state)
//hue_0100_0017882c2e3c_23_brightness.sendCommand("100") 
if (HUE_Presence_Rachel_iPhone.state == ON )  //If my wife is home, have Alexa tell her so her boyfriend has time to get the hell out of the house.
{
	if(now.getHourOfDay > 9 && now.getHourOfDay < 22  )  //but only between 9am and 10om
{
	//FF_Office_Light.sendCommand(100)		
	OfficeBot_Remind.sendCommand("Joseph is home") 
	Bedroom_Remind.sendCommand("Joseph is home") 
	//hue_0100_0017882c2e3c_23_brightness.sendCommand("2") 
	logInfo("HUE", "HUE_Presence_Joseph_iPhone: its between 9 and 22: " + now.getHourOfDay)
}
	else
{
	//hue_0100_0017882c2e3c_23_brightness.sendCommand("2") 
	logInfo("HUE", "HUE_Presence_Joseph_iPhone: its NOT between 9 and 22: " + now.getHourOfDay)
}
	
}

end

Also when you are using the Hue CLIP API Debugger you can simulate arrival and departure like this:
image

OR conversely using

{"state":{"presence":true}}

or

{"state":{"presence":false}}

So you don’t need to leave your house to test your rules.

3 Likes

Thanks for posting. You should put the JSON in code fences like you did with the JS transform and your Rules. Looks great. We can always use more ways to detect presence.

Hello,

thx for this Tutorial, my Status is working but i have a Problem with my rule.

my item:

String HUE_Presence_Christian_iPhone <present>	{ http="<[HTTP://192.168.178.81/api/i added my id here/sensors/10:3000:JS(getHuePresence.js)]" }

and i created a button Hubpanel an this button says on

so i created a rule for my frontdoor:

// Status Wohnungstür niemand Zuhause

rule "Wohnungstür geöffnet"
when
    Item Tuer_Wohnung_OpenStatus changed to OPEN

then
      if (HUE_Presence_Christian_iPhone.state==ON) {
          sendBroadcastNotification("Wohnungstür wurde geöffnet niemand ist Zuhause")
          EchoStube_Speak.sendCommand("Wohnungstür wurde geöffnet")
      }
end



rule "Wohnungstür geschlossen"
when
    Item Tuer_Wohnung_OpenStatus changed to CLOSED
then
      if (HUE_Presence_Christian_iPhone.state==OFF) {
          sendBroadcastNotification("Wohnungstür wurde geschlossen Zuhause")
          EchoStube_Speak.sendCommand("Wohnungstür wurde geschlossen")
      }
end

fot testing i set the state in one rule ON and in the other to OFF

but i didn’t get any messages, if i remove "if (HUE_Presence_Christian_iPhone.state==OFF) { } " i get the messages.
Is there an mistake in my rule? I have no idea and i am really bad at this.

Maybe someone can help oder improve the rule.

I change my item from String to Switch an now it works any Idea why?

There is not such thing as a State item afaik

Your item is a String:

String HUE_Presence_Christian_iPhone <present>	{ http="<[HTTP://192.168.178.81/api/i added my id here/sensors/10:3000:JS(getHuePresence.js)]" }

So in your rule you need to check for a string “ON” or “OFF”

rule "Wohnungstür geöffnet"
when
    Item Tuer_Wohnung_OpenStatus changed to OPEN

then
      if (HUE_Presence_Christian_iPhone.state=="ON") {
          sendBroadcastNotification("Wohnungstür wurde geöffnet niemand ist Zuhause")
          EchoStube_Speak.sendCommand("Wohnungstür wurde geöffnet")
      }
end



rule "Wohnungstür geschlossen"
when
    Item Tuer_Wohnung_OpenStatus changed to CLOSED
then
      if (HUE_Presence_Christian_iPhone.state=="OFF") {
          sendBroadcastNotification("Wohnungstür wurde geschlossen Zuhause")
          EchoStube_Speak.sendCommand("Wohnungstür wurde geschlossen")
      }
end

BUT

You will be better off changing your item to a Switch item
Because it can only be two states ON or OFF anyway

Switch HUE_Presence_Christian_iPhone <present>	{ http="<[HTTP://192.168.178.81/api/i added my id here/sensors/10:3000:JS(getHuePresence.js)]" }

If you do that then if (HUE_Presence_Christian_iPhone.state==ON) { will work

Thx for you help :slight_smile: I change it to a Switch.

otherwise
Can you explain “how i check for a string”? for my knowledge

If it’s a String then you some smething like this

if (value == "String") { ....

String in ""

If your item is a Switch then you can test for ON of OFF
If it’s a Number then you can do == 1 or >20 or whatever
If it’s a Contact Item then the allowed values are OPEN and CLOSED

But String are in ""

Thanks for this finding. Do you got, any experience on reliability and power consumption on the phone?

My iPhone 6 works perfectly but has the battery issues for which apple is doing replacements at a reduced price. The Hue presence works most of the time may does not work 1/10 of the time. My wife’s iphone Se is much more reliable but it also has not worked correctly maybe 1/30 days.

-Joseph

Does anyone know if this still works ?
mine just stopped working ?

I have made a new user via. the debugger but still nothing and
my rule stil work ,but no action when i leave or come home ,it only works via an hue light bulb
turns on/off fine via the hue hub so connections there from openhab to phillips hue hub ,i`m a littel lost
of how to do more fault finding of what might have happened ,it worked fine before.

Simon, did you get anywhere with working out what the problem was.
I have set up a hue Geofence thing and item and get the same behaviour that a linked hue light turns on or off depending on if I’m home but the Geofence item never updates.

Any pointers, assuming you got it working again?

Hello there

I am trying to also use the hue geofence Sensor, that automatically gets added to the inbox.
Things and items have been created through the UI.

The items however seem to never get updated. The geofence trigger works in the hue App.

I am using openHAB 3.2.0. new installation on an RaspBerry Pi 3 B+ (latest version of openHABian)

Has anyone got this working? If so, how?