-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPJ-1203A-v2.js
372 lines (315 loc) · 12.8 KB
/
PJ-1203A-v2.js
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
const fz = require('zigbee-herdsman-converters/converters/fromZigbee');
const tz = require('zigbee-herdsman-converters/converters/toZigbee');
const exposes = require('zigbee-herdsman-converters/lib/exposes');
const tuya = require('zigbee-herdsman-converters/lib/tuya');
const utils = require('zigbee-herdsman-converters/lib/utils');
const e = exposes.presets;
const ea = exposes.access;
const {Buffer} = require('buffer');
//
// WARNING !!!! only tested on _TZE204_81yrt3lo
//
// The PJ1203A is sending quick sequence of messages containing a single datapoint.
// A sequence occurs every `update_frequency` seconds (10s by default)
//
// A typical sequence is composed two identifcal groups for channel a and b.
//
// 102 energy_flow_a
// 112 voltage
// 113 current_a
// 101 power_a
// 110 power_factor_a
// 111 ac_frequency
// 115 power_ab
// ---
// 104 energy_flow_b
// 112 voltage
// 114 current_b
// 105 power_b
// 121 power_factor_b
// 111 ac_frequency
// 115 power_ab
//
// It should be noted that when no current is detected on channel x then
// energy_flow_x is not emited and current_x==0, power_x==0 and power_factor_x==100.
// Simply speaking, energy_flow_x is optional but the case can easily be detected
// by checking if current_x or power_x is 0.
//
// The other datapoints are emitted every few minutes.
//
// Store our internal state in meta.device._priv
function PJ1203A_getPrivateState(meta) {
if ( ! ('_priv' in meta.device) ) {
meta.device._priv = {
'sign_a': null,
'sign_b': null,
'power_a': null,
'power_b': null,
'current_a': null,
'current_b': null,
'power_factor_a': null,
'power_factor_b': null,
'last_seq': -99999,
'updated_a':0,
'updated_b':0,
// Also save the last published values of power_a and power_b
// to recompute power_ab on the fly.
'pub_power_a': null,
'pub_power_b': null,
} ;
}
return meta.device._priv ;
}
// The _TZE204_81yrt3lo increments its payload 'seq' value by steps of 256
// This is hopefully the same for all variants.
const PJ1203A_SEQ_INCREMENT = 256 ;
const PJ1203A_options = {
energy_flow_qwirk: () => exposes.binary('energy_flow_qwirk', ea.SET, true, false )
.withDescription(
' If true then use the next energy flow datapoint before publishing grouped data.'+
' The default is false.'
),
};
function PJ1203A_get_energy_flow_qwirk(options) {
return options?.energy_flow_qwirk || false ;
}
// Increment the updated_a or updated_b attribute
function PJ1203A_next_update_id(result,priv,x) {
let updated_x = 'updated_'+x ;
result[updated_x] = priv[updated_x] = (priv[updated_x]+1) % 1000 ;
}
// Recompute power_ab when power_a or power_b is published.
function PJ1203A_recompute_power_ab(result,priv,options) {
let modified = false ;
if ( 'power_a' in result ) {
priv.pub_power_a = result.power_a ;
modified = true ;
}
if ( 'power_b' in result ) {
priv.pub_power_b = result.power_b ;
modified = true ;
}
if (modified) {
if ( priv.pub_power_a!==null &&
priv.pub_power_b!==null ) {
// Note: We need to "cancel" and reapply the scaling by 10
// because of annoying floating-point rounding errors.
// For example:
// 79.8 - 37.1 --> 42.699999999999996
result['power_ab'] = Math.round(10*priv.pub_power_a + 10*priv.pub_power_b) / 10 ;
}
}
}
function PJ1203A_flush_all(result,x,priv,options) {
let sign = priv['sign_'+x] ;
let power = priv['power_'+x] ;
let current = priv['current_'+x] ;
let power_factor = priv['power_factor_'+x] ;
// Make sure that we use them only once. No obsolete data!!!!
priv['sign_'+x] = priv['power_'+x] = priv['current_'+x] = priv['power_factor_'+x] = null ;
// And only publish after receiving a complete set
if ( sign!==null && power!==null && current!==null && power_factor!==null ) {
result['power_'+x] = sign * power ;
result['current_'+x] = current;
result['power_factor_'+x] = power_factor;
PJ1203A_recompute_power_ab(result,priv,options);
PJ1203A_next_update_id(result, priv, x);
return ;
}
return ;
}
// When the device does not detect any flow, it stops sending
// the energy_flow datapoint (102 and 104) and always set
// current_x=0, power_x=0 and power_factor_x=100.
//
// So if we see a datapoint with current==0 or power==0
// then we can safely assume that we are in that zero energy state.
//
function PJ1203A_flush_zero(result,x,priv,options) {
priv['sign_'+x] = +1 ;
priv['power_'+x] = 0 ;
priv['current_'+x] = 0 ;
priv['power_factor_'+x] = 100 ;
PJ1203A_flush_all(result,x,priv,options);
}
const PJ1203A_valueConverters = {
energy_flow: (x) => {
return {
from: (v, meta, options) => {
let priv = PJ1203A_getPrivateState(meta) ;
let result = {} ;
priv['sign_'+x] = (v==1) ? -1 : +1 ;
let energy_flow_qwirk = PJ1203A_get_energy_flow_qwirk(options)
if (energy_flow_qwirk) {
PJ1203A_flush_all(result, x, priv, options);
}
return result;
}
};
}, // end of energy_flow:
power: (x) => {
return {
from: (v, meta, options) => {
let priv = PJ1203A_getPrivateState(meta) ;
let result = {} ;
let power_x = v / 10.0 ;
priv['power_'+x] = power_x;
if (v==0) {
PJ1203A_flush_zero(result, x, priv, options);
return result;
}
return result;
}
};
}, // end of power:
current: (x) => {
return {
from: (v, meta, options) => {
let priv = PJ1203A_getPrivateState(meta) ;
let result = {} ;
let current_x = v / 1000.0 ;
priv['current_'+x] = current_x ;
if (v==0) {
PJ1203A_flush_zero(result, x, priv, options);
return result;
}
return result;
}
};
}, // end of current:
power_factor: (x) => {
return {
from: (v, meta, options) => {
let priv = PJ1203A_getPrivateState(meta) ;
let result = {} ;
priv['power_factor_'+x] = v ;
let energy_flow_qwirk = PJ1203A_get_energy_flow_qwirk(options)
if (!energy_flow_qwirk) {
PJ1203A_flush_all(result, x, priv, options);
}
return result;
}
};
}, // end of power_factor:
// We currently discard the power_ab datapoint.
// It is always recomputed on the fly to match
// the published values or power_a and power_b
power_ab: () => {
return {
from: (v, meta, options) => {
return {} ;
}
};
}, // end of power_ab:
};
//
// A customized version of fz.ignore_tuya_set_time
// that also increases our private 'next_seq' field.
//
// This is needed to prevent 'commandMcuSyncTime' from
// messing up with our detection of missing datapoints (see
// below in PJ1203A_fz_datapoints).
//
const PJ1203A_ignore_tuya_set_time = {
cluster: 'manuSpecificTuya',
type: ['commandMcuSyncTime'],
convert: (model, msg, publish, options, meta) =>
{
// There is no 'seq' field in the msg payload of 'commandMcuSyncTime'
// but the device is increasing its counter.
let priv = PJ1203A_getPrivateState(meta) ;
priv.last_seq += PJ1203A_SEQ_INCREMENT ;
}
};
//
// This is basically tuya.fz.datapoints extended to detect missing
// or reordered messages.
//
const PJ1203A_fz_datapoints = {
...tuya.fz.datapoints,
convert: (model, msg, publish, options, meta) => {
let result = {}
// Uncomment the next line to test the behavior
// when random messages are lost
// if ( Math.random() < 0.05 ) return ;
let priv = PJ1203A_getPrivateState(meta) ;
// Detect missing or re-ordered messages but allow duplicate messages.
let expected_seq = (priv.last_seq+PJ1203A_SEQ_INCREMENT) & 0xFFFF ;
if ( ( msg.data.seq != expected_seq ) && ( msg.data.seq != priv.last_seq ) ) {
meta.logger.debug(`[PJ1203A] Missing or re-ordered message detected: Got seq=${msg.data.seq}, expected ${priv.next_seq}`);
// Clear all pending attributes
priv.sign_a = null;
priv.sign_b = null;
priv.power_a = null;
priv.power_b = null;
priv.current_a = null;
priv.current_b = null;
priv.power_factor_a = null;
priv.power_factor_b = null;
}
priv.last_seq = msg.data.seq;
// Uncomment to display device data in the state (for debug)
// result['device'] = meta.device ;
// And finally, process the dp with tuya.fz.datapoints
Object.assign( result, tuya.fz.datapoints.convert(model, msg, publish, options, meta) ) ;
// WARNING: MUST BE REMOVED IN FINAL RELEASE
// meta.logger.debug(`[PJ1203A] priv = ${JSON.stringify(priv)}`);
// meta.logger.debug(`[PJ1203A] result = ${JSON.stringify(result)}`);
return result;
}
};
const PJ1203A_tz_datapoints = {
...tuya.tz.datapoints,
key: [ 'update_frequency' ]
};
const definition = {
fingerprint: tuya.fingerprint('TS0601',
[
'_TZE204_81yrt3lo',
// '_TZE200_rks0sgb7' // not tested
]),
model: 'PJ-1203A',
vendor: 'TuYa',
description: 'Bidirectional energy meter with 80A current clamp (CUSTOMIZED)',
fromZigbee: [PJ1203A_fz_datapoints, PJ1203A_ignore_tuya_set_time],
toZigbee: [PJ1203A_tz_datapoints],
onEvent: tuya.onEventSetTime,
configure: tuya.configureMagicPacket,
options: [
PJ1203A_options.energy_flow_qwirk(),
],
exposes: [
tuya.exposes.powerWithPhase('a'), tuya.exposes.powerWithPhase('b'), tuya.exposes.powerWithPhase('ab'),
tuya.exposes.currentWithPhase('a'), tuya.exposes.currentWithPhase('b'),
tuya.exposes.powerFactorWithPhase('a'), tuya.exposes.powerFactorWithPhase('b'),
// tuya.exposes.energyFlowWithPhase('a'), tuya.exposes.energyFlowWithPhase('b'),
tuya.exposes.energyWithPhase('a'), tuya.exposes.energyWithPhase('b'),
tuya.exposes.energyProducedWithPhase('a'), tuya.exposes.energyProducedWithPhase('b'),
e.ac_frequency(),
e.voltage(),
e.numeric('updated_a', ea.STATE).withDescription('Modified after each update of channel a'),
e.numeric('updated_b', ea.STATE).withDescription('Modified after each update of channel b'),
e.numeric('update_frequency',ea.STATE_SET).withUnit('s').withDescription('Update frequency').withValueMin(3).withValueMax(60),
],
meta: {
tuyaDatapoints: [
[111, 'ac_frequency', tuya.valueConverter.divideBy100],
[112, 'voltage', tuya.valueConverter.divideBy10],
[101, null, PJ1203A_valueConverters.power('a')], // power_a
[105, null, PJ1203A_valueConverters.power('b')], // power_b
[113, null, PJ1203A_valueConverters.current('a')], // current_a
[114, null, PJ1203A_valueConverters.current('b')], // current_b
[110, null, PJ1203A_valueConverters.power_factor('a')], // power_factor_a
[121, null, PJ1203A_valueConverters.power_factor('b')], // power_factor_b
[102, null, PJ1203A_valueConverters.energy_flow('a')], // the sign of power_a
[104, null, PJ1203A_valueConverters.energy_flow('b')], // the sign of power_b
[115, null, PJ1203A_valueConverters.power_ab()],
[106, 'energy_a', tuya.valueConverter.divideBy100],
[108, 'energy_b', tuya.valueConverter.divideBy100],
[107, 'energy_produced_a', tuya.valueConverter.divideBy100],
[109, 'energy_produced_b', tuya.valueConverter.divideBy100],
[129, 'update_frequency', tuya.valueConverter.raw],
],
},
};
module.exports = definition;