Skip to content

Commit 9b69841

Browse files
author
Xavier Berger
committed
feat(energy_counter): improve theorical counter
1 parent ef25764 commit 9b69841

6 files changed

+96
-2
lines changed

esp8266-standalone_on_off.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ substitutions:
3838
# Power meter source -----------------------------------------------------------
3939
# Define ip address of Power Meter (Fronius Inverter)
4040
main_power_sensor: "sensor.smart_meter_ts_100a_1_puissance_reelle"
41+
consumption_sensor: "sensor.solarnet_power_load_consumed"
4142

4243
# LEDs -------------------------------------------------------------------------
4344
# Green LED is reflecting regulation status

solar_router/energy_counter_theorical.yaml

+12-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,18 @@ script:
2020
mode: single
2121
then:
2222
- lambda: |-
23-
id(therorical_energy_diverted).publish_state(id(load_power).state*id(regulator_opening).state / 100);
23+
double diverted_energy = id(load_power).state*id(regulator_opening).state / 100;
24+
if (id(consumption).state >= diverted_energy or isnan(id(consumption).state))
25+
{
26+
// Therorical energy diverted is consumed (or we don't know the consumption)
27+
id(therorical_energy_diverted).publish_state(diverted_energy);
28+
}
29+
else
30+
{
31+
// Therorical energy diverted is not consumed
32+
id(therorical_energy_diverted).publish_state(0.0);
33+
}
34+
2435
2536
# Sensor showing the actual energy diverted consumption
2637
sensor:

solar_router/power_meter_common.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,11 @@ sensor:
2424
device_class: "power"
2525
unit_of_measurement: "W"
2626
update_interval: 1s
27+
28+
# Sensor showing the actual power consumption
29+
- id: consumption
30+
platform: template
31+
name: "Consumption"
32+
device_class: "power"
33+
unit_of_measurement: "W"
34+
update_interval: 1s

solar_router/power_meter_fronius.yaml

+36
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,42 @@ script:
6161
- lambda: |-
6262
ESP_LOGW("custom", "HTTP Request failed or timeout occurred");
6363
id(real_power).publish_state(NAN);
64+
- http_request.get:
65+
url: http://${power_meter_ip_address}/solar_api/v1/GetPowerFlowRealtimeData.fcgi
66+
headers:
67+
Content-Type: application/json
68+
capture_response: true
69+
max_response_buffer_size: 4096
70+
on_response:
71+
then:
72+
- lambda: |-
73+
if (response->status_code != 200) {
74+
ESP_LOGW("custom", "HTTP Request failed with status: %d", response->status_code);
75+
id(consumption).publish_state(NAN);
76+
} else {
77+
bool parse_success = json::parse_json(body, [](JsonObject root) -> bool {
78+
if (!root.containsKey("Body")) {
79+
ESP_LOGW("custom", "Invalid JSON structure");
80+
id(consumption).publish_state(NAN);
81+
return false;
82+
}
83+
id(consumption).publish_state(
84+
root["Body"]["Data"]["Site"]["P_Grid"].as<float>()
85+
+ root["Body"]["Data"]["Site"]["P_PV"].as<float>()
86+
);
87+
return true;
88+
});
89+
90+
if (!parse_success) {
91+
ESP_LOGW("custom", "JSON Parsing failed");
92+
id(consumption).publish_state(NAN);
93+
}
94+
}
95+
on_error:
96+
then:
97+
- lambda: |-
98+
ESP_LOGW("custom", "HTTP Request failed or timeout occurred");
99+
id(consumption).publish_state(NAN);
64100
time:
65101
- platform: sntp
66102
on_time:

solar_router/power_meter_home_assistant.yaml

+10-1
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,20 @@
55
# ----------------------------------------------------------------------------------------------------
66

77
sensor:
8-
# Sensor showing the actual power consumption
8+
# Sensor showing the actual power exchange
99
- platform: homeassistant
1010
id: real_power
1111
entity_id: ${main_power_sensor}
1212
internal: False
1313
name: "Real Power"
1414
device_class: "power"
1515
unit_of_measurement: "W"
16+
17+
# Sensor showing the actual consumption
18+
- platform: homeassistant
19+
id: consumption
20+
entity_id: ${consumption_sensor}
21+
internal: False
22+
name: "Consumption"
23+
device_class: "power"
24+
unit_of_measurement: "W"

solar_router/power_meter_proxy_client.yaml

+29
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,36 @@ script:
5757
- lambda: |-
5858
ESP_LOGW("custom", "HTTP Request failed or timeout occurred");
5959
id(real_power).publish_state(NAN);
60+
- http_request.get:
61+
url: http://${power_meter_ip_address}/sensor/consumption
62+
capture_response: true
63+
max_response_buffer_size: 4096
64+
on_response:
65+
then:
66+
- lambda: |-
67+
if (response->status_code != 200) {
68+
ESP_LOGW("custom", "HTTP Request failed with status: %d", response->status_code);
69+
id(consumption).publish_state(NAN);
70+
} else {
71+
bool parse_success = json::parse_json(body, [](JsonObject root) -> bool {
72+
if (!root.containsKey("value")) {
73+
ESP_LOGW("custom", "Invalid JSON structure");
74+
return false;
75+
}
76+
id(consumption).publish_state(root["value"].as< float >());
77+
return true;
78+
});
6079
80+
if (!parse_success) {
81+
ESP_LOGW("custom", "JSON Parsing failed");
82+
id(consumption).publish_state(NAN);
83+
}
84+
}
85+
on_error:
86+
then:
87+
- lambda: |-
88+
ESP_LOGW("custom", "HTTP Request failed or timeout occurred");
89+
id(consumption).publish_state(NAN);
6190
time:
6291
- platform: sntp
6392
on_time:

0 commit comments

Comments
 (0)