Skip to content

Commit

Permalink
few changes here and there, writing the data into influxsdb added
Browse files Browse the repository at this point in the history
  • Loading branch information
mdrocan committed Sep 15, 2023
1 parent 26fb5ce commit 226c6eb
Showing 1 changed file with 51 additions and 38 deletions.
89 changes: 51 additions & 38 deletions fronius.py
Original file line number Diff line number Diff line change
@@ -1,68 +1,81 @@
"""
Importing data from the Fronius device.
Import data from the Fronius inverter and write the data into InfluxDB.
"""
import json # pylint: disable=import-error
import time # pylint: disable=import-error
import requests # pylint: disable=import-error
from influxdb_client import (
InfluxDBClient,
) # pylint: disable=import-error
from influxdb_client.client.write_api import SYNCHRONOUS # pylint: disable=import-error
from requests.exceptions import HTTPError # pylint: disable=import-error

with open("config.json", "r", encoding="utf8") as config_file:
config = json.load(config_file)

URL = config["fronius"]["url"]
MAX_C = config["fronius"]["max_capacity"]
converted_MAX_C = int(MAX_C)

t = time.localtime()
current_time = time.strftime("%H:%M:%S", t)


def check_values():
"""
Actual logic of getting the JSON file with data and analyzing parts of it.
"""

try:
response = requests.get(URL, timeout=5)
response.raise_for_status()
jsondata = response.json()
with open("config.json", "r", encoding="utf8") as config_file:
config = json.load(config_file)

url = config["fronius"]["url"]
converted_max_c = int(config["fronius"]["max_capacity"])

current_time = time.strftime("%H:%M:%S", time.localtime())
print("Time:", current_time)
print("Response timestamp:", jsondata["Head"]["Timestamp"])

response = requests.get(url, timeout=5)
response.raise_for_status()
jsondata = response.json()

currently = jsondata["Body"]["Data"]["Site"]["P_PV"]
if currently == "None":
currently = "0"
print("Currently producing:", currently, "W")
print("Setup utilization:", "0%")

if currently is None:
print("Not producing.")
currently = 0
percentage = 0
else:
print("Currently producing:", currently, "W")
percentage = currently / converted_MAX_C * 100
print("Currently producing:", round(currently), "W")
percentage = round(currently / converted_max_c * 100)
print("Setup utilization:", round(percentage), "%")

today = jsondata["Body"]["Data"]["Site"]["E_Day"]
print("Today:", today, "Wh")

year = jsondata["Body"]["Data"]["Site"]["E_Year"]
print("Year:", year, "Wh")
timestamp = jsondata["Head"]["Timestamp"]
today = round(jsondata["Body"]["Data"]["Site"]["E_Day"])
year = round(jsondata["Body"]["Data"]["Site"]["E_Year"])
total_produce = round(jsondata["Body"]["Data"]["Site"]["E_Total"]) # noqa: E501

total_produce = jsondata["Body"]["Data"]["Site"]["E_Total"] # noqa: E501
print("Total:", total_produce, "Wh")

data = {
"currently": currently,
"percentage": percentage,
"today": today,
"year": year,
"total_produce": total_produce,
}
data = [
{
"measurement": "fronius",
"time": timestamp,
"fields": {
"currently": round(currently),
"percentage": round(percentage),
"today": round(today),
"year": round(year),
"total_produce": round(total_produce),
},
}
]

with open("data.json", "w", encoding="utf8") as outfile:
json.dump(data, outfile, indent=4)

bucket = config["influxdb"]["bucket"]
org = config["influxdb"]["org"]

client = InfluxDBClient(
url=config["influxdb"]["url"], token=config["influxdb"]["token"], org=org
)

# print(data)
write_api = client.write_api(write_options=SYNCHRONOUS)
write_api.write(bucket=bucket, org=org, record=data)

except HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except Exception as err: # pylint: disable=broad-except
print(f"Other error occurred: {err}")


if __name__ == "__main__":
Expand Down

0 comments on commit 226c6eb

Please sign in to comment.