Bluetooth

February 1, 2025 ยท View on GitHub

This module contains three bluetooth sensors:

Find address for Bluetooth device

If the device address is unknown, select the method to determine it depending on the Bluetooth type.

Regular Bluetooth device

Make sure the Bluetooth device is visible (e. g. in pairing mode), then run:

hcitool scan

The output lists all visible Bluetooth devices as address / name pairs.

Bluetooth low energie device (BTLE)

Use this method for Bluetooth 4.0 devices. Make sure the Bluetooth device is active (switched on), then run:

sudo hcitool lescan

The output will be a address / name pairs for each BTLE message received. After discovering the desired address hit ctrl + c to stop the output.

bt.btle_sensor.BtleSensor

A Polling Sensor that listens for BTLE broadcasts from devices with a given BT address for a short time on each poll. When a packet for a device of interest is received during that period, the ON value is published to the destination associated with the device address. When no packet is received for a device of interest, the OFF value is published.

Dependencies

This sensor uses bluepy to receive and parse the BTLE packets. It depends also on the packages libglib2.0-dev and bluetooth. Network capabilities must be granted to the bluepy-helper executable, so it can access the Bluetooth hardware.

cd /srv/sensorReporter
sudo ./install_dependencies.sh bt
sudo setcap 'cap_net_admin+ep' $(find -name bluepy-helper)

Parameters

ParameterRequiredRestrictionsPurpose
ClassXbt.btle_sensor.BtleSensor
ConnectionsXdictionary of connectorsDefines where to publish the sensor status for each connection. Look at connection readme's for 'Actuator / sensor relevant parameters' for details.
LevelDEBUG, INFO, WARNING, ERRORWhen provided, sets the logging level for the sensor.
PollXA number in seconds, greater than TimeoutHow often to poll for broadcasts.
TimeoutXA number in secondsHow long to listen for BTLE packets during a single poll.
AddressXXBT MAC address format (i.e A1:B2:C3:E4:F5:67)The MAC address of a device to listen for broadcasts from. X must be a number starting from 1 and each subsequent address must be sequential in numbering.
Valueslist of strings or dictionaryValues to replace the default state message for all outputs (default is ON, OFF). For details see below.

Values parameter

With this parameter the default state messages for all output can be overwrite. Two different layouts are possible. To override the state message for all defined connections, configure a list of two string items:

Values:
    - 'ON'
    - 'OFF'

The fist string will be send if the configured address is detected, the second if not.

If separate state messages for each connection are desired, configure a dictionary of connection names containing the string item list:

Values:
    <connection_name>:
        - 'ON'
        - 'OFF'
    <connection_name2>:
        - 'high'
        - 'low'

If a configured connection is not present in the Values parameter it will use the sensor default state messages (ON, OFF).

Output

