-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.py
370 lines (307 loc) · 14.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
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
#!/usr/bin/env python
"""
SDM120c Energy Meter
Author: Xavier Beaudouin
Requirements:
1. modbus over TCP adapter like PW21
2. pymodbus AND pymodbusTCP
"""
"""
<plugin key="SDM120c_ModbusTCP" name="SDM120c ModbusTCP" author="Xavier Beaudouin" version="0.0.1" externallink="https://github.com/xbeaudouin/domoticz-sdm120c-modbus-tcp">
<params>
<param field="Address" label="IP Address" width="150px" required="true" />
<param field="Port" label="Port Number" width="100px" required="true" default="502" />
<param field="Mode3" label="Modbus address" width="100px" required="true" default="1" />
<param field="Mode5" label="Collect extended data" width="100px">
<options>
<option label="Enabled" value="Moredata"/>
<option label="Disabled" value="Regular" default="true" />
</options>
</param>
<param field="Mode6" label="Debug" width="100px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import Domoticz
import sys
sys.path.append('/usr/local/lib/python3.4/dist-packages')
sys.path.append('/usr/local/lib/python3.5/dist-packages')
sys.path.append('/usr/local/lib/python3.6/dist-packages')
sys.path.append('/usr/local/lib/python3.7/dist-packages')
sys.path.append('/usr/local/lib/python3.8/dist-packages')
sys.path.append('/usr/local/lib/python3.9/dist-packages')
sys.path.append('/usr/local/lib/python3.10/dist-packages')
import pymodbus
from pyModbusTCP.client import ModbusClient
from pymodbus.constants import Endian
from pymodbus.payload import BinaryPayloadDecoder
#
# 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)
def strget(self):
return str(sum(self.samples) / len(self.samples))
# Plugin itself
class BasePlugin:
def __init__(self):
# Voltage for last 5 minutes
self.voltage=Average()
# Current for last 5 minutes
self.current=Average()
# Apparent Power for last 5 minutes
self.apparent_power=Average()
# Active power for last 5 minutes
self.active_power=Average()
# Reactive power for last 5 minutes
self.reactive_power=Average()
# Power factor for last 5 minutes
self.power_factor=Average()
# Phase Angle for last 5 minutes
self.phase_angle=Average()
# Frequency for last 5 minutes
self.frequency=Average()
# Used only when Moredata is set...
# Total demand power for last 5 minutes
self.total_demand_power=Average()
# Import demand power for last 5 minutes
self.import_demand_power=Average()
# Export demand power for last 5 minutes
self.export_demand_power=Average()
# Total Demand Current for last 5 minutes
self.total_demand_current=Average()
return
def onStart(self):
Domoticz.Log("SDM120c Energy Meter TCP loaded!")
# Check dependancies
try:
if (float(Parameters["DomoticzVersion"][:6]) < float("2020.2")): Domoticz.Error("WARNING: Domoticz version is outdated or not supported. Please update!")
if (float(sys.version[:1]) < 3): Domoticz.Error("WARNING: Python3 should be used !")
except:
Domoticz.Error("Warning ! Dependancies could not be checked !")
# Debug
if Parameters["Mode6"] == "Debug":
Domoticz.Debugging(1)
else:
Domoticz.Debugging(0)
self.IPAddress = Parameters["Address"]
self.IPPort = Parameters["Port"]
self.MBAddr = int(Parameters["Mode3"])
Domoticz.Debug("Query IP " + self.IPAddress + ":" + str(self.IPPort) +" on device : "+str(self.MBAddr))
# Create the devices if they does not exists
if 1 not in Devices:
Domoticz.Device(Name="Voltage", Unit=1, TypeName="Voltage", Used=0).Create()
if 2 not in Devices:
Domoticz.Device(Name="Current", Unit=2, TypeName="Current (Single)", Used=0).Create()
if 3 not in Devices:
Options = { "Custom": "1;W" }
Domoticz.Device(Name="Active Power", Unit=3, TypeName="Custom", Used=0, Options=Options).Create()
if 4 not in Devices:
Options = { "Custom": "1;VA" }
Domoticz.Device(Name="Apparent Power", Unit=4, TypeName="Custom", Used=0, Options=Options).Create()
if 5 not in Devices:
Options = { "Custom": "1;VAr" }
Domoticz.Device(Name="Reactive Power", Unit=5, TypeName="Custom", Used=0, Options=Options).Create()
if 6 not in Devices:
Options = { "Custom": "1;PF" }
Domoticz.Device(Name="Power Factor", Unit=6, TypeName="Custom", Used=0, Options=Options).Create()
if Parameters["Mode5"] == "Moredata":
if 7 not in Devices:
Options = { "Custom": "1;Deg" }
Domoticz.Device(Name="Phase Angle", Unit=7, TypeName="Custom", Used=0, Options=Options).Create()
if 8 not in Devices:
Options = { "Custom": "1;Hz" }
Domoticz.Device(Name="Frequency", Unit=8, TypeName="Custom", Used=0, Options=Options).Create()
if 9 not in Devices:
Domoticz.Device(Name="Import Energy", Unit=9, Type=243, Subtype=29, Used=0).Create()
if 10 not in Devices:
Domoticz.Device(Name="Export Energy", Unit=10, Type=243, Subtype=29, Used=0).Create()
# 11 will be not used Import Energy (Reactive) / kVArh
# 12 will be not used Export Energy (Reactive) / kVArh
if Parameters["Mode5"] == "Moredata":
if 13 not in Devices:
Options = { "Custom": "1;W" }
Domoticz.Device(Name="Total Demand Power", Unit=13, TypeName="Custom", Used=0, Options=Options).Create()
if 14 not in Devices:
Options = { "Custom": "1;W" }
Domoticz.Device(Name="Maximum Total Demand Power", Unit=14, TypeName="Custom", Used=0, Options=Options).Create()
if 15 not in Devices:
Options = { "Custom": "1;W" }
Domoticz.Device(Name="Import Demand Power", Unit=15, TypeName="Custom", Used=0, Options=Options).Create()
if 16 not in Devices:
Options = { "Custom": "1;W" }
Domoticz.Device(Name="Maximum Import Demand Power", Unit=16, TypeName="Custom", Used=0, Options=Options).Create()
if 17 not in Devices:
Options = { "Custom": "1;W" }
Domoticz.Device(Name="Export Demand Power", Unit=17, TypeName="Custom", Used=0, Options=Options).Create()
if 18 not in Devices:
Options = { "Custom": "1;W" }
Domoticz.Device(Name="Maximum Export Demand Power", Unit=18, TypeName="Custom", Used=0, Options=Options).Create()
if 19 not in Devices:
Domoticz.Device(Name="Total Demand Current", Unit=19, TypeName="Current (Single)", Used=0).Create()
if 20 not in Devices:
Domoticz.Device(Name="Maximum Total Demand Current", Unit=20, TypeName="Current (Single)", Used=0).Create()
if 21 not in Devices:
Domoticz.Device(Name="Total Energy", Unit=21, Type=0xfa, Subtype=0x01, Used=0).Create()
# 22 will not be used Total Energy (Reactive)
return
def onStop(self):
Domoticz.Debugging(0)
def onHeartbeat(self):
Domoticz.Debug(" Interface : IP="+self.IPAddress +", Port="+str(self.IPPort)+" ID="+str(self.MBAddr))
try:
client = ModbusClient(host=self.IPAddress, port=self.IPPort, unit_id=self.MBAddr, auto_open=True, auto_close=True, timeout=2)
except:
Domoticz.Error("Error connecting to TCP/Interface on address : "+self.IPaddress+":"+str(self.IPPort))
# Set value to 0 -> Error on all devices
Devices[1].Update(1, "0")
Devices[2].Update(1, "0")
Devices[3].Update(1, "0")
Devices[4].Update(1, "0")
Devices[5].Update(1, "0")
Devices[6].Update(1, "0")
if Parameters["Mode5"] == "Moredata":
Devices[7].Update(1, "0")
Devices[8].Update(1, "0")
Devices[9].Update(1, "0")
Devices[10].Update(1, "0")
if Parameters["Mode5"] == "Moredata":
Devices[13].Update(1, "0")
Devices[14].Update(1, "0")
Devices[15].Update(1, "0")
Devices[16].Update(1, "0")
Devices[17].Update(1, "0")
Devices[18].Update(1, "0")
Devices[19].Update(1, "0")
Devices[20].Update(1, "0")
Devices[21].Update(1, "0")
# Voltage
self.voltage.update(getmodbus(0x0000, client))
Devices[1].Update(1, self.voltage.strget())
# Current
self.current.update(getmodbus(0x0006, client))
Devices[2].Update(1, self.current.strget())
# Power (Active)
self.active_power.update(getmodbus(0x000c, client))
Devices[3].Update(1, self.active_power.strget())
# Power (Apparent)
self.apparent_power.update(getmodbus(0x0012, client))
Devices[4].Update(1, self.apparent_power.strget())
# Power (Reactive)
self.reactive_power.update(getmodbus(0x0018, client))
Devices[5].Update(1, self.reactive_power.strget())
# Power Factor
self.power_factor.update(getmodbus(0x001e, client))
Devices[6].Update(1, self.power_factor.strget())
if Parameters["Mode5"] == "Moredata":
# Phase Angle
self.phase_angle.update(getmodbus(0x0024, client))
Devices[7].Update(1, self.phase_angle.strget())
# Frequency
self.frequency.update(getmodbus(0x0046, client))
Devices[8].Update(1, self.frequency.strget())
# For Imported/Exported Energy (Active) and the last P1 Counter
power = self.active_power.get()
if power >= 0:
import_power = power
else:
import_power = 0
if power < 0:
export_power = abs(power)
else:
export_power = 0
# Imported Energy (Active)
import_e = str(getmodbus(0x0048, client)*1000)
Devices[9].Update(1, sValue=str(import_power)+";"+import_e)
# Exported Energy (Active)
export_e = str(getmodbus(0x004a, client)*1000)
Devices[10].Update(1, sValue=str(export_power)+";"+export_e)
if Parameters["Mode5"] == "Moredata":
# Total Demand Powier (Active)
self.total_demand_power.update(getmodbus(0x0054, client))
Devices[13].Update(1, self.total_demand_power.strget())
# Maximum Total Demand Power (Active)
Devices[14].Update(1, str(getmodbus(0x0056, client)))
# Import Demand Power (Active)
self.import_demand_power.update(getmodbus(0x0058, client))
Devices[15].Update(1, self.import_demand_power.strget())
# Maximum Import Demand Power (Active)
Devices[16].Update(1, str(getmodbus(0x005a, client)))
# Export Demand Power (Active)
self.import_demand_power.update(getmodbus(0x0058, client))
Devices[17].Update(1, self.import_demand_power.strget())
# Maxium Export Demand Power (Active)
Devices[18].Update(1, str(getmodbus(0x005e, client)))
# Total Demand Current
self.total_demand_current.update(getmodbus(0x0102, client))
Devices[19].Update(1, self.total_demand_current.strget())
# Maxium Total Demand Current
Devices[20].Update(1, str(getmodbus(0x0108, client)))
# P1 Counter with import and export values
Devices[21].Update(1, sValue=import_e+";0;"+export_e+";0;"+str(import_power)+";"+str(export_power))
global _plugin
_plugin = BasePlugin()
def onStart():
global _plugin
_plugin.onStart()
def onStop():
global _plugin
_plugin.onStop()
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
# get Modbus float 32 bits values
def getmodbus(register, client):
value = 0
try:
data = client.read_input_registers(register, 2)
Domoticz.Debug("Data from register "+str(register)+": "+str(data))
#decoder = BinaryPayloadDecoder.fromRegisters(data, byteorder=Endian.Big, wordorder=Endian.Big)
decoder = BinaryPayloadDecoder.fromRegisters(data, byteorder=Endian.BIG, wordorder=Endian.BIG)
value = round(decoder.decode_32bit_float(), 3)
except:
Domoticz.Error("Error getting data from "+str(register) + ", try 1")
try:
data = client.read_input_registers(register, 2)
Domoticz.Debug("Data from register "+str(register)+": "+str(data))
decoder = BinaryPayloadDecoder.fromRegisters(data, byteorder=Endian.BIG, wordorder=Endian.BIG)
value = round(decoder.decode_32bit_float(), 3)
except:
Domoticz.Error("Error getting data from "+str(register) + ", try 2")
return value