Passive BLE Monitor integration

The first versions of HA with the new Bluetooth integration were using active scanning, which is using slightly more battery. The devs were working on some implementation that would start with passive scanning, and switch to active scanning only if needed. Not sure if that already made it into HA. But I wouldn’t bother too much about battery life, it is only a few percent more.

There isn’t averaging anymore in the new HA Bluetooth integrations, that is correct. So you can see some small differences between BLE monitor and the Xiaomi or BTHome integration.

Beside the new Bluetooth integration I have the BLE running. In the BLE configuration I have selected: Active Scan. When I disable this option, the sensors in the new Bluetooth integration become unavailable.
For me it is still unclear how the new Bluetooth integration works with Active Scan.

1 Like

Active scan or passive scan is a setting of your Bluetooth radio (dongle or internal radio). This is a system (hardware) setting. To explain the difference, passive scanning is “just listening to BLE advertisements”, while active scanning is sort of shouting around “everyone who hears this, please send all the data you have”.

A Bluetooth radio can only use one type of scanning at the same time. So, if you set BLE monitor wants to use passive scanning, while the HA integration wants to use active scanning, your Bluetooth radio will have to switch back and forth, and will get confused. Moreover, switching back an forth between the two will require time, during which you can’t receive BLE data.

Most of the times, BLE monitor wins this battle, as it works on a lower level, so your HA machine will switch to passive scanning. This is the cause that HA BLE integrations will stop working, when setting BLE monitor to passive scanning. Using active scanning in BLE monitor switches the Bluetooth radio to active scanning, which works for both.

3 Likes

Thanks Ernst for your reply.

In HA I have installed the new Bluetooth integration:
Bluetooth

All Bluetooth devices are deleted in the BLE app.
BLE
The only device which it available in the BLE app is the BT dongle with Active Scan on.

I have added the BT devices in HA via the new Bluetooth integration:
Qingping Xiaomi

So far so good.
But when I delete the BLE integration all my BT devices become unavailable. The reason for this is the missing option: Active scan.
What I still do not understand, if the new BT integration has standard Active Scan on, why do I still need the BLE integration and put Active Scan on? After all no devices added to the BLE integration.

What do you mean by “delete BLE integration”. Which one do you delete, Bluetooth Low Energy Monitor or Bluetooth?

Delete the Low Energy Monitor. If I delete this integration I also delete the Active Scan option.

ah, no, if you delete BLE monitor, the active scanning will be set by the Bluetooth integration. However, you probably will have to do a full system restart (not only HA, but do a full system restart, to make sure your Bluetooth scanning mode is set by the Bluetooth integration). After that, everything should work (normally).

ok, I see. I did a full system restart but that didn’t work.
This week I will install my Yellow and will test it again.

That is very strange. The HA Bluetooth integration should work on its own. If it doesn’t work on your yellow either, please report a bug in the Home Assistant core repository.

I will test it on my Pi 3+ and later the week on my Yellow.

Hi Ernst,

I did the test with my Pi3+. It looked like that it worked after a power cycle yesterday evening but this morning none of the Bluetooth devices are available anymore.
Therefore I have to install the BLE Monitor again and put Active Scan on to get the Bluetooth devices back online.

When I receive the Yellow today I will do the same test. I let you know the result.

Thanks. So is there a way for me to find out whether the native Bluetooth integration uses passive or active scanning? I have disabled BLE, and all sensors are on the new integration, some directly on the Homeassistant machine (running in virtualbox in a Macos laptop), some on an ESPHome using Bluetooth proxy.

:nerd_face: My first post (Also Posted in [ESPHome BLE Gateway and other BLE components])(ESPHome BLE Gateway and other BLE components)
I have read post after post, and cannot get it. I have modified this code to use a DHT11,
:point_right:Here is the code for the esp32 server:

