-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathplugin.py
544 lines (425 loc) · 27.7 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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# SolarEdge ModbusTCP
#
# Source: https://github.com/addiejanssen/domoticz-solaredge-modbustcp-plugin
# Author: Addie Janssen (https://addiejanssen.com)
# License: MIT
#
"""
<plugin key="SolarEdge_ModbusTCP" name="SolarEdge ModbusTCP" author="Addie Janssen" version="1.1.1" externallink="https://github.com/addiejanssen/domoticz-solaredge-modbustcp-plugin">
<params>
<param field="Address" label="Inverter IP Address" width="150px" required="true" />
<param field="Port" label="Inverter Port Number" width="100px" required="true" default="502" />
<param field="Mode3" label="Inverter Modbus device address" width="100px" required="true" default="1" />
<param field="Mode1" label="Add missing devices" width="100px" required="true" default="Yes" >
<options>
<option label="Yes" value="Yes" default="true" />
<option label="No" value="No" />
</options>
</param>
<param field="Mode2" label="Interval" width="100px" required="true" default="5" >
<options>
<option label="1 second" value="1" />
<option label="2 seconds" value="2" />
<option label="3 seconds" value="3" />
<option label="4 seconds" value="4" />
<option label="5 seconds" value="5" default="true" />
<option label="10 seconds" value="10" />
<option label="20 seconds" value="20" />
<option label="30 seconds" value="30" />
<option label="60 seconds" value="60" />
</options>
</param>
<param field="Mode4" label="Auto Avg/Max math" width="100px">
<options>
<option label="Enabled" value="math_enabled" default="true" />
<option label="Disabled" value="math_disabled"/>
</options>
</param>
<param field="Mode5" label="Log level" width="100px">
<options>
<option label="Normal" value="Normal" default="true" />
<option label="Extra" value="Extra"/>
<option label="Debug" value="Debug"/>
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import solaredge_modbus
import json
from datetime import datetime, timedelta
from enum import IntEnum, unique, auto
from pymodbus.exceptions import ConnectionException
#
# Domoticz shows graphs with intervals of 5 minutes.
# When collecting information from the inverter more frequently than that, then it makes no sense to only show the last value.
#
# The Average class can be used to calculate the average value based on a sliding window of samples.
# The number of samples stored depends on the interval used to collect the value from the inverter itself.
#
class Average:
def __init__(self):
self.samples = []
self.max_samples = 30
def set_max_samples(self, max):
self.max_samples = max
if self.max_samples < 1:
self.max_samples = 1
def update(self, new_value, scale = 0):
self.samples.append(new_value * (10 ** scale))
while (len(self.samples) > self.max_samples):
del self.samples[0]
Domoticz.Debug("Average: {} - {} values".format(self.get(), len(self.samples)))
def get(self):
return sum(self.samples) / len(self.samples)
#
# Domoticz shows graphs with intervals of 5 minutes.
# When collecting information from the inverter more frequently than that, then it makes no sense to only show the last value.
#
# The Maximum class can be used to calculate the highest value based on a sliding window of samples.
# The number of samples stored depends on the interval used to collect the value from the inverter itself.
#
class Maximum:
def __init__(self):
self.samples = []
self.max_samples = 30
def set_max_samples(self, max):
self.max_samples = max
if self.max_samples < 1:
self.max_samples = 1
def update(self, new_value, scale = 0):
self.samples.append(new_value * (10 ** scale))
while (len(self.samples) > self.max_samples):
del self.samples[0]
Domoticz.Debug("Maximum: {} - {} values".format(self.get(), len(self.samples)))
def get(self):
return max(self.samples)
#
# The Unit class lists all possible pieces of information that can be retrieved from the inverter.
#
# Not all inverters will support all these options.
# The class is used to generate a unique id for each device in Domoticz.
#
@unique
class Unit(IntEnum):
STATUS = 1
VENDOR_STATUS = 2
CURRENT = 3
L1_CURRENT = 4
L2_CURRENT = 5
L3_CURRENT = 6
L1_VOLTAGE = 7
L2_VOLTAGE = 8
L3_VOLTAGE = 9
L1N_VOLTAGE = 10
L2N_VOLTAGE = 11
L3N_VOLTAGE = 12
POWER_AC = 13
FREQUENCY = 14
POWER_APPARENT = 15
POWER_REACTIVE = 16
POWER_FACTOR = 17
ENERGY_TOTAL = 18
CURRENT_DC = 19
VOLTAGE_DC = 20
POWER_DC = 21
TEMPERATURE = 22
#
# The plugin is using a few tables to setup Domoticz and to process the feedback from the inverter.
# The Column class is used to easily identify the columns in those tables.
#
@unique
class Column(IntEnum):
ID = 0
NAME = 1
TYPE = 2
SUBTYPE = 3
SWITCHTYPE = 4
OPTIONS = 5
MODBUSNAME = 6
MODBUSSCALE = 7
FORMAT = 8
PREPEND = 9
LOOKUP = 10
MATH = 11
#
# This table represents a single phase inverter.
#
SINGLE_PHASE_INVERTER = [
# ID, NAME, TYPE, SUBTYPE, SWITCHTYPE, OPTIONS, MODBUSNAME, MODBUSSCALE, FORMAT, PREPEND, LOOKUP, MATH
[Unit.STATUS, "Status", 0xF3, 0x13, 0x00, {}, "status", None, "{}", None, solaredge_modbus.INVERTER_STATUS_MAP, None ],
[Unit.VENDOR_STATUS, "Vendor Status", 0xF3, 0x13, 0x00, {}, "vendor_status", None, "{}", None, None, None ],
[Unit.CURRENT, "Current", 0xF3, 0x17, 0x00, {}, "current", "current_scale", "{:.2f}", None, None, Average() ],
[Unit.L1_CURRENT, "L1 Current", 0xF3, 0x17, 0x00, {}, "l1_current", "current_scale", "{:.2f}", None, None, Average() ],
[Unit.L1_VOLTAGE, "L1 Voltage", 0xF3, 0x08, 0x00, {}, "l1_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.L1N_VOLTAGE, "L1-N Voltage", 0xF3, 0x08, 0x00, {}, "l1n_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_AC, "Power", 0xF8, 0x01, 0x00, {}, "power_ac", "power_ac_scale", "{:.2f}", None, None, Average() ],
[Unit.FREQUENCY, "Frequency", 0xF3, 0x1F, 0x00, { "Custom": "1;Hz" }, "frequency", "frequency_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_APPARENT, "Power (Apparent)", 0xF3, 0x1F, 0x00, { "Custom": "1;VA" }, "power_apparent", "power_apparent_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_REACTIVE, "Power (Reactive)", 0xF3, 0x1F, 0x00, { "Custom": "1;VAr" }, "power_reactive", "power_reactive_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_FACTOR, "Power Factor", 0xF3, 0x06, 0x00, {}, "power_factor", "power_factor_scale", "{:.2f}", None, None, Average() ],
[Unit.ENERGY_TOTAL, "Total Energy", 0xF3, 0x1D, 0x04, {}, "energy_total", "energy_total_scale", "{};{}", Unit.POWER_AC, None, None ],
[Unit.CURRENT_DC, "DC Current", 0xF3, 0x17, 0x00, {}, "current_dc", "current_dc_scale", "{:.2f}", None, None, Average() ],
[Unit.VOLTAGE_DC, "DC Voltage", 0xF3, 0x08, 0x00, {}, "voltage_dc", "voltage_dc_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_DC, "DC Power", 0xF8, 0x01, 0x00, {}, "power_dc", "power_dc_scale", "{:.2f}", None, None, Average() ],
[Unit.TEMPERATURE, "Temperature", 0xF3, 0x05, 0x00, {}, "temperature", "temperature_scale", "{:.2f}", None, None, Maximum() ]
]
#
# This table represents a three phase inverter.
#
THREE_PHASE_INVERTER = [
# ID, NAME, TYPE, SUBTYPE, SWITCHTYPE, OPTIONS, MODBUSNAME, MODBUSSCALE, FORMAT, PREPEND, LOOKUP, MATH
[Unit.STATUS, "Status", 0xF3, 0x13, 0x00, {}, "status", None, "{}", None, solaredge_modbus.INVERTER_STATUS_MAP, None ],
[Unit.VENDOR_STATUS, "Vendor Status", 0xF3, 0x13, 0x00, {}, "vendor_status", None, "{}", None, None, None ],
[Unit.CURRENT, "Current", 0xF3, 0x17, 0x00, {}, "current", "current_scale", "{:.2f}", None, None, Average() ],
[Unit.L1_CURRENT, "L1 Current", 0xF3, 0x17, 0x00, {}, "l1_current", "current_scale", "{:.2f}", None, None, Average() ],
[Unit.L2_CURRENT, "L2 Current", 0xF3, 0x17, 0x00, {}, "l2_current", "current_scale", "{:.2f}", None, None, Average() ],
[Unit.L3_CURRENT, "L3 Current", 0xF3, 0x17, 0x00, {}, "l3_current", "current_scale", "{:.2f}", None, None, Average() ],
[Unit.L1_VOLTAGE, "L1 Voltage", 0xF3, 0x08, 0x00, {}, "l1_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.L2_VOLTAGE, "L2 Voltage", 0xF3, 0x08, 0x00, {}, "l2_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.L3_VOLTAGE, "L3 Voltage", 0xF3, 0x08, 0x00, {}, "l3_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.L1N_VOLTAGE, "L1-N Voltage", 0xF3, 0x08, 0x00, {}, "l1n_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.L2N_VOLTAGE, "L2-N Voltage", 0xF3, 0x08, 0x00, {}, "l2n_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.L3N_VOLTAGE, "L3-N Voltage", 0xF3, 0x08, 0x00, {}, "l3n_voltage", "voltage_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_AC, "Power", 0xF8, 0x01, 0x00, {}, "power_ac", "power_ac_scale", "{:.2f}", None, None, Average() ],
[Unit.FREQUENCY, "Frequency", 0xF3, 0x1F, 0x00, { "Custom": "1;Hz" }, "frequency", "frequency_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_APPARENT, "Power (Apparent)", 0xF3, 0x1F, 0x00, { "Custom": "1;VA" }, "power_apparent", "power_apparent_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_REACTIVE, "Power (Reactive)", 0xF3, 0x1F, 0x00, { "Custom": "1;VAr" }, "power_reactive", "power_reactive_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_FACTOR, "Power Factor", 0xF3, 0x06, 0x00, {}, "power_factor", "power_factor_scale", "{:.2f}", None, None, Average() ],
[Unit.ENERGY_TOTAL, "Total Energy", 0xF3, 0x1D, 0x04, {}, "energy_total", "energy_total_scale", "{};{}", Unit.POWER_AC, None, None ],
[Unit.CURRENT_DC, "DC Current", 0xF3, 0x17, 0x00, {}, "current_dc", "current_dc_scale", "{:.2f}", None, None, Average() ],
[Unit.VOLTAGE_DC, "DC Voltage", 0xF3, 0x08, 0x00, {}, "voltage_dc", "voltage_dc_scale", "{:.2f}", None, None, Average() ],
[Unit.POWER_DC, "DC Power", 0xF8, 0x01, 0x00, {}, "power_dc", "power_dc_scale", "{:.2f}", None, None, Average() ],
[Unit.TEMPERATURE, "Temperature", 0xF3, 0x05, 0x00, {}, "temperature", "temperature_scale", "{:.2f}", None, None, Maximum() ]
]
#
# The BasePlugin is the actual Domoticz plugin.
# This is where the fun starts :-)
#
class BasePlugin:
def __init__(self):
# The _LOOKUP_TABLE will point to one of the tables above, depending on the type of inverter.
self._LOOKUP_TABLE = None
# This is the solaredge_modbus Inverter object that will be used to communicate with the inverter.
self.inverter = None
# Default heartbeat is 10 seconds; therefore 30 samples in 5 minutes.
self.max_samples = 30
# Whether the plugin should add missing devices.
# If set to True, a deleted device will be added on the next restart of Domoticz.
self.add_devices = False
# When there is an issue contacting the inverter, the plugin will retry after a certain retry delay.
# The actual time after which the plugin will try again is stored in the retry after variable.
# According to the documenation, the inverter may need up to 2 minutes to "reset".
self.retrydelay = timedelta(minutes = 2)
self.retryafter = datetime.now() - timedelta(seconds = 1)
#
# onStart is called by Domoticz to start the processing of the plugin.
#
def onStart(self):
self.add_devices = bool(Parameters["Mode1"])
# Domoticz will generate graphs showing an interval of 5 minutes.
# Calculate the number of samples to store over a period of 5 minutes.
self.max_samples = 300 / int(Parameters["Mode2"])
# Now set the interval at which the information is collected accordingly.
Domoticz.Heartbeat(int(Parameters["Mode2"]))
if Parameters["Mode5"] == "Debug":
Domoticz.Debugging(1)
else:
Domoticz.Debugging(0)
Domoticz.Debug(
"onStart Address: {} Port: {} Device Address: {}".format(
Parameters["Address"],
Parameters["Port"],
Parameters["Mode3"]
)
)
self.inverter = solaredge_modbus.Inverter(
host=Parameters["Address"],
port=Parameters["Port"],
timeout=5,
unit=int(Parameters["Mode3"]) if Parameters["Mode3"] else 1
)
# Lets get in touch with the inverter.
self.contactInverter()
#
# OnHeartbeat is called by Domoticz at a specific interval as set in onStart()
#
def onHeartbeat(self):
Domoticz.Debug("onHeartbeat")
# We need to make sure that we have a table to work with.
# This will be set by contactInverter and will be None till it is clear
# that the inverter responds and that a matching table is available.
if self._LOOKUP_TABLE:
inverter_values = None
try:
inverter_values = self.inverter.read_all()
except ConnectionException:
inverter_values = None
Domoticz.Debug("ConnectionException")
else:
if inverter_values:
if "Mode5" in Parameters and (Parameters["Mode5"] == "Extra" or Parameters["Mode5"] == "Debug"):
to_log = inverter_values
if "c_serialnumber" in to_log:
to_log.pop("c_serialnumber")
Domoticz.Log("inverter values: {}".format(json.dumps(to_log, indent=4, sort_keys=False)))
# Just for cosmetics in the log
updated = 0
device_count = 0
# Now process each unit in the table.
for unit in self._LOOKUP_TABLE:
Domoticz.Debug(str(unit))
# Skip a unit when the matching device got deleted.
if unit[Column.ID] in Devices:
Domoticz.Debug("-> found in Devices")
# For certain units the table has a lookup table to replace the value with something else.
if unit[Column.LOOKUP]:
Domoticz.Debug("-> looking up...")
lookup_table = unit[Column.LOOKUP]
to_lookup = int(inverter_values[unit[Column.MODBUSNAME]])
if to_lookup >= 0 and to_lookup < len(lookup_table):
value = lookup_table[to_lookup]
else:
value = "Key not found in lookup table: {}".format(to_lookup)
# When a math object is setup for the unit, update the samples in it and get the calculated value.
elif unit[Column.MATH] and Parameters["Mode4"] == "math_enabled":
Domoticz.Debug("-> calculating...")
m = unit[Column.MATH]
if unit[Column.MODBUSSCALE]:
m.update(inverter_values[unit[Column.MODBUSNAME]], inverter_values[unit[Column.MODBUSSCALE]])
else:
m.update(inverter_values[unit[Column.MODBUSNAME]])
value = m.get()
# When there is no math object then just store the latest value.
# Some values from the inverter need to be scaled before they can be stored.
elif unit[Column.MODBUSSCALE]:
Domoticz.Debug("-> scaling...")
# we need to do some calculation here
value = inverter_values[unit[Column.MODBUSNAME]] * (10 ** inverter_values[unit[Column.MODBUSSCALE]])
# Some values require no action but storing in Domoticz.
else:
Domoticz.Debug("-> copying...")
value = inverter_values[unit[Column.MODBUSNAME]]
Domoticz.Debug("value = {}".format(value))
# Time to store the value in Domoticz.
# Some devices require multiple values, in which case the plugin will combine those values.
# Currently, there is only a need to prepend one value with another.
if unit[Column.PREPEND]:
Domoticz.Debug("-> has prepend")
prepend = Devices[unit[Column.PREPEND]].sValue
Domoticz.Debug("prepend = {}".format(prepend))
sValue = unit[Column.FORMAT].format(prepend, value)
else:
Domoticz.Debug("-> no prepend")
sValue = unit[Column.FORMAT].format(value)
Domoticz.Debug("sValue = {}".format(sValue))
# Only store the value in Domoticz when it has changed.
# TODO:
# We should not store certain values when the inverter is sleeping.
# That results in a strange graph; it would be better just to skip it then.
if sValue != Devices[unit[Column.ID]].sValue:
Devices[unit[Column.ID]].Update(nValue=0, sValue=str(sValue), TimedOut=0)
updated += 1
device_count += 1
else:
Domoticz.Debug("-> NOT found in Devices")
Domoticz.Log("Updated {} values out of {}".format(updated, device_count))
else:
Domoticz.Log("Inverter returned no information")
# Try to contact the inverter when the lookup table is not yet initialized.
else:
self.contactInverter()
#
# Contact the inverter and find out what type it is.
# Initialize the lookup table when the type is supported.
#
def contactInverter(self):
# Do not stress the inverter when it did not respond in the previous attempt to contact it.
if self.retryafter <= datetime.now():
# Here we go...
inverter_values = None
try:
inverter_values = self.inverter.read_all()
except ConnectionException:
# There are multiple reasons why this may fail.
# - Perhaps the ip address or port are incorrect.
# - The inverter may not be connected to the networ,
# - The inverter may be turned off.
# - The inverter has a bad hairday....
# Try again in the future.
self.retryafter = datetime.now() + self.retrydelay
inverter_values = None
Domoticz.Log("Connection Exception when trying to contact: {}:{} Device Address: {}".format(Parameters["Address"], Parameters["Port"], Parameters["Mode3"]))
Domoticz.Log("Retrying to communicate with inverter after: {}".format(self.retryafter))
else:
if inverter_values:
Domoticz.Log("Connection established with: {}:{} Device Address: {}".format(Parameters["Address"], Parameters["Port"], Parameters["Mode3"]))
inverter_type = solaredge_modbus.sunspecDID(inverter_values["c_sunspec_did"])
Domoticz.Log("Inverter type: {}".format(inverter_type))
# The plugin currently has 2 supported types.
# This may be updated in the future based on user feedback.
if inverter_type == solaredge_modbus.sunspecDID.SINGLE_PHASE_INVERTER:
self._LOOKUP_TABLE = SINGLE_PHASE_INVERTER
elif inverter_type == solaredge_modbus.sunspecDID.THREE_PHASE_INVERTER:
self._LOOKUP_TABLE = THREE_PHASE_INVERTER
else:
Domoticz.Log("Unsupported inverter type: {}".format(inverter_type))
if self._LOOKUP_TABLE:
# Set the number of samples on all the math objects.
for unit in self._LOOKUP_TABLE:
if unit[Column.MATH] and Parameters["Mode4"] == "math_enabled":
unit[Column.MATH].set_max_samples(self.max_samples)
# We updated some device types over time.
# Let's make sure that we have the correct type setup.
for unit in self._LOOKUP_TABLE:
if unit[Column.ID] in Devices:
device = Devices[unit[Column.ID]]
if (device.Type != unit[Column.TYPE] or
device.SubType != unit[Column.SUBTYPE] or
device.SwitchType != unit[Column.SWITCHTYPE] or
device.Options != unit[Column.OPTIONS]):
Domoticz.Log("Updating device \"{}\"".format(device.Name))
nValue = device.nValue
sValue = device.sValue
device.Update(
Type=unit[Column.TYPE],
Subtype=unit[Column.SUBTYPE],
Switchtype=unit[Column.SWITCHTYPE],
Options=unit[Column.OPTIONS],
nValue=nValue,
sValue=sValue
)
# Add missing devices if needed.
if self.add_devices:
for unit in self._LOOKUP_TABLE:
if unit[Column.ID] not in Devices:
Domoticz.Device(
Unit=unit[Column.ID],
Name=unit[Column.NAME],
Type=unit[Column.TYPE],
Subtype=unit[Column.SUBTYPE],
Switchtype=unit[Column.SWITCHTYPE],
Options=unit[Column.OPTIONS],
Used=1,
).Create()
else:
Domoticz.Log("Connection established with: {}:{} Device Address: {}. BUT... inverter returned no information".format(Parameters["Address"], Parameters["Port"], Parameters["Mode3"]))
Domoticz.Log("Retrying to communicate with inverter after: {}".format(self.retryafter))
else:
Domoticz.Log("Retrying to communicate with inverter after: {}".format(self.retryafter))
#
# Instantiate the plugin and register the supported callbacks.
# Currently that is only onStart() and onHeartbeat()
#
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onHeartbeat():
global _plugin
_plugin.onHeartbeat()