-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpower_meter_fronius_with_simulated_load.yaml
107 lines (98 loc) · 4.25 KB
/
power_meter_fronius_with_simulated_load.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
<<: !include power_meter_common.yaml
esphome:
min_version: 2024.11.1
# ----------------------------------------------------------------------------------------------------
# Sensor updated every second to give feedback in Home Assistant
# ----------------------------------------------------------------------------------------------------
sensor:
# Sensor showing the actual power consumption
- id: real_power
platform: template
name: "Real Power"
device_class: "power"
unit_of_measurement: "W"
update_interval: 1s
# ----------------------------------------------------------------------------------------------------
# User interaction
# ----------------------------------------------------------------------------------------------------
number:
# Simulated load used for developement
# It should be commented out for production
- platform: template
name: "Simulated Load"
id: simulated_load_power
optimistic: True
restore_value: True
mode: box
min_value: -99999
max_value: 99999
initial_value: 0
unit_of_measurement: "W"
step: 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(response->body, [](JsonObject root) -> bool {
if (!root.containsKey("Body") || !root["Body"]["Data"]["0"].containsKey("PowerReal_P_Sum")) {
ESP_LOGW("custom", "Invalid JSON structure");
return false;
}
id(real_power).publish_state(
data["Body"]["Data"]["0"]["PowerReal_P_Sum"].as< float >()
+ id(simulated_load_power).state*id(triac_opening).state / 100 // Simulated load used for developement. It should be commented out for production.
);
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);
time:
- platform: sntp
on_time:
- seconds: /1
then:
- if:
condition:
- lambda: return id(power_meter_activated) != 0;
then:
- script.execute: power_meter_source