/*********
  Rui Santos
  Complete instructions at https://RandomNerdTutorials.com/esp32-ble-server-client/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/

#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLEUtils.h>
#include <BLE2902.h>

#include "DHT.h"
#define DHTTYPE DHT11  // DHT 11
#define dht_PIN 4      // Saras Room
DHT dht(dht_PIN, DHTTYPE);


//BLE server name
#define bleServerName "Temperature_ESP32"

float temp;
float tempF;
float hum;

// Timer variables
unsigned long lastTime = 0;
unsigned long timerDelay = 30000;

bool deviceConnected = false;

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "91bad492-b950-4226-aa2b-4ede9fa42f59"

// Temperature Characteristic and Descriptor
#ifdef temperatureCelsius
BLECharacteristic dhtTemperatureCelsiusCharacteristics("cba1d466-344c-4be3-ab3f-189f80dd7518", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor dhtTemperatureCelsiusDescriptor(BLEUUID((uint16_t)0x2902));
#else
BLECharacteristic dhtTemperatureFahrenheitCharacteristics("f78ebbff-c8b7-4107-93de-889a6a06d408", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor dhtTemperatureFahrenheitDescriptor(BLEUUID((uint16_t)0x2901));
#endif

// Humidity Characteristic and Descriptor
BLECharacteristic dhtHumidityCharacteristics("ca73b3ba-39f6-4ab3-91ae-186dc9577d99", BLECharacteristic::PROPERTY_NOTIFY);
BLEDescriptor dhtHumidityDescriptor(BLEUUID((uint16_t)0x2903));

//Setup callbacks onConnect and onDisconnect
class MyServerCallbacks : public BLEServerCallbacks {
  void onConnect(BLEServer *pServer) {
    deviceConnected = true;
  };
  void onDisconnect(BLEServer *pServer) {
    deviceConnected = false;
  }
};


void setup() {
  // Start serial communication
  Serial.begin(115200);

  // Init dht Sensor
  //initdht();
  dht.begin();
  // Create the BLE Device
  BLEDevice::init(bleServerName);

  // Create the BLE Server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  // Create the BLE Service
  BLEService *dhtService = pServer->createService(SERVICE_UUID);

// Create BLE Characteristics and Create a BLE Descriptor
// Temperature
#ifdef temperatureCelsius
  dhtService->addCharacteristic(&dhtTemperatureCelsiusCharacteristics);
  dhtTemperatureCelsiusDescriptor.setValue("dht temperature Celsius");
  dhtTemperatureCelsiusCharacteristics.addDescriptor(&dhtTemperatureCelsiusDescriptor);
#else
  dhtService->addCharacteristic(&dhtTemperatureFahrenheitCharacteristics);
  dhtTemperatureFahrenheitDescriptor.setValue("dht temperature Fahrenheit");
  dhtTemperatureFahrenheitCharacteristics.addDescriptor(&dhtTemperatureFahrenheitDescriptor);
#endif

  // Humidity
  dhtService->addCharacteristic(&dhtHumidityCharacteristics);
  dhtHumidityDescriptor.setValue("dht humidity");
  dhtHumidityCharacteristics.addDescriptor(new BLE2902());

  // Start the service
  dhtService->start();

  // Start advertising
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  pAdvertising->addServiceUUID(SERVICE_UUID);
  pServer->getAdvertising()->start();
  Serial.println("Waiting a client connection to notify...");
}

void loop() {
  if (deviceConnected) {
    if ((millis() - lastTime) > timerDelay) {
      // Read temperature as Celsius (the default)
      temp = dht.readTemperature();
      // Fahrenheit
      tempF = 1.8 * temp + 32;
      // Read humidity
      hum = dht.readHumidity();

//Notify temperature reading from DHT sensor
#ifdef temperatureCelsius
      static char temperatureCTemp[6];
      dtostrf(temp, 6, 2, temperatureCTemp);
      //Set temperature Characteristic value and notify connected client
      dhtTemperatureCelsiusCharacteristics.setValue(temperatureCTemp);
      dhtTemperatureCelsiusCharacteristics.notify();
      Serial.print("Temperature Celsius: ");
      Serial.print(temp);
      Serial.print(" ºC");
#else
      static char temperatureFTemp[6];
      dtostrf(tempF, 6, 2, temperatureFTemp);
      //Set temperature Characteristic value and notify connected client
      dhtTemperatureFahrenheitCharacteristics.setValue(temperatureFTemp);
      dhtTemperatureFahrenheitCharacteristics.notify();
      Serial.print("Temperature Fahrenheit: ");
      Serial.print(tempF);
      Serial.print(" ºF");
#endif

      //Notify humidity reading from dht
      static char humidityTemp[6];
      dtostrf(hum, 6, 2, humidityTemp);
      //Set humidity Characteristic value and notify connected client
      dhtHumidityCharacteristics.setValue(humidityTemp);
      dhtHumidityCharacteristics.notify();
      Serial.print(" - Humidity: ");
      Serial.print(hum);
      Serial.println(" %");

      lastTime = millis();
    }
  }
}

:point_right: Here is my ESPhome Yaml:

esphome:
  name: myble-client

esp32:
  board: wemos_d1_mini32
  framework:
    type: arduino

# Enable Home Assistant API
api:
  encryption:
    key: "gdOpgYZcDBnLlk9GJ46i4iOtmh3pS3hcC9dHjwI/0EI="

wifi:
  ssid: "DD_***"
  password: "****"
  manual_ip:
    static_ip: 192.168.3.160
    gateway: 192.168.3.1
    subnet: 255.255.255.0
    #dns1: 192.168.3.1
    #dns2: 8.8.8.8

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "esp_32_2 Fallback Hotspot"
    password: "***"

captive_portal:

# Enable logging
logger:

ota:
  password: "***"

web_server:
  port: 80
  ota: true

esp32_ble_tracker:

ble_client:
  - mac_address: 78:E3:6D:10:6D:BA
    id: my_ble_client  
    on_connect:
      then:
        - lambda: |-
            ESP_LOGD("ble_client_lambda", "Connected to BLE device");

text_sensor:
  - platform: template
    name: "My_Esp_Tmp_Atmpt"
    
  - platform: ble_client
    ble_client_id: 
    id: my_ble_client1
    name: "Humidity"
    service_uuid: "91bad492-b950-4226-aa2b-4ede9fa42f59"  # Humidity
    characteristic_uuid: "ca73b3ba-39f6-4ab3-91ae-186dc9577d99"  
    notify: true

  - platform: ble_client
    ble_client_id: 
    id: my_ble_client2
    name: "Temperature"
    service_uuid: "91bad492-b950-4226-aa2b-4ede9fa42f59"  # Temperarure
    characteristic_uuid: "f78ebbff-c8b7-4107-93de-889a6a06d408"  
    notify: true

This is the web page from the esp32-server:

How do I turn the Temp. and Humid. results into a home-assistant sensor or (entity) such as:
ESPHOME_TMP_SENSOR

I want to thank all who contribute to these forums, IT IS A HUGE ADVANTAGE TO US WANA-BEE PROGRAMMERS!

If you check the options on the integrations page for the config entry you can see if passive scanning is turned on for the adapter. If you don’t have options, your BlueZ version is too old to support passive scanning and you are getting active scanner.

For the esp32s, active scanning is the default so its active unless you altered it in the YAML

1 Like

hi everyone,

last week i added a bunch of ATC temp/hum sensors by inserting the macadresses in the configuration yaml, all fine. i got the sensors with all their ententities.
but now some ATC’s are “renewd” and have the “_2” on the end.
and offcourse this messes up my automations etc…

how can i keep the entities as the are?

Remove the old and rename the new.

2 Likes

thnx!
also just found this: ( discovery: False )
hope it works

ble_monitor:
discovery: False
devices:
# sensors
- mac: A4:C1:3xxxxxxx
name: “ATC kelder”
- mac: A4:C1:38:Dxxxxxx
name: “atc auto”
- mac: A4:C1:38:xxxxxx
name: “atc_keuken”
- mac: A4:C1:38:0E:BCxxxx
name: “atc_woonkamer”

Thanks for that - hopefully there is a roadmap for this.

Unfortunately, there isn’t a roadmap. Next release (2022.12) the pink Xiaomi plant sensors will be added.

Hello everybody! Is there any way to wake up a device? For example, I’m using a mi scale to monitor the weight of a gas cylinder, but the scale seems to get used to the weight of the cylinder and doesn’t update.