knx-monitor/db.py

29 lines
1.0 KiB
Python

import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
from dotenv import load_dotenv
import os
from os.path import dirname, join
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
bucket=os.environ.get('INFLUX_BUCKET')
org=os.environ.get('INFLUX_ORG')
token=os.environ.get('INFLUX_TOKEN')
url=os.environ.get('INFLUX_URL')
client = influxdb_client.InfluxDBClient(url=url, token=token, org=org)
write_api = client.write_api(write_options=SYNCHRONOUS)
def publish_measurement(value):
# Only push to Influx if not logging
if (os.environ.get('LOG_ONLY', "0") != "1"):
try:
p = influxdb_client.Point(f'KNX-{value["type"]}').tag("name", f'{value["name"]}').tag("destination", f'{value["destination"]}').tag("description", f'{value["description"]}').field("value", value["value"])
write_api.write(bucket=bucket, org=org, record=p)
except Exception as e:
print(f"Could not send value {value} to {url}")
print(e)
else:
print(f"debug: {value}")