forked from flopp999/NordPoolSpotPrice-Domoticz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin.py
327 lines (295 loc) · 10.8 KB
/
plugin.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# NordPoolSpotPrice Python Plugin
#
# Author: flopp999
#
"""
<plugin key="NordPoolSpotPrice1" name="NordPoolSpotPrice 0.25" author="flopp999" version="0.25" wikilink="https://github.com/flopp999/NordPoolSpotPrice-Domoticz" externallink="https://www.nordpoolgroup.com/api/marketdata/page/10">
<description>
<h2>NordPoolSpotPrice is used to read data from https://www.nordpoolgroup.com/api/marketdata/page/10</h2><br/>
<h2>Support me with a coffee &<a href="https://www.buymeacoffee.com/flopp999">https://www.buymeacoffee.com/flopp999</a></h2><br/>
<h3>Features</h3>
<h2>Presentation of data is in kWh and nothing is added eg. TAX, VAT</h2>
<h3>Configuration</h3>
</description>
<params>
<param field="Mode1" label="Area" width="320px" required="true" default="Identifier">
<options>
<option label="AT" value="AT" />
<option label="BE" value="BE" />
<option label="DE-LU" value="DE-LU" />
<option label="DK1" value="DK1" />
<option label="DK2" value="DK2" />
<option label="EE" value="EE" />
<option label="FI" value="FI" />
<option label="FR" value="FR" />
<option label="LT" value="LT" />
<option label="LV" value="LV" />
<option label="NL" value="NL" />
<option label="NO Bergen" value="Bergen" />
<option label="NO Kr.sand" value="Kr.sand" />
<option label="NO Molde" value="Molde" />
<option label="NO Oslo" value="Oslo" />
<option label="NO Tr.heim" value="Tr.heim" />
<option label="NO Tromsø" value="Tromsø" />
<option label="SE1" value="SE1" />
<option label="SE2" value="SE2" />
<option label="SE3" value="SE3" />
<option label="SE4" value="SE4" />
</options>
</param>
<param field="Mode2" label="Divide values by 10(/10) so it will be presented in Öre/Øre/Cent" width="70px">
<options>
<option label="Yes" value="Yes" />
<option label="No" value="No" />
</options>
</param>
<param field="Mode6" label="Debug to file (NordPoolSpotPrice.log)" width="70px">
<options>
<option label="Yes" value="Yes" />
<option label="No" value="No" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
from nordpool import elspot
Package = True
try:
import requests, json, os, logging
except ImportError as e:
Package = False
try:
from logging.handlers import RotatingFileHandler
except ImportError as e:
Package = False
try:
from datetime import datetime, timedelta
except ImportError as e:
Package = False
dir = os.path.dirname(os.path.realpath(__file__))
logger = logging.getLogger("NordPoolSpotPrice")
logger.setLevel(logging.INFO)
handler = RotatingFileHandler(dir+'/NordPoolSpotPrice.log', maxBytes=1000000, backupCount=5)
logger.addHandler(handler)
class BasePlugin:
enabled = False
def __init__(self):
self.hour = 0
self.CurrentPriceUpdated = False
self.TodayPriceUpdated = False
return
def onStart(self):
WriteDebug("===onStart===")
self.Area = Parameters["Mode1"]
self.Divide = Parameters["Mode2"]
if len(self.Area) < 3:
Domoticz.Log("Area too short")
WriteDebug("Area too short")
if os.path.isfile(dir+'/NordPoolSpotPrice.zip'):
if 'NordPoolSpotPrice' not in Images:
Domoticz.Image('NordPoolSpotPrice.zip').Create()
self.ImageID = Images["NordPoolSpotPrice"].ID
if self.Area == "SE1" or "SE2" or "SE3" or "SE4":
self.currency = "SEK"
if self.Divide == "Yes":
self.Unit = "öre"
else:
self.Unit = "kronor"
elif self.Area == "DK1" or "DK2":
self.currency = "DKK"
if self.Divide == "Yes":
self.Unit = "øre"
else:
self.Unit = "kroner"
elif self.Area == "Oslo" or "Kr.sand" or "Bergen" or "Molde" or "Tr.heim" or "Tromsø":
self.currency = "NOK"
if self.Divide == "Yes":
self.Unit = "øre"
else:
self.Unit = "kroner"
else:
self.curency = "EUR"
if self.Divide == "Yes":
self.Unit = "cents"
else:
self.Unit = "euro"
def onHeartbeat(self):
if CheckInternet() == True:
HourNow = (datetime.now().hour)
MinuteNow = (datetime.now().minute)
if MinuteNow < 59 and self.CurrentPriceUpdated is False:
WriteDebug("onHeartbeatGetCurrentPrice")
CurrentPrice(HourNow)
if MinuteNow == 59 and self.CurrentPriceUpdated is True:
self.CurrentPriceUpdated = False
# if HourNow >= 0 and MinuteNow >= 2 and MinuteNow < 59 and self.TodayPriceUpdated is False:
if MinuteNow == 28 and self.TodayPriceUpdated is False:
TodayPrice()
if MinuteNow == 29 and self.TodayPriceUpdated is True:
self.TodayPriceUpdated = False
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def TodayPrice():
hour = 0
HourNow = (datetime.now().hour)
prices_spot = elspot.Prices(_plugin.currency)
price=prices_spot.hourly(end_date=datetime.now().date(),areas=[_plugin.Area])
# Domoticz.Log(str(price))
for each,b in price["areas"][_plugin.Area].items():
if each == "values":
for each in price["areas"][_plugin.Area]["values"]:
if hour < HourNow:
FutureHour = FuturePrice(hour)
if str(FutureHour) != 'inf':
Domoticz.Log("Hour "+str(hour)+" "+str(round(FutureHour/10.0,1)))
UpdateDevice(int(hour), FutureHour, str("Hour"+" "+str(hour)))
else:
Domoticz.Log("Hour "+str(hour)+" "+str(round(each["value"]/10.0,1)))
UpdateDevice(int(hour), each["value"], str("Hour"+" "+str(hour)))
else:
Domoticz.Log("Hour "+str(hour)+" "+str(round(each["value"]/10.0,1)))
UpdateDevice(int(hour), each["value"], str("Hour"+" "+str(hour)))
hour += 1
elif each == "Min":
Domoticz.Log(str(each+" "+str(round(b/10.0,1))))
UpdateDevice(int(24), b, str("Min"))
elif each == "Max":
Domoticz.Log(each+" "+str(round(b/10.0,1)))
UpdateDevice(int(25), b, str("Max"))
elif each == "Average":
Domoticz.Log(each+" "+str(round(b/10.0,1)))
UpdateDevice(int(26), b, str("Average"))
_plugin.TodayPriceUpdated = True
Domoticz.Log("Today Price Updated")
def CurrentPrice(CurrentHour):
UpdateDevice(int(27), TodayPriceHour(CurrentHour), str("CurrentPrice"))
_plugin.CurrentPriceUpdated = True
Domoticz.Log("Current Price Updated")
# Domoticz.Log(TodayPriceHour(CurrentHour))
def FuturePrice(CurrentHour):
hour = 0
prices_spot = elspot.Prices(_plugin.currency)
price=prices_spot.hourly(areas=[_plugin.Area])
for each,b in price["areas"][_plugin.Area].items():
if each == "values":
for each in price["areas"][_plugin.Area]["values"]:
if hour == CurrentHour:
return each["value"]
break
hour += 1
def TodayPriceHour(CurrentHour):
hour = 0
prices_spot = elspot.Prices(_plugin.currency)
price=prices_spot.hourly(end_date=datetime.now().date(),areas=[_plugin.Area])
for each,b in price["areas"][_plugin.Area].items():
if each == "values":
for each in price["areas"][_plugin.Area]["values"]:
if hour == CurrentHour:
return each["value"]
break
hour += 1
def UpdateDevice(PID, sValue, Name):
Design="a"
if PID == 0:
ID = 1
if PID == 1:
ID = 2
if PID == 2:
ID = 3
if PID == 3:
ID = 4
if PID == 4:
ID = 5
if PID == 5:
ID = 6
if PID == 6:
ID = 7
if PID == 7:
ID = 8
if PID == 8:
ID = 9
if PID == 9:
ID = 10
if PID == 10:
ID = 11
if PID == 11:
ID = 12
if PID == 12:
ID = 13
if PID == 13:
ID = 14
if PID == 14:
ID = 15
if PID == 15:
ID = 16
if PID == 16:
ID = 17
if PID == 17:
ID = 18
if PID == 18:
ID = 19
if PID == 19:
ID = 20
if PID == 20:
ID = 21
if PID == 21:
ID = 22
if PID == 22:
ID = 23
if PID == 23:
ID = 24
if PID == 24:
ID = 25
if PID == 25:
ID = 26
if PID == 26:
ID = 27
if PID == 27:
ID = 28
if (ID not in Devices):
if sValue == "-32768":
Used = 0
else:
Used = 1
Domoticz.Device(Name=Name, Unit=ID, TypeName="Custom", Options={"Custom": "0;"+_plugin.Unit}, Used=1, Image=(_plugin.ImageID), Description="ParameterID="+str(PID)).Create()
if (ID in Devices):
if Devices[ID].sValue != sValue:
if _plugin.Divide == "Yes":
Devices[ID].Update(0, str(round(sValue/10.0,1)))
else:
Devices[ID].Update(0, str(round(sValue/1000,2)))
def CheckInternet():
WriteDebug("Entered CheckInternet")
try:
WriteDebug("Ping")
requests.get(url='https://8.8.8.8', timeout=2)
WriteDebug("Internet is OK")
return True
except:
WriteDebug("Internet is not available")
return False
def WriteDebug(text):
if Parameters["Mode6"] == "Yes":
timenow = (datetime.now())
logger.info(str(timenow)+" "+text)
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()
# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return