forked from tfyoung/esphome-junctek_kgf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expanded_sample.yaml
324 lines (272 loc) · 9.63 KB
/
expanded_sample.yaml
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
substitutions:
name: master-batt-bus
device_description: "Master Battery Bus Monitor - KG160F"
external_components:
- source: github://alanv72/esphome-junctek_kgf
refresh: 0s
esphome:
name: ${name}
# project:
# name: "alanv72.esphome-junctek_kgf"
# version: latestesphome:
esp8266:
board: nodemcuv2
# Enable logging
logger:
level: INFO
# Enable Home Assistant API
api:
encryption:
key: *****
ota:
- platform: esphome
password: ****
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Master-Batt-Bus Fallback Hotspot"
password: *****
captive_portal:
globals:
- id: sample_1
type: float
restore_value: no
initial_value: '0.0'
- id: sample_2
type: float
restore_value: no
initial_value: '0.0'
- id: sample_3
type: float
restore_value: no
initial_value: '0.0'
- id: sample_4
type: float
restore_value: no
initial_value: '0.0'
- id: sample_5
type: float
restore_value: no
initial_value: '0.0'
- id: sample_6
type: float
restore_value: no
initial_value: '0.0'
- id: sample_index
type: int
restore_value: no
initial_value: '0'
- id: mean_battery_life_1min
type: float
restore_value: no
initial_value: '0.0'
- id: mean_battery_life_10min
type: float
restore_value: no
initial_value: '0.0'
- id: sum_1min_averages
type: float
restore_value: no
initial_value: '0.0'
- id: count_1min_averages
type: int
restore_value: no
initial_value: '0'
interval:
- interval: 10s
then:
- lambda: |-
int index = id(sample_index);
if (index >= 6) {
index = 0;
}
float battery_life = id(mainbus_battery_life).state;
// ESP_LOGI("sensor_readings", "Raw Battery Life: %.2f", id(mainbus_battery_life).state);
// Check for NaN and handle it
if (isnan(battery_life)) {
ESP_LOGE("battery_life_error", "Received NaN value for battery life");
battery_life = 0.0; // Set a default or retry fetching the value
}
// Store the current battery life in the current sample (in minutes)
switch (index) {
case 0: id(sample_1) = battery_life; break;
case 1: id(sample_2) = battery_life; break;
case 2: id(sample_3) = battery_life; break;
case 3: id(sample_4) = battery_life; break;
case 4: id(sample_5) = battery_life; break;
case 5: id(sample_6) = battery_life; break;
}
// Update the sample index
id(sample_index) = index + 1;
// Calculate the mean battery life in minutes, excluding zeros
float sum = 0.0;
int valid_samples = 0;
if (id(sample_1) > 0) { sum += id(sample_1); valid_samples++; }
if (id(sample_2) > 0) { sum += id(sample_2); valid_samples++; }
if (id(sample_3) > 0) { sum += id(sample_3); valid_samples++; }
if (id(sample_4) > 0) { sum += id(sample_4); valid_samples++; }
if (id(sample_5) > 0) { sum += id(sample_5); valid_samples++; }
if (id(sample_6) > 0) { sum += id(sample_6); valid_samples++; }
if (valid_samples > 0) {
id(mean_battery_life_1min) = sum / valid_samples;
} else {
id(mean_battery_life_1min) = 0.0; // Handle case where all samples are zero
}
// ESP_LOGI("mean1min_debug", "Sum: %.2f, Valid Samples: %d, Mean 1min: %.2f", sum, valid_samples, id(mean_battery_life_1min));
- interval: 1min
then:
- lambda: |-
// ESP_LOGI("mainbus_battery_life_logger", "Main Bus Battery Life: %.2f minutes", id(mainbus_battery_life).state);
// ESP_LOGI("sample_1_logger", "sample 1: %.2f minutes", id(sample_1));
// ESP_LOGI("sample_2_logger", "sample 2: %.2f minutes", id(sample_2));
// ESP_LOGI("sample_3_logger", "sample 3: %.2f minutes", id(sample_3));
// ESP_LOGI("sample_4_logger", "sample 4: %.2f minutes", id(sample_4));
// ESP_LOGI("sample_5_logger", "sample 5: %.2f minutes", id(sample_5));
// ESP_LOGI("sample_6_logger", "sample 6: %.2f minutes", id(sample_6));
// ESP_LOGI("mean1min_logger", "mean 1min: %.2f minutes", id(mean_battery_life_1min));
// Update the rolling 10-minute average
if (id(mean_battery_life_1min) > 0) {
id(sum_1min_averages) += id(mean_battery_life_1min);
id(count_1min_averages)++;
}
if (id(count_1min_averages) > 10) {
id(sum_1min_averages) -= id(mean_battery_life_1min); // Remove the oldest value from the sum
id(count_1min_averages) = 10; // Keep the count at 10
}
if (id(count_1min_averages) > 0) {
id(mean_battery_life_10min) = id(sum_1min_averages) / id(count_1min_averages);
} else {
id(mean_battery_life_10min) = 0.0; // Handle case where no valid averages
}
// ESP_LOGI("mbl10t_logger", "MBL10T: %.2f", id(mean_battery_life_10min));
// Calculate and log mean battery life in human-readable format
float mean_minutes = id(mean_battery_life_10min);
int days = static_cast<int>(mean_minutes) / 1440;
mean_minutes = fmod(mean_minutes, 1440);
int hours = static_cast<int>(mean_minutes) / 60;
mean_minutes = fmod(mean_minutes, 60);
int minutes = static_cast<int>(mean_minutes);
// ESP_LOGI("mean_battery_life_10min_logger", "Mean Battery Life Over 10 Minutes: %d days %d hours %d minutes", days, hours, minutes);
uart:
- id: kg_uart
tx_pin: 15
rx_pin: 13
baud_rate: 115200
rx_buffer_size: 384
stop_bits: 1
data_bits: 8
# debug:
# direction: RX
# dummy_receiver: false
# after:
# delimiter: "\n"
# sequence:
# - lambda: UARTDebug::log_string(direction, bytes);
sensor:
- platform: junctek_kgf
uart_id: kg_uart
address: 5
invert_current: false
voltage:
name: "mainbus_battery_voltage"
current:
name: "mainbus_battery_current"
battery_level:
name: "mainbus_battery_level"
temperature:
name: "mainbus_temperature"
ah_battery_level:
name: "mainbus_ah_battery_level"
ah_total_used:
name: "mainbus_ah_total_used"
wh_battery_level:
name: "mainbus_wh_battery_level"
running_time:
name: "mainbus_running_time"
id: mainbus_running_time
battery_internal_resistor:
name: "mainbus_battery_internal_resistor"
battery_life:
name: "mainbus_battery_life"
id: mainbus_battery_life
power:
name: "mainbus_power"
relay_status:
name: "mainbus_relay_status"
direction:
name: "mainbus_direction"
id: mainbus_direction
######## Relay set value ##########
over_voltage_set:
name: "mainbus_over_voltage_set"
under_voltage_set:
name: "mainbus_under_voltage_set"
positive_overcurrent_set:
name: "mainbus_positive_overcurrent_set"
negative_overcurrent_set:
name: "mainbus_negative_overcurrent_set"
over_power_protection_set:
name: "mainbus_over_power_protection_set"
over_temperature_set:
name: "mainbus_over_temperature_set"
protection_recovery_seconds_set:
name: "mainbus_protection_recovery_seconds_set"
delay_time_set:
name: "mainbus_delay_time_set"
battery_amphour_capacity_set:
name: "mainbus_battery_amphour_capacity_set"
voltage_calibration_set:
name: "mainbus_voltage_calibration_set"
current_calibration_set:
name: "mainbus_current_calibration_set"
temperature_calibration_set:
name: "mainbus_temperature_calibration_set"
reserved_set:
name: "mainbus_reserved_set"
relay_normally_open:
name: "mainbus_relay_normally_open"
current_ratio_set:
name: "mainbus_current_ratio_set"
text_sensor:
- platform: template
name: "Main Bus Monitor Run Time"
id: mainbus_battery_runtime_friendly
update_interval: 5 minutes
lambda: |-
float running_time_state = id(mainbus_running_time).state;
// Check for NaN and handle it
if (isnan(running_time_state)) {
ESP_LOGE("runtime_error", "Received NaN value for running time");
return std::string("Invalid data");
}
int total_seconds = static_cast<int>(running_time_state);
int years = total_seconds / 31536000; // 365 days/year for simplicity, ignoring leap years
total_seconds %= 31536000;
int days = total_seconds / 86400;
total_seconds %= 86400;
int hours = total_seconds / 3600;
total_seconds %= 3600;
int minutes = total_seconds / 60;
char buffer[50];
sprintf(buffer, "%d years %d days %d hours %d mins", years, days, hours, minutes);
return std::string(buffer);
- platform: template
name: "Mean Battery Life Over 10 Minutes"
id: mean_battery_life_10min_text
update_interval: 1min
lambda: |-
float mean_minutes = id(mean_battery_life_10min);
if (mean_minutes == 0.0) {
return std::string("0 days 0 hours 0 mins");
} else {
int days = static_cast<int>(mean_minutes) / 1440;
mean_minutes = fmod(mean_minutes, 1440);
int hours = static_cast<int>(mean_minutes) / 60;
mean_minutes = fmod(mean_minutes, 60);
int minutes = static_cast<int>(mean_minutes);
char buffer[50];
sprintf(buffer, "%d days %d hours %d mins", days, hours, minutes);
return std::string(buffer);
}