mKeRix / room-assistant

Presence tracking and more for automation on the room-level
https://www.room-assistant.io
MIT License
1.27k stars 122 forks source link

Android companion App for BLE #472

Open Alfiegerner opened 3 years ago

Alfiegerner commented 3 years ago

Is your feature request related to a problem? Please describe. A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

I've been using room-assistant successfully for a long time, and since v2.0 using bluetooth classic.

However it would be good to have the speed and stability improvements that come from using BLE.

Describe the solution you'd like A clear and concise description of what you want to happen.

A companion app like the one in test for iPhone.

Describe alternatives you've considered A clear and concise description of any alter native solutions or features you've considered.

Bluetooth classic and carrying BLE fobs.

Additional context Add any other context or screenshots about the feature request here.

I've attempted to use both Beaconscope and Beacon Simulator, both have issues of not restarting when Bluetooth is turned off and on again, phone retdtartinf etc. I've contacted both of the devs for these apps but unfortunately neither have interest /time in improving the avaiability of the BLE transmissions, it sounds like the apps are intended more for occasional testing than permanent use.

Beacon simulator source code is open and available on git hub.

mKeRix commented 3 years ago

I agree that this would be great from a UX point of view - it would give the users something that "just runs" on Android. It's not on my immediate priority list, but I could imagine working on this sometime in the future. Should also be easy enough to get my hands on a test device.

I'd also appreciate any contributions from the community. If an Android developer stumbles upon this thread - hit me up! :)

Alfiegerner commented 3 years ago

@mKeRix FYI I've been looking at making a pull request to the homeassistant android app for this rather than making a separate app.

Some advantages:

Downsides Tighter coupling to HA

I'm not an Android or kotlin developer but hopefully should be able to get an mvp together soonish.

Conversation here: https://github.com/home-assistant/android/issues/960

Will keep you posted.

Alfiegerner commented 3 years ago

P.s. i just saw from you Reddit comment that you do java / kotlin for a living. Feel free to pick this up if you want otherwise will carry on having a crack myself sometime soon.

mKeRix commented 3 years ago

That's a great idea! The envisioned setup over there would benefit multiple projects, so I think the coupling to the HA app is not a big issue. Plus, as you mentioned, one less app to install. Thanks for the initiative.

I don't think I'll get to this anytime in the near future, so feel free to carry on. I can just post in here again once I'm at the point where I can invest some time into this - in case you haven't already solved it by then. 😄

Alfiegerner commented 3 years ago

Okay pull request is in: https://github.com/home-assistant/android/pull/1343, may be some changes required but should be ready pretty soon if anyone wants to test it.

josephmgaffney commented 3 years ago

I'm up for testing it, I don't have Android studio installed at the moment so if you get an apk built I'll gladly try it out. Otherwise I'll get it installed on a machine later on to build and test.

Great timing btw! I was literally just searching this morning around ble :)

Alfiegerner commented 3 years ago

Hi @josephmgaffney - there's an apk here, any testing appreciated.

Something that won't be functional in this apk but should be in the beta, HomeAssistant commands. These will let you turn on / off the BLE tranmitter from HA. My thinking is that we can use these to provide required resilience and control in automations like:

Trigger: Phone Bluetooth has turned off
action: 
  Turn Bluetooth back on
  Turn Beacon emitter on

or

Trigger: Phone has entered Geo zone (e.g. work)
action: 
  Turn Beacon emitter off (to save battery)
   Turn Beacon emitter on
josephmgaffney commented 3 years ago

Thanks for the apk!

Grabbed, installed and tested. I changed the major and minor to test functionality, and saw them detected by Room Assistant which I ran directly rather than as a service to monitor things.

When set to the major and minor I was using with beaconscope (and tracking in RA), had fully functioning ble location detection. No issues that's I've seen so far.

I would agree on the value of the other controls - likely what I would do is disable the transmitter when I leave home, and enable it when I come back (gps location through HA is certainly good enough for that) or when I go back on my wifi.

