Skip to content

Commit 7bb89dd

Browse files
Merge pull request #82 from martijnvwezel/develop
Improve Home Assistant stability, liters handling, and calibration safety
2 parents 1485525 + 1f8c52c commit 7bb89dd

3 files changed

Lines changed: 108 additions & 38 deletions

File tree

muino-water-meter-esp32.yaml

Lines changed: 103 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -56,34 +56,43 @@ interval:
5656
condition:
5757
lambda: |-
5858
return id(debugmodus).state;
59-
then:
60-
- text_sensor.template.publish:
61-
id: debug_json
62-
state: !lambda |-
63-
return "{\"liters\":" + to_string(id(liters)) +
64-
",\"phase\":" + to_string(id(phase)) +
65-
",\"last_reported_liters\":" + to_string(id(last_reported_liters)) +
66-
",\"last_reported_liters__mili\":" + to_string(id(last_reported_liters__mili)) +
67-
",\"aa\":" + to_string(id(aa)) +
68-
",\"bb\":" + to_string(id(bb)) +
69-
",\"cc\":" + to_string(id(cc)) +
70-
",\"max_a\":" + to_string(id(max_a)) +
71-
",\"max_b\":" + to_string(id(max_b)) +
72-
",\"max_c\":" + to_string(id(max_c)) +
73-
",\"min_a\":" + to_string(id(min_a)) +
74-
",\"min_b\":" + to_string(id(min_b)) +
75-
",\"min_c\":" + to_string(id(min_c)) +
76-
",\"upper_bound\":" + to_string(id(upper_bound)) +
77-
",\"lower_bound\":" + to_string(id(lower_bound)) +
78-
",\"flow_rate_current\":" + to_string(id(flow_rate_current)) +
79-
",\"flow_rate_last\":" + to_string(id(flow_rate_last)) +
80-
",\"current_consumption\":" + to_string(id(current_consumption)) +
81-
",\"previous_consumption\":" + to_string(id(previous_consumption)) +
82-
"}";
59+
then:
60+
- lambda: |-
61+
char json[256];
62+
int flow_cur = (int)(id(flow_rate_current) * 1);
63+
int flow_last = (int)(id(flow_rate_last) * 1);
64+
int cons_cur = (int)(id(current_consumption) * 1);
65+
int cons_last = (int)(id(previous_consumption) * 1);
66+
snprintf(
67+
json, sizeof(json),
68+
"{"
69+
"\"liters\":%d,\"phase\":%d,"
70+
"\"last_L\":%d,\"last_mL\":%d,"
71+
"\"aa\":%d,\"bb\":%d,\"cc\":%d,"
72+
"\"max_a\":%d,\"max_b\":%d,\"max_c\":%d,"
73+
"\"min_a\":%d,\"min_b\":%d,\"min_c\":%d,"
74+
"\"flow_cur\":%d,\"flow_last\":%d,"
75+
"\"cons_cur\":%d,\"cons_last\":%d"
76+
"}"
77+
,id(liters), id(phase)
78+
,id(last_reported_liters)
79+
,id(last_reported_liters__mili)
80+
81+
,id(aa), id(bb), id(cc)
82+
,id(max_a), id(max_b), id(max_c)
83+
,id(min_a), id(min_b), id(min_c)
84+
85+
,flow_cur, flow_last
86+
,cons_cur, cons_last
87+
);
88+
89+
ESP_LOGD("debug_json", "Publishing debug json: %s", json);
90+
id(debug_json).publish_state(json);
91+
8392
else:
8493
- lambda: |-
85-
// If fastupdate is off, only update every 60 seconds
86-
if ((millis() - id(last_water_flow)) >= 60000) {
94+
// If fastupdate is off, only update every 60 seconds (configurable)
95+
if ((millis() - id(last_water_flow)) >= id(update_interval_idle) * 1000) {
8796
if (id(spindel_normal).state) {
8897
// Normal meter: symmetric 180° pattern
8998
id(last_reported_liters__mili) = (int)(1000*(id(liters)+id(phase)/6.0));
@@ -172,6 +181,28 @@ switch:
172181
- lambda: |-
173182
id(manual_calibration) = false;
174183
184+
button:
185+
- platform: template
186+
name: "Reset Calibration"
187+
icon: "mdi:restart"
188+
on_press:
189+
- lambda: |-
190+
int min = id(lower_bound);
191+
int a = id(light_sensor_a_light).state - id(light_sensor_a_dark).state;
192+
if (a < min) a = id(light_sensor_a_light).state;
193+
int b = id(light_sensor_b_light).state - id(light_sensor_b_dark).state;
194+
if (b < min) b = id(light_sensor_b_light).state;
195+
int c = id(light_sensor_c_light).state - id(light_sensor_c_dark).state;
196+
if (c < min) c = id(light_sensor_c_light).state;
197+
198+
id(liters) = 0;
199+
id(min_a)= a;
200+
id(min_b)= b;
201+
id(min_c)= c;
202+
id(max_a)= 0;
203+
id(max_b)= 0;
204+
id(max_c)= 0;
205+
175206
text_sensor:
176207
- platform: template
177208
name: "debug_JSON"
@@ -373,19 +404,34 @@ sensor:
373404
374405
# liters
375406
- platform: template
376-
name: "water_liter_sensor"
407+
name: "⚠️ Water Total (mL) [DEPRECATED]"
408+
icon: mdi:alert-circle-outline
377409
id: report_liters
378410
force_update: false
379411
device_class: "water"
380412
unit_of_measurement: "mL"
381-
accuracy_decimals: 0
413+
accuracy_decimals: 3
382414
state_class: total_increasing
383415
lambda: |-
384416
if (id(liters) < 2){
385417
return 0;
386418
}
387419
return id(last_reported_liters__mili);
388420
421+
- platform: template
422+
name: "Muino Water Total (L)"
423+
id: report_liters_ml
424+
force_update: false
425+
device_class: "water"
426+
unit_of_measurement: "L"
427+
accuracy_decimals: 3
428+
state_class: total_increasing
429+
lambda: |-
430+
if (id(liters) < 2){
431+
return 0;
432+
}
433+
return id(last_reported_liters__mili)/1000.0;
434+
389435
- platform: template
390436
name: "liters"
391437
id: report_liters_rounded
@@ -429,7 +475,6 @@ sensor:
429475
device_class: "water"
430476
unit_of_measurement: "L"
431477
accuracy_decimals: 1
432-
state_class: measurement
433478
lambda: |-
434479
return id(current_consumption);
435480
@@ -440,7 +485,6 @@ sensor:
440485
device_class: "water"
441486
unit_of_measurement: "L"
442487
accuracy_decimals: 1
443-
state_class: measurement
444488
lambda: |-
445489
return id(previous_consumption);
446490
@@ -456,6 +500,7 @@ sensor:
456500
switch.is_on: fastupdate
457501
then:
458502
- component.update: report_liters
503+
- component.update: report_liters_ml
459504
- component.update: report_liters_rounded
460505
- component.update: al
461506
- component.update: bl
@@ -500,7 +545,7 @@ sensor:
500545
int min = id(lower_bound);
501546
502547
if (a < min || b < min || c < min ){
503-
ESP_LOGW("light_level", "Too dark, ignoring this measurement");
548+
ESP_LOGW("light_level", "Signal too weak. Possible causes: too bright environment, LED failure, or sensor misalignment. Ignoring measurement.");
504549
return 0;
505550
}
506551
if (a > max || b > max || c > max){
@@ -523,9 +568,17 @@ sensor:
523568
if (id(liters) < 2) {
524569
alpha_cor = 0.1; // when 2 liter not found correct harder
525570
if (id(liters) < 0) {
571+
// Reset calibration if negative liters detected
526572
id(liters) = 0;
573+
id(min_a)= a;
574+
id(min_b)= b;
575+
id(min_c)= c;
576+
id(max_a)= 0;
577+
id(max_b)= 0;
578+
id(max_c)= 0;
527579
}
528580
}
581+
529582
auto mini_average = [](float x, float y, float alpha_cor){
530583
if ((x + 5) <= y && y > 10) {
531584
return x;
@@ -566,7 +619,7 @@ sensor:
566619
c -= (id(min_c) + id(max_c)) >> 1;
567620
}
568621
// log with manual calibration the offset and without first string manual_ofset* tehn the auto calibration one
569-
ESP_LOGI("calibration", "a_offset:%d b_offset:%d c_offset:%d | a_auto:%d b_auto:%d c_auto:%d",
622+
ESP_LOGD("calibration", "a_offset:%d b_offset:%d c_offset:%d | a_auto:%d b_auto:%d c_auto:%d",
570623
id(manual_offset_a), id(manual_offset_b), id(manual_offset_c),
571624
(id(min_a) + id(max_a)) >> 1,
572625
(id(min_b) + id(max_b)) >> 1,
@@ -772,6 +825,11 @@ globals:
772825
type: int
773826
initial_value: "0"
774827

828+
- id: update_interval_idle
829+
type: int
830+
restore_value: yes
831+
initial_value: "60"
832+
775833
number:
776834
- platform: template
777835
id: offset_a_number
@@ -809,12 +867,24 @@ number:
809867
- lambda: |-
810868
id(manual_offset_c) = (int) x;
811869
870+
- platform: template
871+
id: update_interval_idle_number
872+
name: "Update Interval (s)"
873+
min_value: 1
874+
max_value: 3600
875+
step: 1
876+
lambda: return id(update_interval_idle);
877+
set_action:
878+
then:
879+
- lambda: |-
880+
id(update_interval_idle) = (int) x;
881+
812882
813883
814884
# Make sure logging is correct for solving platform IO bugs
815885
logger:
816886
hardware_uart: USB_SERIAL_JTAG
817-
level: ERROR
887+
level: INFO
818888

819889
# API is a requirement of the dashboard import.
820890
api:

production_installer.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#!/bin/bash
2-
3-
docker run --rm --privileged -v ${PWD}:/config -it ghcr.io/esphome/esphome compile "muino-water-meter-esp32.yaml"
2+
docker pull ghcr.io/esphome/esphome:latest
3+
docker run --rm --privileged -v ${PWD}:/config -it ghcr.io/esphome/esphome compile "muino-water-meter-esp32.factory.yaml"
44
counter=1
55
while true; do
66
if [ -e "/dev/ttyACM0" ]; then
77
counter=$((counter+1))
88
echo -e "\033[34mGoing to program $counter times\033[0m"
9-
# ...existing code...
10-
docker run --rm --privileged -v ${PWD}:/config -it ghcr.io/esphome/esphome upload --device=/dev/ttyACM0 "muino-water-meter-esp32.yaml"
11-
docker run --rm --privileged -v ${PWD}:/config -it ghcr.io/esphome/esphome logs --device=/dev/ttyACM0 "muino-water-meter-esp32.yaml"
9+
10+
docker run --rm --privileged -v ${PWD}:/config -it ghcr.io/esphome/esphome upload --device=/dev/ttyACM0 "muino-water-meter-esp32.factory.yaml"
11+
docker run --rm --privileged -v ${PWD}:/config -it ghcr.io/esphome/esphome logs --device=/dev/ttyACM0 "muino-water-meter-esp32.factory.yaml"
1212
echo -e "\033[34mEntered the program $counter times\033[0m"
1313

1414
fi

0 commit comments

Comments
 (0)