Archives de catégorie : HomeAssistant

Drop InfluxDB 1.x measurements with a Python script

I configured my Home Assistant influxdb integration to log absolutely everything, but my InfluxDB database grew out of control.
After setting up proper filtering for the entities I really needed, I had to manually delete (drop) all the unwanted measurements.

First, I dumped all the measurements in a text file :

influx -database ha -username admin -execute "show measurements"

With the influx command line tool and a bit of bash scripting, I was able to send an InfluxQL command DROP MEASUREMENT, but often some measurements were not dropped (still displayed with « SHOW MEASUREMENTS »), and sometimes still had some records.

This is a known issue and it won’t probably be fixed in versions 1.x.

Finally, I ended up writing a « quick and dirty » python script to drop the measurements listed in a text file.
It performs the following steps:

  • Reads the measurement to drop, line by line, from a text file
  • Issues a SHOW MEASUREMENT query, and iterates through the results until a match is found
  • Tries to display the count of points found
  • Executes the DROP MEASUREMENT query
  • Attemps to display the points count again (but I never saw this happening) or, if the measurement is not found, considers it was successfully dropped

Ensure you have installed the influxdb pip package :

pip install influxdb

Here is the script :

import time
from influxdb import InfluxDBClient
client = InfluxDBClient('nas', 8086, 'admin', '', 'ha') 
file = open("//nas/data/ms-2025-01-04-delete.txt")
for line in file :
    ligne = line.strip()
    print()
    print("%s : " % ligne, end="")
    measurements = client.get_list_measurements()
    for measurement in measurements :
        if ligne == measurement['name'] :
            print("found, ", end="")
            result = client.query('select count(value) from "' + ligne + '";')
            try:
                print("count before: %d, " % result.raw['series'][0]['values'][0][1], end="")
            except Exception :
                print("N/A, ", end="")
            client.drop_measurement(ligne)
            result = client.query('select count(value) from "' + ligne + '";')
            try:
                count_after = result.raw['series'][0]['values'][0][1]
                print("count after: %d" % count_after, end="")
            except Exception :
                print("drop OK (not found)", end="")
file.close()

Theengs Gateway on LibreElec

I wanted HomeAssistant to use my Body Mi Scale with Home Assistant, but my server is too far from the scale and can’t detect the bluetooth data.

But I do have a Raspberry running LibreElec (in a beautiful Flirc case) near my bathroom, and I decided to use it as a bluetooth proxy. LibreElec being a « Just Enough OS », I didn’t want to mess with it, as my config should survive version upgrades.
Fortunately, LibreElec supports Docker, and a Docker image exists

Here are my quick notes :

LibreElec

Use a recent version of LibreElec (12.x in my case)

Make sure bluetooth is enable in Kodi / Settings / LibreElec / Services

Make sure the Docker addon for Kodi is installed

SSH in LibreElec as root

Pull the docker image :

# docker pull theengs/gateway

And run it with the following settings :

# docker run --restart always --network host -e MQTT_HOST=mqtt_broker_hostname -e BLE=true -e DISCOVERY=true -e SCANNING_MODE=active -e PUBLISH_ALL=false -e SCAN_TIME=1 -e TIME_BETWEEN=20 -v /var/run/dbus:/var/run/dbus --name TheengsGateway theengs/gateway

Each time you use your scale, you should see appear after ~10 secs some data into the home/TheengsGateway/BTtoMQTT topic

Optional : use passive scanning

Because passive scanning is soooo much cool than active (I guess that’s a good enough reason), you can enable it with a few extra steps.

First, enable experimental features in bluez :

# systemctl edit bluetooth.service

and add the following lines :

[Service]
ExecStart=
ExecStart=/usr/lib/bluetooth/bluetoothd --experimental $BLUEZ_ARGS $BLUEZ_DEBUG

Then restart bluetooth

# systemctl restart bluetooth

And replace the docker command with

# docker run --restart always --network host -e MQTT_HOST=mqtt_broker_hostname -e BLE=true -e DISCOVERY=true -e SCANNING_MODE=passive -e PUBLISH_ALL=false -v /var/run/dbus:/var/run/dbus --name TheengsGateway theengs/gateway

Note : upon reboot, because the container starts before the Wifi is up, using a hostname for the MQTT broker resulted in a connection failure and the container being stucked. I opened an issue on github. To work around this issue, i simply used the IP address for the MQTT broker instead.

HomeAssistant

If MQTT Integration is installed in Home Assistant, it should discover the scale and its sensors.
You can now install the bodymyscale integration.