Will continue testing and let you know if I run into any issues, but so far it's an early success

josephmgaffney commented 3 years ago

So if I disable bluetooth, then reenable it, it doesn't begin broadcasting unless I go back into the home assistant app and manage sensors. But once I do it comes back up. I know you had further updates since the package I tested, just wanted to mention this bit

Alfiegerner commented 3 years ago

So if I disable bluetooth, then reenable it, it doesn't begin broadcasting unless I go back into the home assistant app and manage sensors. But once I do it comes back up. I know you had further updates since the package I tested, just wanted to mention this bit

Thanks very much, I'll look into that.

nashant commented 3 years ago

If you want another person to test this the let me know. Very interested in this indeed.

Alfiegerner commented 3 years ago

If you want another person to test this the let me know. Very interested in this indeed.

Hi @nashant - latest version is here, though still missing the ability to control via notifications which will require a production release.

https://github.com/home-assistant/android/suites/1986121299/artifacts/39559896

bachoo786 commented 3 years ago

Quick question: can we not use BluetoothClassic for android? what are the advantages or disadvantages of using BluetoothClassic over BLE?

bachoo786 commented 3 years ago

@Alfiegerner I have downloaded the app for testing and have configured it with room-assistant as BLE. However I cannot discover my android phone in home assistant as a sensor ? am I missing something?

However If I put my bluetooth address of my android phone under Bluetooth Classic in room-assistant, its discovered as a sensor in home assistant.

mKeRix commented 3 years ago

@bachoo786 You can use Bluetooth Classic for Android as well, the advantages of BLE can be summarized like it was put in the original issue post:

speed and stability improvements

The BLE approach is actually meant for presence detection/locating finding, while BT Classic is more of a hack to support devices where BLE won't work. I want to make a docs page on what to choose when (including pros + cons) sometime in the future.

As for your other problem: this likely has nothing to do with this issue, so I'd recommend that you open a new issue or discussion for it. Also check that you have it included in the integrations and allowlist, plus that your leader instance runs the bluetoothLowEnergy integration. If you have a mixed setup (some instances with BLE enabled, some without) check out the docs on preferred leaders.

bachoo786 commented 3 years ago

Hi @mKeRix

here is my config and it seems correct to me:

global:
  instanceName: Office
  integrations:
    - bluetoothLowEnergy
    - homeAssistant
    - bluetoothClassic
homeAssistant:
  mqttUrl: 'mqtt://XXXXXX:1883'
  mqttOptions:
    username: XXXX
    password: XXXX
bluetoothLowEnergy:
#  hciDeviceId: 1
  timeout: 60
  maxDistance: 7
  allowlist:
  - 'E8FEFD18-065B-4C55-A462-6EF762E8C51A'
  - '7287dac4-a3d9-4def-8fe6-5924af984220-100-1'
  tagOverrides:
    7287dac4-a3d9-4def-8fe6-5924af984220-100-1:
      name: s20test
bluetoothClassic:
  minRssi: -20
  hciDeviceId: 0
  addresses:
    - 'FC:DE:90:3E:60:96'
Alfiegerner commented 3 years ago

@bachoo786 from memory I think you need whitelist rather than allowlist, also the uuids need to have the hyphens stripped out.

bachoo786 commented 3 years ago

@Alfiegerner thanks that worked for me :) how do I now set the distances for my android from one room (with raspberry pi) to another room (different raspberry pi)?

Alfiegerner commented 3 years ago

@Alfiegerner thanks that worked for me :) how do I now set the distances for my android from one room (with raspberry pi) to another room (different raspberry pi)?

Not sure what you mean by 'set the distances'. You can use maxDistance to stop a specific RA instance from reporting after a certain distance is reached, this can help define boundaries between rooms.

bachoo786 commented 3 years ago

@Alfiegerner thanks that worked for me :) how do I now set the distances for my android from one room (with raspberry pi) to another room (different raspberry pi)?

