-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinverter.yaml
371 lines (324 loc) · 10.5 KB
/
inverter.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
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
# Basic configuration substitutions
substitutions:
name1: "inverter"
friendly_name: "Inverter"
tx_pin: GPIO17
rx_pin: GPIO16
sw1_pin: GPIO22 #Relay1
sw2_pin: GPIO18 #Relay2
esphome:
name: ${name1}
friendly_name: ${friendly_name}
esp32:
board: esp32doit-devkit-v1
framework:
type: arduino
# Enable logging
logger:
level: INFO #VERY_VERBOSE
logs:
uart: INFO
sensor: INFO
text_sensor: INFO #DEBUG
# Enable Home Assistant API
api:
ota:
platform: esphome
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable AP mode if WiFi connection fails
ap:
ssid: "Inverter Test Fallback"
password: !secret fallback_password
# Base UART Configuration
uart:
id: uart_bus
tx_pin: ${tx_pin}
rx_pin: ${rx_pin}
baud_rate: 2400
data_bits: 8
parity: NONE
stop_bits: 1
rx_buffer_size: 256 # Increased buffer size
# Status LED for visual debugging
status_led:
pin:
number: GPIO2
inverted: no
globals:
- id: last_response_global
type: std::string
initial_value: ""
- id: last_successful_update
type: uint32_t
restore_value: no
initial_value: '0'
button:
- platform: restart
name: "Restart Device"
- platform: template
name: "Send Test Command"
on_press:
then:
- uart.write: [0x51, 0x36, 0x0D]
- logger.log: "Test command sent"
# Text Sensors
text_sensor:
- platform: template
name: "Last Response"
id: last_response
icon: "mdi:solar-power"
entity_category: "diagnostic"
update_interval: never
lambda: |-
return {id(last_response_global)};
- platform: template
name: "Status"
id: inverter_status
icon: "mdi:power"
update_interval: never
- platform: template
name: "Version"
id: inverter_version
icon: "mdi:information"
update_interval: never
# Relay Configuration (Optional)
switch:
- platform: gpio
name: "Relay 1"
pin: ${sw1_pin}
inverted: true # Important: true because relay is active-low
- platform: gpio
name: "Relay 2"
pin: ${sw2_pin}
inverted: true
# Sensors Configuration
sensor:
- platform: template
name: "Input Voltage"
id: input_voltage
unit_of_measurement: "V"
icon: "mdi:transmission-tower"
accuracy_decimals: 1
update_interval: never
- platform: template
name: "Input Frequency"
id: input_frequency
unit_of_measurement: "Hz"
icon: "mdi:sine-wave"
accuracy_decimals: 1
update_interval: never
- platform: template
name: "Output Voltage"
id: output_voltage
unit_of_measurement: "V"
icon: "mdi:power-plug"
accuracy_decimals: 1
update_interval: never
- platform: template
name: "Output Frequency"
id: output_frequency
unit_of_measurement: "Hz"
icon: "mdi:sine-wave"
accuracy_decimals: 1
update_interval: never
- platform: template
name: "Battery Voltage"
id: battery_voltage
unit_of_measurement: "V"
icon: "mdi:battery"
accuracy_decimals: 1
update_interval: never
- platform: template
name: "Battery Charging Current"
id: battery_charging_current
unit_of_measurement: "A"
icon: "mdi:current-ac"
accuracy_decimals: 1
update_interval: never
- platform: template
name: "Apparent Power"
id: apparent_power
unit_of_measurement: "VA"
icon: "mdi:flash"
accuracy_decimals: 0
update_interval: never
- platform: template
name: "Active Power"
id: active_power
unit_of_measurement: "W"
icon: "mdi:power-plug"
accuracy_decimals: 0
update_interval: never
- platform: template
name: "Power Factor"
id: power_factor
accuracy_decimals: 2
icon: "mdi:percent"
update_interval: never
- platform: template
name: "Load"
id: inverter_load
unit_of_measurement: "%"
icon: "mdi:gauge"
accuracy_decimals: 0
update_interval: never
- platform: template
name: "Temperature"
id: inverter_temp
unit_of_measurement: "°C"
icon: "mdi:thermometer"
accuracy_decimals: 1
update_interval: never
- platform: template
name: "Output Current"
id: output_current
unit_of_measurement: "A"
icon: "mdi:current-ac"
accuracy_decimals: 1
update_interval: never
lambda: |-
if (id(output_voltage).state > 0) {
return id(active_power).state / id(output_voltage).state;
}
return 0.0;
# Binary Sensors Configuration
binary_sensor:
- platform: status
name: "${friendly_name} Status"
id: system_status
entity_category: "diagnostic"
- platform: template
name: "${friendly_name} Online"
id: inverter_online
device_class: "connectivity"
lambda: |-
static const uint32_t TIMEOUT = 10000; // 10 seconds
return (millis() - id(last_successful_update)) < TIMEOUT;
- platform: template
name: "Battery Mode"
id: battery_mode
device_class: "power"
- platform: template
name: "Line Mode"
id: line_mode
device_class: "power"
# Main interval loop for data polling and processing
interval:
- interval: 2s
then:
- lambda: |-
uint8_t data;
std::string response;
bool received_data = false;
// Read all available UART data
while (id(uart_bus).available()) {
if (id(uart_bus).read_byte(&data)) {
char hex[4];
sprintf(hex, "%02X ", data);
response += hex;
received_data = true;
}
}
if (received_data) {
ESP_LOGD("uart_test", "Raw data: %s", response.c_str());
id(last_response_global) = response;
id(last_response).publish_state(response);
// Convert hex to ASCII
std::string ascii_data;
std::string temp;
for(size_t i = 0; i < response.length(); i += 3) { // 3 because each byte is "XX "
if(response[i] && response[i+1]) {
char byte = (char)strtol(response.substr(i, 2).c_str(), nullptr, 16);
ascii_data += byte;
}
}
// Split into values
std::vector<std::string> values;
temp = "";
for(char c : ascii_data) {
if(c == ' ') {
if(!temp.empty()) values.push_back(temp);
temp = "";
} else {
temp += c;
}
}
if(!temp.empty()) values.push_back(temp);
if(values.size() >= 15) {
// Update last successful data timestamp
id(last_successful_update) = millis();
ESP_LOGD("parse", "Parsing values: %s", ascii_data.c_str());
// Remove parenthesis from first value
if(!values[0].empty() && values[0][0] == '(') {
values[0] = values[0].substr(1);
}
// Convert strings to numbers
float in_voltage = atof(values[0].c_str());
float in_freq = atof(values[1].c_str());
float out_voltage = atof(values[2].c_str());
float out_freq = atof(values[3].c_str());
float batt_voltage = atof(values[4].c_str()) * 4;
float ac_power = atof(values[5].c_str());
float charging_current = atof(values[6].c_str());
float temperature = atof(values[13].c_str());
int apparent = atoi(values[10].c_str()) * 10;
int active = atoi(values[11].c_str()) * 10;
int load = atoi(values[12].c_str());
// Publish values with range checking
if (in_voltage >= 0 && in_voltage <= 250)
id(input_voltage).publish_state(in_voltage);
if (in_freq >= 0 && in_freq <= 60)
id(input_frequency).publish_state(in_freq);
if (out_voltage >= 0 && out_voltage <= 250)
id(output_voltage).publish_state(out_voltage);
if (out_freq >= 0 && out_freq <= 60)
id(output_frequency).publish_state(out_freq);
if (batt_voltage >= 0 && batt_voltage <= 60)
id(battery_voltage).publish_state(batt_voltage);
if (ac_power >= 0 && ac_power <= 6000)
id(active_power).publish_state(ac_power);
if (charging_current >= -100 && charging_current <= 100)
id(battery_charging_current).publish_state(charging_current);
if (temperature >= 0 && temperature <= 100)
id(inverter_temp).publish_state(temperature);
if (apparent >= 0 && apparent <= 6000)
id(apparent_power).publish_state(apparent);
if (active >= 0 && active <= 6000)
id(active_power).publish_state(active);
if (load >= 0 && load <= 100)
id(inverter_load).publish_state(load);
// Calculate and publish power factor
if(apparent > 0) {
float pf = pow((float)active/apparent, 2);
if (pf >= 0 && pf <= 1)
id(power_factor).publish_state(pf);
}
// Calculate and publish output current
if (out_voltage > 0) {
float current = (float)active/out_voltage;
if (current >= 0 && current <= 30) { // 30A limit as example
id(output_current).publish_state(current);
}
}
// Update status
std::string status_code = values[14];
if(status_code == "B") {
id(inverter_status).publish_state("Battery");
id(battery_mode).publish_state(true);
id(line_mode).publish_state(false);
} else if(status_code == "L") {
id(inverter_status).publish_state("Line");
id(battery_mode).publish_state(false);
id(line_mode).publish_state(true);
}
// Update version if available
if(values.size() >= 18) {
id(inverter_version).publish_state(values[17]);
}
}
}
# Send command
- uart.write: [0x51, 0x36, 0x0D]
- delay: 60ms