PeteLawrence/homebridge-people

Occupancy state change triggered via ping not immediate in home app

Opened this issue · 61 comments

Hi there - apologies if this is a duplicate.
I've noticed that if I make a call via a webhook to change the state of the sensor, the change is immediate in the home app:

Feb 25 12:52:21 raspberrypi homebridge[29674]: [2/25/2017, 12:52:21 PM] Received hook for paul -> false Feb 25 12:52:21 raspberrypi homebridge[29674]: [2/25/2017, 12:52:21 PM] Running hook for 192.168.0.22 -> false Feb 25 12:52:22 raspberrypi homebridge[29674]: [2/25/2017, 12:52:22 PM] Changed occupancy state for 192.168.0.22 to false. Last successful ping 2017-02-25T12:52:19+00:00 , last webhook 2017-02-25T12:52:21+00:00 .

However, when a ping detects the occupancy change, the status is not updated in the home app until I restart it? Is this expected behaviour?

Feb 25 12:51:25 raspberrypi homebridge[29674]: [2/25/2017, 12:51:25 PM] Changed occupancy state for 192.168.0.22 to true. Last successful ping 2017-02-25T12:51:25+00:00 , last webhook none .

Thanks,
Paul

Should be same as #27.

Thanks @benzman81 - I'm not sure it is quite the same, because the webhooks update the occupancy sensors immediately without fail, but the changes initiated by the ping/timeout do not. Is the state refresh achieved differently? Thanks again for your tips and tricks.

The both use the same method to update the state.

So I think I've got a solution for this - I added to the http server so if you call /?sensor=all it returns a 1 or a 0 based on if any of the sensors are tripped. Check my fork if you'd like. Then I hooked that up to the homebridge-http-advanced plugin which does realtime polling and pushing of data to homekit. It's working so far for me!

How would we be able to test this out to see if it works 🙂?

If you clone my homebrige-people fork, and install my fork of homebridge-http-advanced then you can set up an Occupancy accessory and test it yourself. I've had it running for a couple of hours and it seems to work well. Let me know if it works for you

Is it necessary to configure the http advanced in the config or just use your version of people in the config file?

Yes, you have to configure http advanced as well, to tell it what url to check. If you're using my version of http advanced, this is the config I use:

{
        "accessory": "Http-advanced",
        "name": "Occupancy Sensor",
        "switchHandling": "yes",
        "http_method": "GET",
        "service": "Occupancy",
        "status_url":  "http://YOUR LOCAL IP HERE:51828/?sensor=all",
        "username" : "",
        "password" : "",
        "sendimmediately": ""

       },

If you're using the original http-advanced, set "service" to "Motion" and it will create a motion sensor device instead

@OultonD I know I'm going to sound like an idiot, but I don't know how to install this on a raspberry pi by cloning a repo....or how to install your version of homebridge-people...

@erfinb Google will probably help the most... but to clone the repo git clone <URL> in /usr/lib/node_modules. It may also be in /usr/local/lib You'll also have to install the dependencies in a sub folder node_modules as well. I'm not sure of an easy way to do that, I just copied a folder from another home bridge plugin. To install my version of homebridge-people you can just replace the existing index.js with mine. I've literally just started working with nodejs/home bridge in the past 48h so I can't be much more help, sorry!

@erfinb I did a little googling and I guess with npm you install a node git repo with npm install git+URL

That worked 🙂 Everything is installed, I will update with how things are going!

@OultonD one more question, is this supposed to be updating each person? I appear to have an added sensor literally called "Occupancy Sensor".

@OultonD so far, I still have to open the Home app to get automations to trigger...

Can you post your config.json for http advanced as well as homebridge people? My setup only updates automatically for the "anyone" sensor, not individuals.

Here's a copy of my configs. I have Ping enabled but mostly use webhooks for people. is there a way to have your enhancement look for each of these users as well as the anyone occupancy sensor? Some of my triggers are based on specific users, or at the very least Anyone and No one occupancy. :-)