Not sure what you mean by 'set the distances'. You can use maxDistance to stop a specific RA instance from reporting after a certain distance is reached, this can help define boundaries between rooms.

right ok thanks for that.

are you experiencing any issues with the home assistant test app? bluetooth classics responds quickly but ble transmitter from my android is very flakey and says not_home every time even if I am right next to my raspberry pi running room assistant.

mKeRix commented 3 years ago

Especially if you run BT Classic and BLE together I would recommend to increase the timeout, as the adapter will not be able to do both at the same time and will pause BLE scanning for every Classic inquiry. This will lead to these not_home scenarios. Try to set it around the time that your not_home blips take.

bachoo786 commented 3 years ago

@mKeRix thank you. I have removed BT classic for now to test it out. How do I increase the timeout?

mKeRix commented 3 years ago

timeout is a setting of the Bluetooth Low Energy integration. You can simply configure it like so:

bluetoothLowEnergy:
  timeout: 60
bachoo786 commented 3 years ago

@mKeRix ah I had already configured it to 1 minute i.e. 60 seconds. I have now increased it to 5 minutes i.e. 300 seconds. Let me try and see if it works for me. Thanks :)

bachoo786 commented 3 years ago

@mKeRix I take it as the timeout is in seconds ?

Alfiegerner commented 3 years ago

Hi @bachoo786 - just to let you know I have radio silence on the pull request on the HA companion app for some reason - I would look at holding off further testing on this until I understand what the next steps are. I'll keep you posted.

Alfiegerner commented 3 years ago

Hi @bachoo786 - good news the pull request has been merged, a pre-release apk is here.

Please be warned that this is a pre-release for the entirey of the HA companion app, which has not been fully tested.

In general for all testing this I would recommend that if you run into problems:

  1. Don't combine with BluetoothClassic
  2. Set timeout to 60
  3. Try using Beaconscope to see if issue is reproducible with that.

This should help either eliminate or identify the problem.

Good news is that it's working really well for me at least. I'm using medium strength currently and will try lower tomorrow. No drops all day.

Now that we're in pre-release the notification commands work to turn on / off the BLE transmitter from home-assistant automations. Notes below copied from HA Android docs that will be published.

Users can turn the iBeacon transmitter on or off using message: command_ble_transmitter with the title being either turn_off or turn_on. If title is blank, not set or not one of the above expected values then the notification will post as normal.

Example:

automation:
  - alias: Notify Mobile app
    trigger: # examples, 1. away from home and want to save battery, 2 bluetooth has been turned off (and should be turned back on again and transmitter restarted) etc
      ...
    action:
      service: notify.mobile_app_<your_device_id_here>
      data:
        message: "command_ble_transmitter"
        title: "turn_off"
Alfiegerner commented 3 years ago

This is now released in the public version of the app on play store.

bachoo786 commented 3 years ago

Thanks @Alfiegerner is just an update of the current HA app correct?

Alfiegerner commented 3 years ago

Thanks @Alfiegerner is just an update of the current HA app correct?

Yep, should just be a standard update. Look for version number 2013.03.1-full.

bachoo786 commented 3 years ago

OK thanks

bachoo786 commented 3 years ago

Hi @Alfiegerner so I am using the updated HA android app and have configured my android with room assistant. I have my NUC in my office which is running HA and room assistant via a docker container but under device tracker and the sensor for ble transmitter, it is not detecting that my android being in the office I am 30cms away from the NUC.

On HA it shows the following:

device_tracker.sm_g986b as home and sensor.sm_g986b_ble_transmitter as transmitting

I am using low strength for my ble transmission.

Can you help please?

Alfiegerner commented 3 years ago

Hi @bachoo786 ,

I would try:

If you still can't see anything, try putting beaconscope on another android device and see if that can detect your HA Android app sending.

bachoo786 commented 3 years ago

Ok will try thanks.

However in HA am I going to see it as a sensor or? Where and what am I meant to see in HA ?

Alfiegerner commented 3 years ago

Hi @bachoo786 ,

