DouweM / homebridge-unifi-occupancy

Homebridge plugin that adds HomeKit occupancy sensors for selected devices (and people) on your UniFi network to the iOS Home app: quickly see who's where and automate accordingly.
Apache License 2.0
41 stars 1 forks source link

Include/exclude devices based on (host)name match #9

Closed DouweM closed 1 year ago

DouweM commented 1 year ago

As requested by @easybizz in https://github.com/DouweM/homebridge-unifi-occupancy/issues/6#issuecomment-1373180833:

I think it wiuld be awesome if this plugin has a specific phrase/word/term/code to look for in device names. So I can add the phrase to every client alias I would like in the Unifi app/controller. This could even be another device than a phone.

so lets say I start every client I want as a sensor with “9xyz9” the plugin automagically creates it in homebridge when it detects them.

And https://github.com/DouweM/homebridge-unifi-occupancy/issues/6#issuecomment-1397256275:

I would love a user configured phrase though. To also add iPad or Apple Watch etc. Without having to name them “phone” in unifi.

DouweM commented 1 year ago

@EasyBizz Can you elaborate on what you're expecting the behavior to be around non-smartphone devices like iPads and Apple Watches, of which a person may have multiple?

Right now, this plugin is optimized for tracking people around the house, which is why it only detects smartphones (that people typically carry around) and filters out suffixes like 's iPhone to show just <Name> @ <AP>. But if what you actually want to do track their individual devices, we should show <Name>'s iPhone @ <AP> and <Name>'s Apple Watch @ <AP> separately. Is that your plan?

Basically, I built this to be an in-home version of the "Find My" "People" tab, but maybe you intend to use it as the in-home version of the "Find My" "Devices" tab instead. I can definitely add full support for that use case, but that would involve a few more changes than just making the phrase configurable.

I can try to make it smart enough to detect all portable WiFi devices, so that'd include things like MacBooks and Nintendo Switches as well, but not a HomePod or printer. Basically anything you could lose somewhere in the house and would want to know where you left it. But if I include all of them, users will likely still want to hide some of them, like a Nintendo Switch that's really just always sitting by the TV.

DouweM commented 1 year ago

@EasyBizz I've implemented two new settings in v1.0.5 that I think you'll like:

Captura de pantalla 2023-02-01 a la(s) 22 38 02

Let me know if this works for your use case!

EasyBizz commented 1 year ago

Hi @DouweM,

Sorry for the late reply, I do not get notified when you @ me. I Have to change my email settings. (Done)

First off, again, this plugin is awesome and you're great for putting the time in.

Now how I envisioned it:

I get that people in your household show up automatically, thats perfect. But anyone with an smartphone visiting my house will be added as a sensor if the plugin detects them. So the phrase is more for exclusian than inclusion. I chose what devices get added as a sensor.

