Read volumes

Read the current volume of every volume zone with the Python client. Install the blickfeld_qb2 library, save the script below as read_volumes.py and run it with the hostname of the sensor and the secret of an application key created in the WebGUI:

pip install -U blickfeld_qb2
python3 read_volumes.py qb2-<serial number> <application key>
read_volumes.py
import sys

import blickfeld_qb2
from blickfeld_qb2.betterproto import which_one_of


def describe(name, zone):
    return f"{name}: {zone.volume:.1f} m³ ({zone.coverage * 100:.0f} % measured)"


def read_volumes(host, key=None):
    token = blickfeld_qb2.TokenFactory(application_key_secret=key) if key else None

    with blickfeld_qb2.Channel(host, token=token) as channel:
        states = blickfeld_qb2.percept_processing.services.States(channel).get().states

        for state in states.states.values():
            kind, zone = which_one_of(state, "state_type")
            if kind == "volume":
                print(describe(state.generator_name, zone))


if __name__ == "__main__":
    read_volumes(*sys.argv[1:3])

Output (example):

Bunker 1: 178.8 m³ (61 % measured)
Bunker 2: 150.1 m³ (67 % measured)