It will appear as a normal room-assistant device, named by default after the uuid if you don't have a tag override. I suggest you look at the really good documentation mkerix has put together.

bachoo786 commented 3 years ago

Ok thanks will look into it.

bachoo786 commented 3 years ago

Hi @Alfiegerner

So I believe the app is working fine it detects when I am in the office. However my other room assistant is in my bedroom but it doesn't detect when I am in my room next to my pi running room-assistant.

I have configured my local.yml file and have added my android running HA app with the BLE transmitter in both of my local.yml files I.e. for 2 instances running room-assistant.

The sensor in HA is always stuck in office even when I am right close to my pi which is in the master bedroom.

Can you help please?

bachoo786 commented 3 years ago

@bachoo786 from memory I think you need whitelist rather than allowlist, also the uuids need to have the hyphens stripped out.

@Alfiegerner can you please share your config? thanks

mKeRix commented 3 years ago

@bachoo786 Since this is a bit unrelated it would be best to open a new discussion on it, but I'll comment on it here for the time being. In order to figure out the issue it would be good if you could confirm that these two instances are talking to each other - and if they do, what is driving the decision. Could you check API of one of your instances for what the entities endpoint reports? There you should be able to see what signal strengths where measured when by whom. Also, since this is a common issue I've seen in the past, you could try re-running the setcap commands from the docs on your bedroom node and then restarting room-assistant.

Apart from that: I suppose there isn't much left to do here for me now, but I do want to include a note about this in the room-assistant docs. For tracking purposes I'll leave the issue open and close it once the docs have been updated. :)

Alfiegerner commented 3 years ago

@bachoo786 - apologies I missed your last message. Config from one of my instances:

bluetoothLowEnergy:
    processIBeacon: true
    timeout: 60
    whitelist:
    - .*-100-.*
    whitelistRegex: true
cluster:
    autodiscovery: false
    peerAddresses:
    - 192.168.86.46:6425
    - 192.168.86.36:6425
    - 192.168.86.91:6425
    - 192.168.86.90:6425
    - 192.168.86.229:6425
    - 192.168.86.83:6425
    - 192.168.86.232:6425
    - 192.168.86.106:6425
    - 192.168.86.44:6425
    - 192.168.86.231:6425
    quorum: 3
global:
    instanceName: home
    integrations:
    - homeAssistant
    - bluetoothLowEnergy
homeAssistant:
    mqttOptions:
        password: home_pa555
        username: homeassistant
    mqttUrl: mqtt://192.168.86.106:1883

In addition to @mKeRix notes above, I'd suggesting going through all installation steps again - as I have several room assistant instances I use the ansible install approach.

bachoo786 commented 2 years ago

@bachoo786 - apologies I missed your last message. Config from one of my instances:

bluetoothLowEnergy:
    processIBeacon: true
    timeout: 60
    whitelist:
    - .*-100-.*
    whitelistRegex: true
cluster:
    autodiscovery: false
    peerAddresses:
    - 192.168.86.46:6425
    - 192.168.86.36:6425
    - 192.168.86.91:6425
    - 192.168.86.90:6425
    - 192.168.86.229:6425
    - 192.168.86.83:6425
    - 192.168.86.232:6425
    - 192.168.86.106:6425
    - 192.168.86.44:6425
    - 192.168.86.231:6425
    quorum: 3
global:
    instanceName: home
    integrations:
    - homeAssistant
    - bluetoothLowEnergy
homeAssistant:
    mqttOptions:
        password: home_pa555
        username: homeassistant
    mqttUrl: mqtt://192.168.86.106:1883

In addition to @mKeRix notes above, I'd suggesting going through all installation steps again - as I have several room assistant instances I use the ansible install approach.

Hey @Alfiegerner sorry its been a year I saw this. I decided to remove RA and now I am back to test it again.

I have the same issues as before and I tried to read what you had told me regarding my UUIDs etc. I followed everything but I cannot see my Samsung Galaxy phone in HA as a BLE sensor.

Could you please help?

Thanks.