-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpower_meter_fronius.yaml
109 lines (102 loc) · 4.73 KB
/
power_meter_fronius.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
<<: !include power_meter_common.yaml
esphome:
min_version: 2024.11.1
# ----------------------------------------------------------------------------------------------------
# Use http request component
# ----------------------------------------------------------------------------------------------------
http_request:
id: http_request_data
useragent: esphome/device
timeout: 10s
verify_ssl: False
# ----------------------------------------------------------------------------------------------------
# Define scripts for power collection
# ----------------------------------------------------------------------------------------------------
script:
# Fronius script gather power reports from inverter and update globals (real_power)
# Information are provided as json. Power exchanged with the grid is names PowerReal_P_Sum
# When this value is positive, energy is taken from the grid.
# When this value is negative, energy is pushed to the grid.
- id: power_meter_source
mode: single
then:
- if:
condition:
lambda: 'return network::is_connected();'
then:
- http_request.get:
url: http://${power_meter_ip_address}/solar_api/v1/GetMeterRealtimeData.cgi
headers:
Content-Type: application/json
capture_response: true
max_response_buffer_size: 4096
on_response:
then:
- lambda: |-
if (response->status_code != 200) {
ESP_LOGW("custom", "HTTP Request failed with status: %d", response->status_code);
id(real_power).publish_state(NAN);
} else {
bool parse_success = json::parse_json(body, [](JsonObject root) -> bool {
if (!root.containsKey("Body")) {
ESP_LOGW("custom", "Invalid JSON structure");
return false;
}
id(real_power).publish_state(root["Body"]["Data"]["0"]["PowerReal_P_Sum"].as<float>());
return true;
});
if (!parse_success) {
ESP_LOGW("custom", "JSON Parsing failed");
id(real_power).publish_state(NAN);
}
}
on_error:
then:
- lambda: |-
ESP_LOGW("custom", "HTTP Request failed or timeout occurred");
id(real_power).publish_state(NAN);
- http_request.get:
url: http://${power_meter_ip_address}/solar_api/v1/GetPowerFlowRealtimeData.fcgi
headers:
Content-Type: application/json
capture_response: true
max_response_buffer_size: 4096
on_response:
then:
- lambda: |-
if (response->status_code != 200) {
ESP_LOGW("custom", "HTTP Request failed with status: %d", response->status_code);
id(consumption).publish_state(NAN);
} else {
bool parse_success = json::parse_json(body, [](JsonObject root) -> bool {
if (!root.containsKey("Body")) {
ESP_LOGW("custom", "Invalid JSON structure");
id(consumption).publish_state(NAN);
return false;
}
id(consumption).publish_state(
root["Body"]["Data"]["Site"]["P_Grid"].as<float>()
+ root["Body"]["Data"]["Site"]["P_PV"].as<float>()
);
return true;
});
if (!parse_success) {
ESP_LOGW("custom", "JSON Parsing failed");
id(consumption).publish_state(NAN);
}
}
on_error:
then:
- lambda: |-
ESP_LOGW("custom", "HTTP Request failed or timeout occurred");
id(consumption).publish_state(NAN);
time:
- platform: sntp
on_time:
- seconds: /1
then:
- if:
condition:
- lambda: return id(power_meter_activated) != 0;
then:
- script.execute: power_meter_source