From 64e4024ab5cf4a76cdec87341a40a1d59c78e5d0 Mon Sep 17 00:00:00 2001 From: m4dm4rtig4n Date: Fri, 22 Oct 2021 21:24:13 +0200 Subject: [PATCH 1/4] add debug influxdb --- app/influxdb.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/influxdb.py b/app/influxdb.py index 0169a75f..857883c5 100644 --- a/app/influxdb.py +++ b/app/influxdb.py @@ -38,6 +38,7 @@ def forceRound(x, n): value = result[2] value_kw = value / 1000 current_price = forceRound(value_kw * price["BASE"], 4) + f.log(f"Insert daily {date} => {value}", "DEBUG") dateObject = datetime.strptime(date, '%Y-%m-%d') - relativedelta(hours=2) p = influxdb_client.Point("enedisgateway_daily") \ .tag("pdl", pdl) \ @@ -61,8 +62,8 @@ def forceRound(x, n): value_wh = value * interval_length / 60 value_kwh = value_wh / 1000 current_price = forceRound(value_kwh * price[measure_type], 4) + f.log(f"Insert detail {date} => {value}", "DEBUG") dateObject = datetime.strptime(date, '%Y-%m-%d %H:%M:%S') - relativedelta(hours=2) - # pprint(current_price) p = influxdb_client.Point("enedisgateway_detail") \ .tag("pdl", pdl) \ .tag("measure_type", measure_type) \ From d6aecf7455ba3aae77f38c78c19b548e1f0869ae Mon Sep 17 00:00:00 2001 From: m4dm4rtig4n Date: Fri, 22 Oct 2021 23:05:52 +0200 Subject: [PATCH 2/4] add influxdb check connexion --- app/main.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index ec5fcc5a..002542aa 100755 --- a/app/main.py +++ b/app/main.py @@ -199,6 +199,12 @@ else: influxdb_host = "" ######################################################################################################################## +# INFLUXDB_PORT +if "INFLUXDB_PORT" in os.environ: + influxdb_port = str(os.environ['INFLUXDB_PORT']) +else: + influxdb_port = "" +######################################################################################################################## # INFLUXDB_TOKEN if "INFLUXDB_TOKEN" in os.environ: influxdb_token = str(os.environ['INFLUXDB_TOKEN']) @@ -604,16 +610,22 @@ def run(): client = f.connect_mqtt() client.loop_start() except: - f.log("MQTT : Connection failed") + f.log("MQTT : Connection failed", "CRITICIAL") # INFLUXDB if influxdb_enable == True: influxdb = influxdb_client.InfluxDBClient( - url=influxdb_host, + url=f"http://{influxdb_host}:{influxdb_port}", token=influxdb_token, org=influxdb_org ) + health = influxdb.health() + if health.status == "pass": + f.log("InfluxDB : Connection success") + else: + f.log("InfluxDB : Connection failed", "CRITICAL") + influxdb_api = influxdb.write_api(write_options=ASYNCHRONOUS) run() From 8de9b1e7fe5ce9e88ef684ebd78ed276e43a32a9 Mon Sep 17 00:00:00 2001 From: m4dm4rtig4n Date: Fri, 22 Oct 2021 23:13:37 +0200 Subject: [PATCH 3/4] remove influxdb params --- app/daily.py | 10 ++++------ app/detail.py | 4 +--- app/main.py | 8 ++++---- 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/app/daily.py b/app/daily.py index f4fd7930..3783c046 100755 --- a/app/daily.py +++ b/app/daily.py @@ -3,15 +3,13 @@ from datetime import datetime, timedelta from dateutil.relativedelta import * from pprint import pprint -import influxdb_client -from influxdb_client.client.write_api import SYNCHRONOUS from importlib import import_module main = import_module("main") f = import_module("function") -def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activation_date=datetime.now()): +def getDaily(cur, con, client, mode="consumption", last_activation_date=datetime.now()): max_days = 1095 max_days_date = datetime.now() + relativedelta(days=-max_days) pdl = main.pdl @@ -31,7 +29,7 @@ def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activ dateEnded = datetime.now() dateEnded = dateEnded.strftime('%Y-%m-%d') - data = dailyBeetwen(cur, con, influxdb_api, pdl, mode, dateBegin, dateEnded, last_activation_date) + data = dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date) if "error_code" in data: f.publish(client, f"{pdl}/{mode}/current_year/error", str(1)) for key, value in data.items(): @@ -92,7 +90,7 @@ def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activ if last_activation_date > datetime.strptime(dateEnded, '%Y-%m-%d'): f.log(" - Skip (activation date > dateEnded)") else: - data = dailyBeetwen(cur, con, influxdb_api, pdl, mode, dateBegin, dateEnded, last_activation_date) + data = dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date) if "error_code" in data: f.publish(client, f"{pdl}/{mode}/year-{current_year}/error", str(1)) for key, value in data.items(): @@ -167,7 +165,7 @@ def getDaily(cur, con, client, influxdb_api=None, mode="consumption", last_activ return ha_discovery -def dailyBeetwen(cur, con, influxdb_api, pdl, mode, dateBegin, dateEnded, last_activation_date): +def dailyBeetwen(cur, con, pdl, mode, dateBegin, dateEnded, last_activation_date): response = {} lastYears = datetime.strptime(dateEnded, '%Y-%m-%d') diff --git a/app/detail.py b/app/detail.py index 10b77312..f70abe9b 100755 --- a/app/detail.py +++ b/app/detail.py @@ -4,8 +4,6 @@ from dateutil.relativedelta import * from pprint import pprint import re -import influxdb_client -from influxdb_client.client.write_api import SYNCHRONOUS from importlib import import_module @@ -15,7 +13,7 @@ date_format = "%Y-%m-%d %H:%M:%S" -def getDetail(cur, con, client, influxdb_api=None, mode="consumption", last_activation_date=datetime.now(), offpeak_hours=None, +def getDetail(cur, con, client, mode="consumption", last_activation_date=datetime.now(), offpeak_hours=None, measure_total=None): max_days = 730 diff --git a/app/main.py b/app/main.py index 002542aa..5369e135 100755 --- a/app/main.py +++ b/app/main.py @@ -413,7 +413,7 @@ def run(): if get_consumption == True: f.logLine() f.log("Get Consumption :") - ha_discovery_consumption = day.getDaily(cur, con, client, influxdb_api, "consumption", last_activation_date) + ha_discovery_consumption = day.getDaily(cur, con, client, "consumption", last_activation_date) # pprint(ha_discovery_consumption) if ha_autodiscovery == True: f.logLine() @@ -445,7 +445,7 @@ def run(): if get_consumption_detail == True: f.log("Get Consumption Detail:") - ha_discovery_consumption = detail.getDetail(cur, con, client, influxdb_api, "consumption", last_activation_date, offpeak_hours) + ha_discovery_consumption = detail.getDetail(cur, con, client, "consumption", last_activation_date, offpeak_hours) if ha_autodiscovery == True: f.logLine() f.log("Home Assistant auto-discovery (Consumption Detail) :") @@ -477,7 +477,7 @@ def run(): if get_production == True: f.logLine() f.log("Get production :") - ha_discovery_production = day.getDaily(cur, con, client, influxdb_api, "production", last_activation_date) + ha_discovery_production = day.getDaily(cur, con, client, "production", last_activation_date) if ha_autodiscovery == True: f.logLine() f.log("Home Assistant auto-discovery (Production) :") @@ -507,7 +507,7 @@ def run(): if get_production_detail == True: f.logLine() f.log("Get production Detail:") - ha_discovery_consumption = detail.getDetail(cur, con, client, influxdb_api, "production", last_activation_date, offpeak_hours) + ha_discovery_consumption = detail.getDetail(cur, con, client, "production", last_activation_date, offpeak_hours) if ha_autodiscovery == True: f.logLine() f.log("Home Assistant auto-discovery (Production Detail) :") From 7ed030d6797d9624caf36de551f232764cb0b889 Mon Sep 17 00:00:00 2001 From: m4dm4rtig4n Date: Fri, 22 Oct 2021 23:21:48 +0200 Subject: [PATCH 4/4] readme --- README.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index c4f26525..562ee84f 100755 --- a/README.md +++ b/README.md @@ -168,6 +168,7 @@ CARD_MYENEDIS="False" CURRENT_PLAN="BASE" INFLUXDB_ENABLE="False" INFLUXDB_HOST="" +INFLUXDB_PORT="" INFLUXDB_TOKEN="" INFLUXDB_ORG="" INFLUXDB_BUCKET="" @@ -202,6 +203,7 @@ docker run -it --restart=unless-stopped \ -e CURRENT_PLAN="$CURRENT_PLAN" \ -e INFLUXDB_ENABLE="$INFLUXDB_ENABLE" \ -e INFLUXDB_HOST="$INFLUXDB_HOST" \ + -e INFLUXDB_PORT="$INFLUXDB_PORT" \ -e INFLUXDB_TOKEN="$INFLUXDB_TOKEN" \ -e INFLUXDB_ORG="$INFLUXDB_ORG" \ -e INFLUXDB_BUCKET="$INFLUXDB_BUCKET" \ @@ -244,11 +246,12 @@ services: DEBUG: "False" CARD_MYENEDIS: "False" CURRENT_PLAN: "BASE" - INFLUXDB_ENABLE: "False" - INFLUXDB_HOST: "" - INFLUXDB_TOKEN: "" - INFLUXDB_ORG: "" - INFLUXDB_BUCKET: "" + INFLUXDB_ENABLE: "False" + INFLUXDB_HOST: "" + INFLUXDB_PORT: "" + INFLUXDB_TOKEN: "" + INFLUXDB_ORG: "" + INFLUXDB_BUCKET: "" logging: options: max-size: "10m"