Shelly Plus Bluetooth Proxy set up for presence detection - problems!

Then: how to configure this with Home Assistant?
I try the config there, but I get the following:

bluetooth:

device_tracker:
  - platform: bluetooth_le_tracker
    track_new_devices: true
    track_battery: true
    track_battery_interval: 3600

The config itself is correct, I removed the battery tracking and do not track new devices… With my beacon that I puchased/linked above, it works realiable.

device_tracker:
  - platform: bluetooth_le_tracker
    track_new_devices: false
    track_battery: false

What kind of gateway do you use?
What is the configuration?
I use ESP32/ M5Stack

I use Shelly Plus 1PM and 2PM as a BT Proxy.

I also have a M5Stack Atom Lite (which I used for the debugging). Just plugged in the M5Stack and my beacon was detected home immediately.
You have, however also the option to define an own device tracker for the same beacon via ESP32 config, check out that config. In this case, an additional device tracker for ESP32 BT Proxy will be created. (In my case, I have my normal BLE device tracker device, detected by Shelly Plus and M5 Statk Atom Lite, as well an additional device tracker for the same beacon, however that one is only detected by the ESP32 BT Proxy)

Can I see the configuration of the “M5Stack Atom Lite”?
I have big problems with that gateway

Sure… When I did the setup of the Atom Lite, everything above wifi was already there.

Then, I added stuff underneath it.
you should start with logger level VERBOSE, and without the binary_sensor. When you program the Atom Lite and let it run, you will see your BLE Tracker messages (not sure about the Shelly Blu Button though, seems not to be the best product right now).

Once you know that MAC address of your BLE tracker, you can remove level: VERBOSE and add the binary_sensory, then program your Atom Lite again.

Good luck

Here is the Atom Lite config from me:

substitutions:
  name: atom-bluetooth-proxy
  friendly_name: BT Proxy M5 Stack
packages:
  esphome.bluetooth-proxy: github://esphome/bluetooth-proxies/m5stack-atom-lite.yaml@main
esphome:
  name: ${name}
  name_add_mac_suffix: false
  friendly_name: ${friendly_name}
api:
  encryption:
    key: XXX

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

logger:
  level: VERBOSE

ota:

esp32_ble_tracker:
  scan_parameters:
    active: true
    continuous: true

bluetooth_proxy:
  active: false
  
binary_sensor:
  - platform: ble_presence
    mac_address: [your BLE Tracker MAC]
    name: [your BLE tracker name]
1 Like

A note to future researchers of this, Shelly plus battery powered devices, such as the Plus H&T (PlusHT), appear to be fairly useless for presence as there is no way to query nor set a polling interval, it only connects every 2 hours or when there is a temp update…

What happens if the Shelly Plus H&T is powered via USB ?

The 2 hour interval should only be valid in battery-power mode.

Hi @PatrickT1, @wilfreddijksman, @bule
I have the same problem with Shelly BLE. I installed room assinstant in the raspberry (where I have the home assistant) and he can detect my mobile phone, but by UUID, not MAC or name, then I think Shelly have a lot of problems reading the advertisment data. In fact trying the functions BLE.GAP.HasService() and BLE.GAP.ParseServiceData() always return true.

Any progress by your side?

Thanks

Did anyone see the new Private BLE Device - Home Assistant integration? It could be a leap forward in tracking iPhones using bluetooth/bluetooth proxies.

1 Like

Hi,

I tried to make a simple “bluetooth presence detector” of Shelly pro 2PM, based on reported RSSI value. Script itself works fine but the built in bluetooth scanner is not detecting any of my phones.
If I try it with my laptop, that has bluetooth enabled, it works fine. Shall I enable anything else from my phones? Bluetooth is enabled and discoverable…

Just to update my original post, I gave up with the Shelly bluetooth.

I bought a handful of M5Atoms and installed ESPresence. I’m still playing with it, but the Shelly just won’t do it unless they make firmware changes.

@PatrickT1 Do you have any thoughts on what firmware changes Shelly should make?

@50bbx - I’m not sure, but if it were able to act as a node for ESPresence then that would be perfect - but I don’t know how to specify what data is needed to do that.

There’s also the issue that the Shelly can’t proxy active connections. For example, I bought a Switchbot blind tilt, and the Shelly can’t be a proxy for that. I had to use a M5Atom as a bluetooth proxy for that.

Honestly I don’t see what the use cases are for the bluetooth in the Shelly with the limited information it can offer as it stands. Maybe it’s useful for first setup with the Shelly app, but I don’t use their app - only HA.

So i got something working with Shelly’s BLE and the mqtt_room integration.

configuration.yaml

- platform: mqtt_room
  device_id: "8c64a22cde82"
  name: "Nordbuds"
  state_topic: "espresense/rooms"
  timeout: 10
  away_timeout: 120

Shelly script

