DIY HomeAssistant Passive Ble Gateway

This is a beautiful little device specially used to collect BLE Bluetooth device information. Using it, you can collect various BLE devices that can be supported by the Passive BLE Monitor Integration plug-in, such as Xiaomi Thermo-Hygrometer 2, Xiaomi Weight Scale, etc. For the device list, please refer to page.


At present, more and more Xiaomi devices are beginning to adopt the Bluetooth protocol, including the Bluetooth MESH protocol. For full localization support, this poses some challenges, as it is difficult to directly access them from Bluetooth regardless of distance. Because obviously, a direct bluetooth adapter would be the ideal solution, but due to the limitations of the Homeassistant head unit, we had to give up doing so.
Homeassistant’s support for Bluetooth devices is relatively limited, one is relatively close, and the other is that the gateway made by ESP32 has relatively small memory and connected devices are relatively small. It is also because the ESP32 with a network port is a bit inappropriate as a gateway, and it is still a distance limit. It seems very difficult to find a more perfect gateway, which is far less convenient and powerful than Zigbee gateway.

ble_monitor is a very good homeassitant plug-in, through which you can passively accept many ble devices, such access is not limited by three devices (because it Not directly connecting them, just reading the broadcast packet data of supported devices.), and we found that using this plugin with ESP32 works very well.

This is the esphome yaml source code written to esp32:

esphome:
  name: screek-ble-monitor-1a
  friendly_name: BLE Monitor Gateway 1A
  name_add_mac_suffix: True
  project: 
    name: Screek.BLE Monitor Gateway
    version: 1A

external_components:
  - source: github://myhomeiot/esphome-components

logger:

esp32:
  variant: ESP32
  board: esp32dev
  framework:
    type: arduino 
    version: 2.0.7
    platform_version: 6.0.1

api:
  reboot_timeout: 1h

# web_server:
#  port: 80

ota:
  password: "YOUR_OTA_PASSWORD"

wifi:
  ap:
    ssid: "SCREEK-BLE-MONITOR"

captive_portal:

improv_serial:

esp32_ble_tracker:

sensor:
  - platform: uptime
    name: Uptime
    id: sys_uptime
    update_interval: 10s
  - platform: wifi_signal 
    name: RSSI
    id: wifi_rssi
    update_interval: 10s
    entity_category: "diagnostic"
  - platform: template
    id: esp_memory
    icon: mdi:memory
    name: ESP Free Memory
    lambda: return heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024;
    unit_of_measurement: 'kB'
    state_class: measurement
    entity_category: "diagnostic"
    
ble_gateway:
  id: blegateway
  on_ble_advertise:
    then:
      homeassistant.event:
        event: esphome.on_ble_advertise
        data:
          packet: !lambda return packet;
#          gateway_id: ${device_id}

binary_sensor:
  - platform: homeassistant
    id: ble_gateway_discovery
    entity_id: binary_sensor.ble_gateway
    attribute: discovery
    on_state:
      then:
        lambda: id(blegateway).set_discovery(x);
  - platform: status
    name: Online
    id: ink_ha_connected

text_sensor:
  - platform: homeassistant
    id: ble_gateway_devices
    entity_id: binary_sensor.ble_gateway
    attribute: devices
    on_value:
      then:
        lambda: id(blegateway).set_devices(x);

switch:
  - platform: template
    id: switch_ble_gateway_discovery
    name: BLE Gateway Discovery
    icon: mdi:bluetooth-connect
    lambda: return id(blegateway).get_discovery();
    turn_on_action: [lambda: id(blegateway).set_discovery(true);]
    turn_off_action: [lambda: id(blegateway).set_discovery(false);]
    disabled_by_default: true
    entity_category: config

button:
  - platform: restart
    # disabled_by_default: True
    icon: mdi:power-cycle
    name: "ESP Reboot"
  - platform: factory_reset
    disabled_by_default: True
    name: Factory Reset
    id: factory_reset_all

Then next, you need to add related settings in HA, you need a service for parsing data:

automation:
  - alias: ESPHome BLE Advertise
    mode: queued
    trigger:
      - platform: event
        event_type: esphome.on_ble_advertise
    action:
      - service: ble_monitor.parse_data
        data:
          packet: "{{ trigger.event.data.packet }}"

Then add some functions to communicate between ble monitor and BLE Gateway:

input_boolean:
  settings_ble_gateway:
    name: BLE Gateway
    icon: mdi:bluetooth
  settings_ble_gateway_discovery:
    name: BLE Gateway Discovery
    icon: mdi:bluetooth-connect

input_text:
  settings_ble_gateway_add_device:
    name: BLE Gateway Add Device
    icon: mdi:bluetooth-connect
    initial: ''

template:
  - binary_sensor:
      - name: BLE Gateway
        icon: mdi:bluetooth
        state: "{{ is_state('input_boolean.settings_ble_gateway', 'on') }}"
        attributes:
          discovery: "{{ is_state('input_boolean.settings_ble_gateway_discovery', 'on') }}"
          devices: >-
            {% set devices = namespace(items = []) %}
            {% for s in states | selectattr('entity_id', 'search', '^(device_tracker|sensor).ble_') | map(attribute='entity_id') %}
              {% set devices.items = devices.items + [device_id(s)] %}
            {% endfor %}
            {% set ns = namespace(items = []) %}
            {% for s in devices.items | unique %}
              {% set ns.items = ns.items + [(device_attr(s, 'identifiers') | first)[1]] %}
            {% endfor %}
            {{ ns.items | unique | sort | join('') | replace(':', '') ~ (states('input_text.settings_ble_gateway_add_device') | replace(':', '') | trim) if is_state('binary_sensor.ble_gateway', 'on') }}

From now on, you can control this BLE gateway in HA, and cooperate with the Ble Monitor plug-in to automatically discover surrounding BLE devices.

The above is a bluetooth passive receiver made by us, it can receive the surrounding bluetooth signal quite stably. Let HA get direct data from bluetooth sensor.

![图像 3|375x500](upload://kOeufxfTT47dPtUkiC5kiXCoN9r.jpeg


We also made some additional kits to share with you, so that everyone can feel the convenience of Bluetooth.

2 Likes

I have some of these devices, do you have the .stl file for the nice box?

1 Like

Trying to create the automation in home assistant, I am receiving the below. I suspect the answer is obvious…but not to me!
image

1 Like

Sorry we have less time to come to the HA forums, we can provide the STL for private use, please contact us by email and we will send you a copy.

We haven’t used this way for a while (instead we use bt proxy directly), and if memory serves, we may have to write directly to the ha to automation file.