ESPHome BLE Gateway and other BLE components.
Please this repo if you find it useful.
To use this repository you should confugure it inside your yaml-configuration:
external_components:
- source: github://myhomeiot/esphome-components
or download component into custom_components
folder (you can use another name) and add following lines to your yaml-configuration:
external_components:
- source: custom_components
You can take a look at samples of usage of those components in examples folder.
BLE Client
BLE Client allow to read characteristics from devices.
Difference from build-in ESPHome BLE Client:
- Always disconnects from device after reading characteristic, this will allow to save device battery. You can specify
update_interval
, defaults to 60min. - Uses lambda for parsing and extracting data into specific sensors make this component very flexible and useful for prototyping.
- There is no limit to the number of BLE Clients used (build-in BLE Client has limit of 3 instances). This component uses BLE Host component which you should count as one instance of build-in BLE Client. All BLE clients are processed sequentially inside the host component at time when they was detected and update interval reached.
BLE Host
Used by BLE Client component.
BLE Gateway
BLE Gateway component will allow you to forward BLE Advertising data packets for external processing to Home Assistant or other systems.
If the heart of your Home Automation system is Home Assistant or another similar system and you use ESPHome devices to extend BLE coverage and process data from BLE sensors, you can dramatically decrease system complexity by remove all BLE data processing from ESPHome devices and forward raw BLE Advertising data to external components like Passive BLE Monitor.
Requirements:
- Passive BLE Monitor integration version 6.2 or later. Thanks @Ernst79
- ESPHome version 2022.1 or later
If you use ESPHome 2021.12 version or earlyer you need to make following changes in ESPHome esp32_ble_tracker
component.
In order to apply this PR you can use following ESPHome configuration (requires ESPHome 2021.11 or later):
external_components:
- source: github://myhomeiot/esphome-components
- source: github://pr#2854
components: [esp32_ble_tracker]
ESPHome configuration example
Note: This example use event, you can use direct ble_monitor.parse_data
service call
ble_gateway:
devices:
- mac_address: 01:23:45:67:89:AB
- mac_address: !secret lywsd03mmc_mac
on_ble_advertise:
then:
homeassistant.event:
event: esphome.on_ble_advertise
data:
packet: !lambda return packet;
Home Assistant Passive BLE Monitor configuration example
Note: Remove automation if you use direct ble_monitor.parse_data
service call
ble_monitor:
discovery: false
restore_state: true
decimals: 1
period: 300
devices:
- name: Living Room Thermo
mac: 01:23:45:67:89:AB
- name: Bedroom Thermo
mac: !secret lywsd03mmc_mac
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 }}"
Advanced configuration where ESPHome devices gets MAC addresses from Passive BLE Monitor configuration
Note: Be sure that you turn on the input_boolean.settings_ble_gateway
if you want to receive BLE packets from BLE Gateway’s.
Important note: New device address will be populated to ESPHome devices only after Passive BLE Monitor receives first BLE packet and creates entities for it. If in your configuration you don’t have BLE stick and you have only ESPHome devices you can use one of the following methods:
- Add this device MAC address manually into the
input_text.settings_ble_gateway_add_device
. After Passive BLE Monitor creates entities for new device, you can remove address. - Enable discovery for Passive BLE Monitor and ESPHome BLE Gateway
input_boolean.settings_ble_gateway_discovery
. After required devices will be discovered turn off discovery options (for Passive BLE Monitor and ESPHome BLE Gateway) and clean up unneeded devices that got detected if any.
# ESPHome
ble_gateway:
id: blegateway
on_ble_advertise:
then:
homeassistant.event:
event: esphome.on_ble_advertise
data:
packet: !lambda return packet;
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);
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
# Home Assistant
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: "{{ states | selectattr('entity_id', 'search', '^(device_tracker|sensor).ble_') | selectattr('attributes.mac address', 'defined') | map(attribute='attributes.mac address') | unique | sort | join('') | replace(':', '') ~ (states('input_text.settings_ble_gateway_add_device') | replace(':', '') | trim) if is_state('binary_sensor.ble_gateway', 'on') }}"
# Important note: In Passive BLE Monitor version 7.8.2 and later 'attributes.mac address' was changed to 'attributes.mac_address', please update your config
# devices: "{{ states | selectattr('entity_id', 'search', '^(device_tracker|sensor).ble_') | selectattr('attributes.mac_address', 'defined') | map(attribute='attributes.mac_address') | unique | sort | join('') | replace(':', '') ~ (states('input_text.settings_ble_gateway_add_device') | replace(':', '') | trim) if is_state('binary_sensor.ble_gateway', 'on') }}"
# Note: In Home Assistant 2022.x, Passive BLE Monitor version 8.x and later you can use device attribute identifiers
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') }}
More configuration examples you can find in examples folder.