This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentsoe_telegraf_pl.py
executable file
·94 lines (65 loc) · 3.02 KB
/
entsoe_telegraf_pl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env /usr/bin/python3.9
# coding: utf-8
#Get day-ahead energy prices from Entsoe API and output stuff in a format readable by Telegraf
# Called few times a day (plus in startup) from Telegraf, results are resent to arska and InfluDb
import settings as s
import time
from datetime import datetime
from datetime import timedelta
from entsoe import EntsoePandasClient
import traceback #error reporting
import pandas as pd
# Telegraf plugin
from telegraf_pyplug.main import print_influxdb_format, datetime_tzinfo_to_nano_unix_timestamp
import sys
import pytz
arska_settings = s.read_settings(s.arska_file_name)
timeZoneLocal = arska_settings["timeZoneLocal"]
EntsoEUAPIToken = arska_settings["EntsoEUAPIToken"]
SpotPriceArea = arska_settings["SpotPriceArea"]
tz_local = pytz.timezone(timeZoneLocal)
#part of the code from https://github.com/EnergieID/entsoe-py
def getNordPoolSPOTfromEntsoEU():
if not EntsoEUAPIToken:
print ("EntsoEU API Token undefined")
return
day_in_seconds = 3600*24
#first hour of this day
dt = datetime.fromtimestamp(int(time.time()/(3600*24))*(3600*24)-3600)
start1 = tz_local.localize(dt)
# start1 = pd.Timestamp(start1,tz=timeZoneLocal)
start1 = pd.Timestamp(dt,tz=timeZoneLocal)
end1 = pd.Timestamp(dt,tz=timeZoneLocal)
end1 = end1 + timedelta(days=3) # can be a bit longer in the future, you get what you get
#if type(start) != pd.Timestamp or type(end) != pd.Timestamp:
#print ("start1:",type(start1))
#print ("end1:",type(end1))
country_code = SpotPriceArea
tag_name = "dayahead"
try:
#print ("Querying - country code: {:s}, start: {:s}, end: {:s}".format(country_code, start1.strftime("%Y-%m-%dT%H:%M:%S"),end1.strftime("%Y-%m-%dT%H:%M:%S")))
client = EntsoePandasClient(EntsoEUAPIToken)
# methods that return Pandas Series
ts =client.query_day_ahead_prices(country_code, start=start1,end=end1)
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception( exc_type,exc_value, exc_traceback,limit=5, file=sys.stdout)
print ("Cannot get prices 1", sys.exc_info())
return
try:
for tr in ts.keys():
trdt = tr.to_pydatetime()
energyPriceSpot = (ts[tr]/10) #+s.spotMarginPurchase
print_influxdb_format(
measurement="spot",
#fields={"energyPriceSpot":energyPriceSpot,"energyPrice":energyPrice,"transferPrice": transferPrice,"totalPrice":totalPrice },
fields={"energyPriceSpot":energyPriceSpot},
tags = { "priceArea": SpotPriceArea, "name" : tag_name},
nano_timestamp=datetime_tzinfo_to_nano_unix_timestamp(trdt)
)
except:
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception( exc_type,exc_value, exc_traceback,limit=5, file=sys.stdout)
print ("Cannot get prices 2", sys.exc_info())
return
getNordPoolSPOTfromEntsoEU()