"accessories": [
{
"accessory": "Http-advanced",
"name": "Occupancy Sensor",
"switchHandling": "yes",
"http_method": "GET",
"service": "Occupancy",
"status_url": "http://10.0.1.28:51828/?sensor=all",
"username" : "",
"password" : "",
"sendimmediately": ""
}

],

"platforms": [
  {
    "platform" : "People",
    "people" : [
      {"name" : "George", "target" : "10.0.1.35"},
      {"name" : "Amanda", "target" : "10.0.1.36"},
      {"name" : "Fred", "target" : "10.0.1.37"}
    ],
    "anyoneSensor" : true,
    "nooneSensor" : true,
    "cacheDirectory": "/var/homebridge/storage",
    "ignoreReEnterExitSeconds": 0
},

Your config looks good. The realtime trigger you should use for automation is the sensor created by http advanced. My changes only work for checking if anyone is detected (you can set the "anyoneSensor" to false and it'll still work). It would be fairly trivial to add support for checking individuals, but I think you would end up with two sensors for each person showing up in HomeKit. You're welcome to take a crack at making the changes though.

@benzman81 is there anyway we can take what he has developed to apply to the entire platform to make sure things are constantly being checked and sent to HomeKit?

If you install the plugin via his git url what else would you want to test? Maybe I dont get your question :-)

Well it does continous polling and updating. For me, I wouldnt want to program around a bug of apple, since this issue is not only in the people plugin. But I am not the maintainer, just a contributor.

@erfinb @benzman81 Can you try out my "realtime" branch? I added realtime polling to all sensors and it seems to be working for me. It's stand alone, doesn't need http-advanced or anything. Like you said, it's working around a bug of Apple's, so I'm going to make it into a separate plugin, homebridge-people-realtime, eventually.

hey @OultonD how do I go about pulling down your branch? I'd love to give it a try!

I will go for it as well, I'm just trying to remember how to pull a branch as well. I know @benzman81 has taught me before, but I cannot find it in my history. :)

Wait, I think I remember for example you would go to his branch and then use the command (for a Raspberry Pi)

sudo npm -g install git+https://github.com/OultonD/homebridge-people-realtime.git

Don't install that repo yet, I'm still updating it into its own plugin. If you've git clone'd my homebridge-people, try git checkout realtime

Haven't done anything yet on this side, let us know when it's ready to test and perhaps a command like I had before pointing to your plugin :) Thanks again for you hard work!

Alright, give it a shot! npm -g install git+https://github.com/OultonD/homebridge-people-realtime.git should do the trick and then if you change the platform to RTPeople in config.json you should start using the realtime plugin.

@OultonD why didn't you fork the project? at least github doesnt show that your repo was forked.

@benzman81 I'm not very good with git or github. I did fork the project and then I pushed the branch into its own repo. Is there a better way to do it?

@OultonD Yes, go to https://github.com/PeteLawrence/homebridge-people. In the upper right corner there are three Buttons "Watch/Unwatch", "Start/Unstart", and "Fork". Just click "Fork" and github will create a fork of this repository for you.

Well it depends on what you want. Forking makes sense if you might want to create a pull request so that your changes go into the original plugin. If not, than a new repository can make sense. But remember, new. Hanges in people plugin will not be able to be merged directly in you new repository. So you might do the merges yourself.

Well it looks like it is updating the changes but HomeKit still doesn't take any action unless the Home App is opened. :(

@benzman81 ok, thanks. Like I said, I have no experience collaborating on github so this is all kind of new to me. If there's interest in merging my changes then I'm happy to try to learn how to do that! But if @erfinb is having trouble maybe it should stay in it's own repo for now?

@erfinb I'm not sure how to troubleshoot for you. Did you install people-realtime? Can you post your config? It's definitely working for me, I've had it running for over 12h now

I'm curious to know where this goes... I'm experiencing the same issues where the automation not always works and I'm using iOS 10.3 beta 4

