knx-monitor/db.py

25 lines
891 B
Python
Raw Normal View History

2022-12-19 12:24:47 +01:00
import influxdb_client
from influxdb_client.client.write_api import SYNCHRONOUS
from dotenv import load_dotenv
import os
2022-12-20 15:39:41 +01:00
from os.path import dirname, join
2022-12-19 12:24:47 +01:00
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):
2022-12-20 15:39:41 +01:00
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)