I could also include guest sensors if I want (example): When me and my wife (both) leave the house, the house turns off (lights, heating, etc.) But sometimes we have a babysitter that stays in the house and we want everything to remain on. I now have to disable the "leave" automation, but I could add an extra step to check if the babysitter ('s device) is connected.

Why I would want the different devices per user is (example): Say I wake up at night. I need to go to the bathroom. I will leave my phone on the nightstand, But I'm wearing my apple watch. I pass a couple motion sensors on my way to turn on the lights, but can have them turn on in different brightness depending on what device is connected to what AP.

Maybe a bad example, because I'm probably faster than the device roams to another AP and I dont wear my watch while sleeping, but I hope this explains the possibilities.

Also if the 'phrase' option is added, I could add any device, even a car, that connects to Wifi and have automations based on anything being within range.

BTW since the last update the plugin is spewing errors on every refresh. "Failed to get connected devices: TypeError: Cannot read properties of undefined (reading çomputed_dev_id')"

I'm at work right now so I only see the errors on my phone, I will post a new issue for the errors when I get home and they still occur after I restart HB.

DouweM commented 1 year ago

@EasyBizz

First off, again, this plugin is awesome and you're great for putting the time in.

Thanks! I'm actually really enjoying building it and happy it's benefiting other people too.

But anyone with an smartphone visiting my house will be added as a sensor if the plugin detects them. So the phrase is more for exclusian than inclusion. I chose what devices get added as a sensor.

Makes sense, I've updated the title of this issue to cover that. My current plan is to add 2 settings: (arrays of) inclusion and exclusion regex patterns, to include devices that aren't automatically detected by one of the device types, or to specifically exclude devices that were matched by one of the preceding filters.

I could also include guest sensors if I want (example): When me and my wife (both) leave the house, the house turns off (lights, heating, etc.) But sometimes we have a babysitter that stays in the house and we want everything to remain on. I now have to disable the "leave" automation, but I could add an extra step to check if the babysitter ('s device) is connected.

If you have the "Devices: smartphones" option enabled (default before v1.0.5), guest devices should already show up as Guest: <device name>. If you then rename this device to <owner>'s <device> in the UniFi Network UI, it should start showing up as <owner> (creating a new accessory and deleting the old, as the accessory UUID contains the display name and thus changed when the device name did). Is this not happening for you?

It would be useful to (optionally) create a sensor for whether a device (or person) is connected to any AP (i.e. whether they're at home at all), that would make automations like the one you mention easier:

Why I would want the different devices per user is (example): Say I wake up at night. I need to go to the bathroom. I will leave my phone on the nightstand, But I'm wearing my apple watch. I pass a couple motion sensors on my way to turn on the lights, but can have them turn on in different brightness depending on what device is connected to what AP.

Maybe a bad example, because I'm probably faster than the device roams to another AP and I dont wear my watch while sleeping, but I hope this explains the possibilities.

With the feature I screenshotted in https://github.com/DouweM/homebridge-unifi-occupancy/issues/9#issuecomment-1413141594 (released in v1.0.5), you can enable both "Smartphones" and "Smart Watches", and a sensor will be created for both. Depending on the "Show as owner" setting, either or neither will show as the device owner (matching (host)name patterns like <owner>'s <device>). You should be able to use both in automations.

Would that work? Or would you want both devices to map to the same <owner> sensor, so that it could be on in two rooms at the same time?

And yeah the reason I haven't used this for automations yet is that roaming delay (quicker to just reach for a light switch...), and I haven't been super impressed with how quickly it shows the new status in Home even after the Homebridge logs show that the accessory was updated. I don't know yet if that's a plugin, Homebridge, HomeKit, Home app, or WiFi issue.

Also if the 'phrase' option is added, I could add any device, even a car, that connects to Wifi and have automations based on anything being within range.

Definitely, the "inclusion pattern" setting makes a ton of sense.

I also think it makes sense to allow all devices on the network to get sensors, not just ones in categories I consider "portable". A device that (dis)connects frequently like a car would be useful to build automations around for at least the home-wide sensor (there is a "Smart Car" category in the fingerprint, but it only detects Teslas so far), but even a stationary device like a NAS is automatable: alert me if it disconnects.

BTW since the last update the plugin is spewing errors on every refresh. "Failed to get connected devices: TypeError: Cannot read properties of undefined (reading 'computed_dev_id')"

Hmm, weird. Let me know if it fixed itself after a restart, or create an issue if not. I looked into it briefly, and it suggests this.raw.fingerprint is undefined here:

https://github.com/DouweM/homebridge-unifi-occupancy/blob/be3077326d32dd99a95d580dffac97a07107ca0d/src/device.ts#L77

That's surprising, given that this.raw comes from the API:

https://github.com/DouweM/homebridge-unifi-occupancy/blob/be3077326d32dd99a95d580dffac97a07107ca0d/src/platform.ts#L173

Or from here, where raw.fingerprint is set just a few lines up:

https://github.com/DouweM/homebridge-unifi-occupancy/blob/be3077326d32dd99a95d580dffac97a07107ca0d/src/platformAccessory.ts#L66

Does your UniFi Network UI show device fingerprints?

If you sign into you UniFi Network UI and then visit https://<ip>/proxy/network/v2/api/site/default/clients/active, do you see a "fingerprint" property?

DouweM commented 1 year ago

Done in v1.2.0!