|
Bogdan Timofte
authored
2 weeks ago
|
1
|
# Conversie nivel baterie intre `NiMH` si `alkaline`
|
|
|
2
|
|
|
|
3
|
## Scop
|
|
|
4
|
|
|
|
5
|
In acest repository exista o conversie configurabila pentru nivelul bateriei raportat de Zigbee2MQTT, folosita atunci cand dispozitivul are baterii reincarcabile `NiMH` in loc de baterii `alkaline`.
|
|
|
6
|
|
|
|
7
|
Motivatia este simpla: multe dispozitive raporteaza procentul bateriei pe o curba calibrata mai degraba pentru baterii alcaline, iar celulele `NiMH` au o curba de descarcare mai plata. Din acest motiv, procentul brut poate parea prea mic pentru baterii `NiMH`, mai ales in zona de mijloc a descarcarii.
|
|
|
8
|
|
|
|
9
|
Comportamentul este:
|
|
|
10
|
|
|
|
11
|
- daca `batteryType` este `alkaline`, valoarea ramane neschimbata
|
|
|
12
|
- daca `batteryType` este `nimh`, valoarea este remapata printr-o conversie bazata pe curbe aproximative `alkaline -> tensiune -> NiMH`
|
|
|
13
|
- valoarea convertita este folosita atat pentru publicarea capabilitatii `battery`, cat si pentru derivarea lui `battery_low` atunci cand dispozitivul nu furnizeaza explicit acest camp
|
|
|
14
|
|
|
|
15
|
## Configurare
|
|
|
16
|
|
|
|
17
|
Conversia este controlata din UI-ul nodurilor Node-RED prin campul `Battery type`, cu valoarea implicita `alkaline`.
|
|
|
18
|
|
|
|
19
|
Referinte UI:
|
|
|
20
|
|
|
|
21
|
- [zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.html](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.html#L11)
|
|
|
22
|
- [PA-44Z/homebus-adapter/z2m-pa-44z-homebus.html](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/PA-44Z/homebus-adapter/z2m-pa-44z-homebus.html#L11)
|
|
|
23
|
|
|
|
24
|
Valorile disponibile sunt:
|
|
|
25
|
|
|
|
26
|
- `alkaline`
|
|
|
27
|
- `nimh`
|
|
|
28
|
|
|
|
29
|
Pragul pentru `battery_low` este separat si configurabil prin `batteryLowThreshold`, implicit `20`.
|
|
|
30
|
|
|
|
31
|
## Implementare generala
|
|
|
32
|
|
|
|
33
|
Fluxul logic este acelasi in ambele adaptoare:
|
|
|
34
|
|
|
|
35
|
1. se citeste `payload.battery`
|
|
|
36
|
2. valoarea este clamp-uita in intervalul `0..100`
|
|
|
37
|
3. daca tipul bateriei este `nimh`, procentul este convertit mai intai intr-o tensiune echivalenta pe o curba `alkaline`
|
|
|
38
|
4. tensiunea echivalenta este proiectata pe o curba `NiMH`
|
|
|
39
|
5. valoarea rezultata este publicata pe capabilitatea `battery`
|
|
|
40
|
6. `battery_low` se calculeaza din valoarea convertita daca dispozitivul nu trimite deja un camp `battery_low`
|
|
|
41
|
|
|
|
42
|
## Proiecte care folosesc functia
|
|
|
43
|
|
|
|
44
|
### 1. `zg-204zv/homebus-adapter`
|
|
|
45
|
|
|
|
46
|
Implementarea este in:
|
|
|
47
|
|
|
|
48
|
- [zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js#L214)
|
|
|
49
|
|
|
|
50
|
Configurarea tipului de baterie:
|
|
|
51
|
|
|
|
52
|
- [zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js#L34)
|
|
|
53
|
|
|
|
54
|
Unde este folosita functia:
|
|
|
55
|
|
|
|
56
|
- pentru `battery`: [zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js#L146)
|
|
|
57
|
- pentru `battery_low`: [zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.js#L164)
|
|
|
58
|
|
|
|
59
|
Functia:
|
|
|
60
|
|
|
|
61
|
```js
|
|
|
62
|
var ALKALINE_BATTERY_CURVE = [
|
|
|
63
|
{ pct: 100, voltage: 1.60 },
|
|
|
64
|
{ pct: 90, voltage: 1.55 },
|
|
|
65
|
{ pct: 80, voltage: 1.50 },
|
|
|
66
|
{ pct: 70, voltage: 1.46 },
|
|
|
67
|
{ pct: 60, voltage: 1.42 },
|
|
|
68
|
{ pct: 50, voltage: 1.36 },
|
|
|
69
|
{ pct: 40, voltage: 1.30 },
|
|
|
70
|
{ pct: 30, voltage: 1.25 },
|
|
|
71
|
{ pct: 20, voltage: 1.20 },
|
|
|
72
|
{ pct: 10, voltage: 1.10 },
|
|
|
73
|
{ pct: 0, voltage: 0.90 }
|
|
|
74
|
];
|
|
|
75
|
|
|
|
76
|
var NIMH_BATTERY_CURVE = [
|
|
|
77
|
{ pct: 100, voltage: 1.40 },
|
|
|
78
|
{ pct: 95, voltage: 1.33 },
|
|
|
79
|
{ pct: 90, voltage: 1.28 },
|
|
|
80
|
{ pct: 85, voltage: 1.24 },
|
|
|
81
|
{ pct: 80, voltage: 1.20 },
|
|
|
82
|
{ pct: 75, voltage: 1.19 },
|
|
|
83
|
{ pct: 70, voltage: 1.18 },
|
|
|
84
|
{ pct: 60, voltage: 1.17 },
|
|
|
85
|
{ pct: 50, voltage: 1.16 },
|
|
|
86
|
{ pct: 40, voltage: 1.15 },
|
|
|
87
|
{ pct: 30, voltage: 1.13 },
|
|
|
88
|
{ pct: 20, voltage: 1.11 },
|
|
|
89
|
{ pct: 10, voltage: 1.07 },
|
|
|
90
|
{ pct: 0, voltage: 1.00 }
|
|
|
91
|
];
|
|
|
92
|
|
|
|
93
|
function translateBatteryLevel(rawValue) {
|
|
|
94
|
if (rawValue === undefined) return undefined;
|
|
|
95
|
var raw = clamp(Math.round(Number(rawValue)), 0, 100);
|
|
|
96
|
if (node.batteryType !== "nimh") return raw;
|
|
|
97
|
|
|
|
98
|
var estimatedVoltage = interpolateCurve(ALKALINE_BATTERY_CURVE, "pct", "voltage", raw);
|
|
|
99
|
var nimhPct = interpolateCurve(NIMH_BATTERY_CURVE, "voltage", "pct", estimatedVoltage);
|
|
|
100
|
return clamp(Math.round(nimhPct), 0, 100);
|
|
|
101
|
}
|
|
|
102
|
```
|
|
|
103
|
|
|
|
104
|
Observatii:
|
|
|
105
|
|
|
|
106
|
- remaparea nu mai este o euristica pe praguri, ci o interpolare liniara intre doua curbe aproximative
|
|
|
107
|
- procentul `alkaline` este reinterpretat ca tensiune echivalenta pe celula
|
|
|
108
|
- tensiunea rezultata este apoi tradusa in procent `NiMH`
|
|
|
109
|
- valoarea de referinta `1.20V` pe celula este tratata ca o zona inca sanatoasa pentru `NiMH`, nu ca baterie aproape goala
|
|
|
110
|
|
|
|
111
|
Exemple:
|
|
|
112
|
|
|
|
113
|
| Brut | Convertit `nimh` |
|
|
|
114
|
| --- | --- |
|
|
|
115
|
| 5 | 0 |
|
|
|
116
|
| 10 | 18 |
|
|
|
117
|
| 20 | 80 |
|
|
|
118
|
| 25 | 83 |
|
|
|
119
|
| 30 | 86 |
|
|
|
120
|
| 40 | 92 |
|
|
|
121
|
| 50 | 97 |
|
|
|
122
|
| 60 | 100 |
|
|
|
123
|
| 80 | 100 |
|
|
|
124
|
| 100 | 100 |
|
|
|
125
|
|
|
|
126
|
Impact functional:
|
|
|
127
|
|
|
|
128
|
- `battery/value` publica procentul convertit
|
|
|
129
|
- `battery_low/value` este calculat din procentul convertit daca nu exista deja `battery_low` in payload
|
|
|
130
|
- la pragul implicit `20`, un `raw=5` devine `25`, deci nu va fi marcat imediat ca `battery_low`
|
|
|
131
|
|
|
|
132
|
### 2. `PA-44Z/homebus-adapter`
|
|
|
133
|
|
|
|
134
|
Implementarea este in:
|
|
|
135
|
|
|
|
136
|
- [PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js#L84)
|
|
|
137
|
|
|
|
138
|
Configurarea tipului de baterie:
|
|
|
139
|
|
|
|
140
|
- [PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js#L9)
|
|
|
141
|
|
|
|
142
|
Unde este folosita functia:
|
|
|
143
|
|
|
|
144
|
- pentru `battery`: [PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js#L215)
|
|
|
145
|
- pentru derivarea `battery_low`: [PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/PA-44Z/homebus-adapter/z2m-pa-44z-homebus.js#L217)
|
|
|
146
|
|
|
|
147
|
Functia:
|
|
|
148
|
|
|
|
149
|
```js
|
|
|
150
|
var ALKALINE_BATTERY_CURVE = [
|
|
|
151
|
{ pct: 100, voltage: 1.60 },
|
|
|
152
|
{ pct: 90, voltage: 1.55 },
|
|
|
153
|
{ pct: 80, voltage: 1.50 },
|
|
|
154
|
{ pct: 70, voltage: 1.46 },
|
|
|
155
|
{ pct: 60, voltage: 1.42 },
|
|
|
156
|
{ pct: 50, voltage: 1.36 },
|
|
|
157
|
{ pct: 40, voltage: 1.30 },
|
|
|
158
|
{ pct: 30, voltage: 1.25 },
|
|
|
159
|
{ pct: 20, voltage: 1.20 },
|
|
|
160
|
{ pct: 10, voltage: 1.10 },
|
|
|
161
|
{ pct: 0, voltage: 0.90 }
|
|
|
162
|
];
|
|
|
163
|
|
|
|
164
|
var NIMH_BATTERY_CURVE = [
|
|
|
165
|
{ pct: 100, voltage: 1.40 },
|
|
|
166
|
{ pct: 95, voltage: 1.33 },
|
|
|
167
|
{ pct: 90, voltage: 1.28 },
|
|
|
168
|
{ pct: 85, voltage: 1.24 },
|
|
|
169
|
{ pct: 80, voltage: 1.20 },
|
|
|
170
|
{ pct: 75, voltage: 1.19 },
|
|
|
171
|
{ pct: 70, voltage: 1.18 },
|
|
|
172
|
{ pct: 60, voltage: 1.17 },
|
|
|
173
|
{ pct: 50, voltage: 1.16 },
|
|
|
174
|
{ pct: 40, voltage: 1.15 },
|
|
|
175
|
{ pct: 30, voltage: 1.13 },
|
|
|
176
|
{ pct: 20, voltage: 1.11 },
|
|
|
177
|
{ pct: 10, voltage: 1.07 },
|
|
|
178
|
{ pct: 0, voltage: 1.00 }
|
|
|
179
|
];
|
|
|
180
|
|
|
|
181
|
function translateBatteryLevel(rawValue) {
|
|
|
182
|
if (rawValue === null || rawValue === undefined) return null;
|
|
|
183
|
var raw = clamp(Math.round(Number(rawValue)), 0, 100);
|
|
|
184
|
if (node.batteryType !== "nimh") return raw;
|
|
|
185
|
|
|
|
186
|
var estimatedVoltage = interpolateCurve(ALKALINE_BATTERY_CURVE, "pct", "voltage", raw);
|
|
|
187
|
var nimhPct = interpolateCurve(NIMH_BATTERY_CURVE, "voltage", "pct", estimatedVoltage);
|
|
|
188
|
return clamp(Math.round(nimhPct), 0, 100);
|
|
|
189
|
}
|
|
|
190
|
```
|
|
|
191
|
|
|
|
192
|
Observatii:
|
|
|
193
|
|
|
|
194
|
- foloseste aceeasi conversie bazata pe curbe ca adaptorul `ZG-204ZV`
|
|
|
195
|
- rezultatul este continuu si mai aproape de comportamentul electric asteptat pentru `NiMH`
|
|
|
196
|
|
|
|
197
|
Exemple:
|
|
|
198
|
|
|
|
199
|
| Brut | Convertit `nimh` |
|
|
|
200
|
| --- | --- |
|
|
|
201
|
| 5 | 0 |
|
|
|
202
|
| 10 | 18 |
|
|
|
203
|
| 15 | 40 |
|
|
|
204
|
| 20 | 80 |
|
|
|
205
|
| 30 | 86 |
|
|
|
206
|
| 50 | 97 |
|
|
|
207
|
| 55 | 98 |
|
|
|
208
|
| 95 | 100 |
|
|
|
209
|
|
|
|
210
|
Impact functional:
|
|
|
211
|
|
|
|
212
|
- `battery` publicat pe HomeBus este valoarea convertita
|
|
|
213
|
- daca `payload.battery_low` lipseste, acesta este derivat din valoarea convertita si comparat cu `batteryLowThreshold`
|
|
|
214
|
|
|
|
215
|
## Diferente intre cele doua implementari
|
|
|
216
|
|
|
|
217
|
In varianta actuala, ambele adaptoare folosesc aceeasi abordare bazata pe curbe, pentru a evita discrepantele dintre proiecte.
|
|
|
218
|
|
|
|
219
|
## Referinte suplimentare in documentatia UI
|
|
|
220
|
|
|
|
221
|
Explicatiile din help-ul nodurilor confirma intentia conversiei:
|
|
|
222
|
|
|
|
223
|
- [zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.html](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/zg-204zv/homebus-adapter/z2m-zg-204zv-homebus.html#L64)
|
|
|
224
|
- [PA-44Z/homebus-adapter/z2m-pa-44z-homebus.html](/Users/bogdan/Documents/Workspaces/Home/NodeRed/myNodes/PA-44Z/homebus-adapter/z2m-pa-44z-homebus.html#L60)
|
|
|
225
|
|
|
|
226
|
## Concluzie
|
|
|
227
|
|
|
|
228
|
In starea actuala a repository-ului, functia de conversie intre `NiMH` si `alkaline` este folosita in doua proiecte:
|
|
|
229
|
|
|
|
230
|
- `zg-204zv/homebus-adapter`
|
|
|
231
|
- `PA-44Z/homebus-adapter`
|
|
|
232
|
|
|
|
233
|
Ambele aplica conversia doar cand `batteryType` este setat la `nimh`, iar pentru `alkaline` publica procentul brut primit de la Zigbee2MQTT.
|