I've updated my system to use RTPeople and my automations still do not fire all the time. Last night when I got home the arrive Home scene worked however this morning my leave home scene didn't. I'm not using ping though I'm only using webhooks. Don't know if this makes a difference.

I'll be honest, I dont understand the value of using ping which is why I use webhook. By the very nature of Arriving Home or Leaving Home you wont be on the network or dont join straight away. I was also having issues with the lights turning on or off even when I was at home.

I'm sure changing the timeout would help but I did a lot of tinkering in the past and gave up. I just could not find a sweet spot. My lights would come on at 4am which annoyed me no end so was so happy when webhook / geofence were introduced. (Gutted this doesn't work all the time though)

From all my testing it's not the plugin at fault. It all boils down to the automations not firing if the sensor state changes. I can see webhooks get sent and i can see that webhooks are received but it doesn't alsways result in a automation firing.

Am I right in thinking that RTPeople only does it's magic with the anyone sensor and is not person specific. My arrive home automations are based on individuals arriving home. My Leave Home however looks at the anyone sensor.

Perhaps I'll give ping another go now that i've installed RTPeople. Just out of interest, what setting have people got? I've got an iPhone 7 Plus and my wife has got a iPhone 6s.

I think we have different use cases for the plugin, which is why it's working better for me than for others. I use it sort of as an extra switch for my automations, rather than a trigger. For example, I have an ambient light sensor set up to turn on my lights if it gets below a certain threshold, but only if the home is occupied.

I use an iPhone 6s, and my partner uses a 5s. I also use the Elgato Eve app to create my automations instead of the Home app, which might make a difference. Another thing to look at would be /var/log/homebridge.log which should have timestamps for when the sensors are updated. You can use that data (especially overnight data) to find the sweet spot for the timeout for each phone.

Apologies for coming back to this late. I've had a little more success with this since using the RTPeople plugin. Certainly the home app now updates immediately on almost every occasion with both the pings and the webhooks. The scenes still do not fire reliably, now in either case. I now have started to put this down to the new iOS 10.3 Beta 4 (which I'm also on) although it's largely my "Leaving Home" scene which fails to fire and should be the responsibility of my Apple TV 4 to fire right?

I share your sentiments @erfinb everyone works so hard to make this plugin awesome and everyone is so helpful and friendly. The whole Hombridge community is great, I use a few other plugins too any they are all awesome.
@OultonD thanks for the hardwork on RTPeople folk, much appreciated. Yeah it sounds like we might have slightly different use cases for the plugin however I do think we have an overlap. I did you the Elgato Eve app but I've started to use the Hesperus app now as this has the funstonality to use != something.

So to be clear I use occupancy for the following things:

Arrive Home
Leave Home
Set Lights at Sunset (if someone is at home)
Set Lights at Twilight (if someone is at home)

What's weird is it tends to be the Arrive Home and Leave Home scenes which are the most flaky. When i'm at home the automations based on occupancy work fine. Hmmmm everytime i think about this it just doesn't make sense.

@jaquesp yep i would agree, it's mainly my Leave Home scene which is flaky too. Yep the Apple TV (or iPad if using this as a hub) is responsible for automations. I've tried having the Apple TV awake or asleep, wired and wireless to see if this makes a difference but it doesn't. the automations are still unreliable.

Could you raise it as an issue as part of the beta please? I used to take the betas but i haven't since i got my new iPhone.

What makes me laugh is Apple is having a bit of a marketing campaign at the moment around how great homekit is (probably in responce to Amazon and Google)but they can't even get the automations to run reliably.

I use regular geofences (ie, not related to this plugin) for "arriving home". For "leaving home" my automation is essentially if AnyoneSensor changes to "away", turn off all lights and it works for me (no geofence required). I'm using an iPad with iOS 10.2.1 as my home hub.

@OultonD what do you mean by regular geofences? do you mean the one built into the Home App?

I couldn't even entertain using the Home App geofence as it doesn't support multile occupancy.