The BtleSensor can have 1 or many outputs depending on how many addresses are defined. These can be configured within the 'Connections' section (Look at connection readme's for 'Actuator / sensor relevant parameters' for details). When using with the openHAB connection configure a switch/string item.

OutputPurpose
DestinationXWhere to send send the presence state (default send ON when the device is present and OFF when not). X is a number starting with 1 and incrementing to list more than one device.

Example Config

Logging:
    Syslog: yes
    Level: INFO

Connection1:
    Class: openhab_rest.rest_conn.OpenhabREST
    Name: openHAB
    URL: http://localhost:8080
    RefreshItem: Test_Refresh

SensorBtleScanner:
    Class: bt.btle_sensor.BtleSensor
    Poll: 10
    Connections:
        openHAB:
            Destination1:
                Item: dev1
            Destination2:
                Item: dev2
            Destination3:
                Item: dev3
    Timeout: 9
    Address1: aa:bb:cc:dd:ee:ff
    Address2: 11:22:33:44:55:66
    Address3: 77:88:99:aa:bb:cc
    Values:
        - present
        - away
    Level: DEBUG

bt.btscan_sensor.SimpleBtSensor

A Polling Sensor that polls BT devices by MAC address to determine if they are present or not.

Look at the PyBluez examples for inquiry.py for a script that can be used to discover the MAC address of a device. Run the script and put the device into pairing mode and the MAC address and device name will be printed out.

Dependencies

The sensor uses PyBluez to scan for BT devices. It depends also on the packages bluetooth and bluez

cd /srv/sensorReporter
sudo ./install_dependencies.sh bt

Parameters

ParameterRequiredRestrictionsPurpose
ClassXbt.btscan_sensor.SimpleBtSensor
ConnectionsXdictionary of connectorsDefines where to publish the sensor status for each connection. Look at connection readme's for 'Actuator / sensor relevant parameters' for details.
LevelDEBUG, INFO, WARNING, ERRORWhen provided, sets the logging level for the sensor.
PollXA number in seconds, greater than 25How often to poll for devices, blocks for 25 seconds.
AddressXXBT MAC address format (i.e A1:B2:C3:E4:F5:67)The MAC address of a device to scan for it's presence. X is a number starting with 1 and incrementing to list more than one device.

Output

The SimpleBtSensor can have 1 or many outputs depending on how many addresses are defined. These can be configured within the 'Connections' section (Look at connection readme's for 'Actuator / sensor relevant parameters' for details). When using with the openHAB connection configure a switch/string item.

OutputPurpose
DestinationXWhere to send send ON then the device is present and OFF when not. X is a number starting with 1 and incrementing to list more than one device.

Example Config

Logging:
    Syslog: yes
    Level: INFO

Connection1:
    Class: openhab_rest.rest_conn.OpenhabREST
    Name: openHAB
    URL: http://localhost:8080
    RefreshItem: Test_Refresh

SensorMyPhone:
    Class: bt.btscan_sensor.SimpleBtSensor
    Poll: 26
    Connections:
        openHAB:
            Destination1:
                Item: phone1
            Destination2:
                Item: phone2
    Address1: aa:bb:cc:dd:ee:ff
    Address2: 11:22:33:44:55:66
    Level: DEBUG

bt.govee_sensor.GoveeSensor

A Background Sensor that listens for and parses BTLE packets from Govee H5075 temperature and humidity sensors, publishing the readings and device status information.

Dependencies

This sensor uses the bleson library to listen for and parse the packets. Network capabilities must be granted to the python3 executable, so it can access the Bluetooth hardware.

cd /srv/sensorReporter
sudo ./install_dependencies.sh bt
sudo setcap 'cap_net_raw,cap_net_admin+ep' $(readlink -f $(find -name python3))

Parameters

ParameterRequiredRestrictionsPurpose
ClassXbt.govee_sensor.GoveeSensor
ConnectionsXdictionary of connectorsDefines where to publish the sensor status for each connection. This sensor has 5 outputs, see below. Look at connection readme's for 'Actuator / sensor relevant parameters' for details.
LevelDEBUG, INFO, WARNING, ERRORWhen provided, sets the logging level for the sensor.
TempUnitF or CThe temperature units. Default is C
AddressXBT MAC address format (i.e a4:c1:38:e4:f5:67)The MAC address of the Govee H5075 sensor. Should start with "a4:c1:38".

If the address is unknown use the example config and check the sensor_reporter log for debug messages with the UUID GV5072_

Outputs

The GoveeSensor has 5 outputs which can be configured within the 'Connections' section (Look at connection readme's for 'Actuator / sensor relevant parameters' for details).

OutputPurpose
TemperatureWhere to publish the temperature.
HumidityWhere to publish the humidity.
BatteryWhere to publish the battery charge (integer between 0 and 100)
RSSIWhere to publish the signal strength (integer between 0 and -100)
DeviceNameWhere to publish the self reported name. Usually GVH5072_XXXX

Example Config

Logging:
    Syslog: yes
    Level: INFO

Connection1:
    Class: mqtt.mqtt_conn.MqttConnection
    Name: MQTT
    Client: test
    User: user
    Password: password
    Host: localhost
    Port: 1883
    Keepalive: 10
    RootTopic: sensor_reporter
    TLS: NO

SensorGoveeKitchen:
    Class: bt.govee_sensor.GoveeSensor
    Connections:
        MQTT:
            Temperature:
                StateDest: govee/temp
            Humidity:
                StateDest: govee/humid
            Battery:
                StateDest: govee/battery
            RSSI:
                StateDest: govee/rssi
            DeviceName:
                StateDest: govee/uuid
    TempUnit: F
    Address: a4:c1:38:00:00:00
    Level: DEBUG

Given the above configuration, the battery level would be reported to sensor_reporter/govee/battery.