// mqtt_room BLE script
BLE.Scanner.Subscribe(function (ev, res) {
    if (ev === BLE.Scanner.SCAN_RESULT) {
      let room = "office";
      let maxDistance = 3;
      let minRSSI = ( maxDistance * -15 ) - 40;
      let topic = "espresense/rooms/"+room;
      
      if (res.rssi > minRSSI){
        let id = res.addr.split(":").join("");
        let distance = ( res.rssi + 40 )/ -15;
        distance = distance < 0.1 ? 0.1 : distance;
        console.log(topic, id, distance);
        if (MQTT.publish(topic, '{ "id": "'+id+'", "distance":'+distance+' }')){
          console.log('published')
        }
      }
    }
});

BLE.Scanner.Start({
    duration_ms: -1,
    active: false,
    interval_ms: 320,
    window_ms: 30,
});

console.log('Script started')

if (MQTT.isConnected()){
  console.log('MQTT is connected')
}

The distance calculation and filtering is crudely based on RSSI.

The Shelly should be connected to the homeassistant MQTT server
and bluetooth should be enabled on the Shelly

This still cannot track my android phone however. It seems shelly only receives BLE advertisements

3 Likes

building on this, I installed a BLE peripheral simulator on my android phone.
It pretends to be a device having a battery and announces via BLE.
After activating it, I very much was able to see my phone, confirming your theory.

For easier reading it is possible to output the device name:

    console.log(topic, id, distance, BLE.GAP.parseName(res.advData));

I am not sure where to go from here. Having to have a scanner app open seems a bit inconvenient.

Edit:
https://www.youtube.com/watch?time_continue=241&v=p7C2QvmsM8M&embeds_referring_euri=https%3A%2F%2Fduckduckgo.com%2F&feature=emb_logo says to use a different tracker than the phone and I might try with a fitbit.

1 Like

Just to resurrect this thread, I have recently switched all my smart plugs to the Shelly plugs, which enabled me to try this out. The good news is that using the new Private BLE you can use it to track iPhones or Apple watches, however I am struggling with how it is working and how it updates, as it is not very useful atm. I created an automation to read the attributes of the source of the Private BLE device when it changes, and then using mqtt_room map that to the correct room in the house. So the problem is it updates very slowly, even though it looks like the script on the shelly devices is running every 300ms. I wanted to understand a bit what is happening from an architecture pov, the shelly’s are all sending updates every 300ms (with 25 devices this is very chatty!) using rpc over http to the shelly integration (I assume), this is then being analysed somewhere in HA (where?) and passed to the Private BLE device as a distance measurement with an update of the mac address of the source. It takes sometimes 20 minutes to register a room change, the data is correct, I would just like to make this update happen a lot quicker.

1 Like

How do you get the IRK from phone, I have ancient mac but only had one id and appears to be my only my also ancient watch?

Can you share? I dont see anything in the attributes or anywhere suggesting which shelly provided what info. Just ambiguous signal reading for only the watch is what i can find IRK from.

Hi,

You get the IRK for the phone or watch by following the instructions in the Private BLE here . It’s a bit fiddly, especially when getting the IRK out of the XML stream, but it works. Once you have the Private BLE device showing up in Settings->Devices&Services->Private BLE, go into the device, click on the device in the Diagnostic panel, in the panel that opens up, you can pull down the Attributes tab, and the second source entry at the bottom will be the MAC address of the Shelly device that detected it.

If you get that working here is my automation, that fires every time there is a state change on the Private BLE device, it looks to see if the source has changed, if so, match the mac address to the real room, and post that to the mqtt_device. I have been trying to work out in the code what triggers the update of the source attribute of the Private BLE, when the Shelly integration is getting multiple updates per second, and where the logic is deciding which is the nearest Shelly device.

###############################################################################
## Apple watch location
###############################################################################

- id: apple_watch_location
  alias: apple_watch_location
  trigger:
    - platform: state
      entity_id:
        - device_tracker.alastairs_watch
  condition:
    - condition: template
      value_template: >
        {{ trigger.from_state and
           trigger.to_state.attributes.source !=
           trigger.from_state.attributes.source }}
  action:
    - service: input_text.set_value
      data_template:
        entity_id: input_text.alastairs_location
        value: > 
          {% set beacons = {
            'xx:xx:xx:xx:xx:xx':'Kitchen',
            'xx:xx:xx:xx:xx:xx':'Lounge',
            'xx:xx:xx:xx:xx:xx':'Bedroom' } %}
          {{ beacons.get(state_attr('device_tracker.alastairs_watch','source')) }}
    - service: mqtt.publish
      data_template:
        topic: homeassistant/device_tracker/alastairs_location
        payload: >
          {% set beacons = {
            'xx:xx:xx:xx:xx:xx':'Kitchen',
            'xx:xx:xx:xx:xx:xx':'Lounge',
            'xx:xx:xx:xx:xx:xx':'Bedroom' } %}
          {{ beacons.get(state_attr('device_tracker.alastairs_watch','source')) }}
2 Likes