I use the geofence from the Eve app, but I haven't tested it with anyone but myself. I use the occupancy sensors to prevent issues with geofences overriding current scenes etc. These geofence issues are Apple/iOS issues though, and not really related to this plugin.

totally agree, the issues with automations (doesnt matter if using ping or geofence/webhook) is down to Apple not the plugin.

for tracking iphones bluetooth seems to work most reliably from my testing.
on the raspi I used bluetoothctl to pair the phone, then hacked a python script to send with mqtt
`
#!/usr/bin/env python

import bluetooth
import time
import paho.mqtt.client as mqtt
import json

iphone1 = '{"name":"iphone1", "state":"present"}'
iphone2 = '{"name":"iphone1", "state":"out of range"}'

client = mqtt.Client()
client.username_pw_set("user", "password")
client.connect("localhost",1883,60)

while True:
result = bluetooth.lookup_name('XX:XX:XX:XX:XX:XX', timeout=5)
if (result != None):
client.publish("home/blue", iphone1)
else:
client.publish("home/blue", iphone2)
time.sleep(10)
client.disconnect();
`

I use node-red to check that I get the same msg 3 times in a row. that seems to be working rather well so far.
After testing iBeacons and ping, I went with BT. I use homebridge-mqtt btw.
This is my function node.
`
var person = msg.payload.name;
var present = msg.payload.state;

// initialise the counter to 0 if it doesn't exist already
var iPhoneCount = context.get('iPhoneCount')||0;
var iPhoneLast = context.get('iPhoneLast')||"not";

if ((person !== null) && (person == "iphone1")) {
if (iPhoneLast != present) {
iPhoneCount = 0;
iPhoneLast = present;
} else {
iPhoneCount += 1;
}
// store the value back
context.set('iPhoneCount',iPhoneCount);
context.set('iPhoneLast',iPhoneLast);
if (iPhoneCount == 3) {
if (iPhoneLast == "present") {
// we are home
msg.payload = {"name":"iPhone", "state":"present"}; // I use this for testing, set trigger here.
} else {
// we are away
msg.payload = {"name":"iPhone", "state":"away"};
}
return msg ;
}
}
if (iPhoneCount == 30) iPhoneCount = 2; //reset to send update, (not needed).

`

Hey @MrsDelish where are you adding this code? And is is reliably triggering automations without having to open the home app?

the above code is the function node code for node-red, it checks the mqtt msg that the python script sends.
Homekit seems to ignore occupancy sensors for automation if there are no iphones presents (I have a ipad pro as home base). so we setup most accessories in homebridge-mqtt and use node-red to do the automations.

The home and away detection using BT work rather well so far, we are probably setting up one another pi to run that script as well soon, to get better coverage of our house.

improvement for home detection:
`
var person = msg.payload.name;
var current = msg.payload.state;

// initialise the counter to 0 if it doesn't exist already
var iPhoneCount = context.get('iPhoneCount')||0;
var iPhoneLast = context.get('iPhoneLast')||"not";

if ((person !== null) && (person == "iphone1")) {
if (iPhoneLast != current) {
iPhoneCount = 0;
iPhoneLast = current;
} else {
iPhoneCount += 1;
}
// store the value back
context.set('iPhoneCount',iPhoneCount);
context.set('iPhoneLast',iPhoneLast);
if (iPhoneCount == 3) || (iPhoneLast == "present") {
if (iPhoneLast == "present") {
// we are home
msg.payload = {"name":"iPhone", "state":"present"}; // I use this for testing, set trigger here.
} else {
// we are away
msg.payload = {"name":"iPhone", "state":"away"};
}
return msg ;
}
}
if (iPhoneCount == 30) iPhoneCount = 2; //reset to send update, (not needed).
`

I don't know what has changed recently, but my triggers seem to be more reliable. Perhaps something has changed on Apple's side? Anyone else experiencing more reliable triggers as well?

This issue is related to Issue #76, which @stephanroslen discovered and is trying to merge his commits.

This should now be fixed in v0.6.0.