From 763c802acf2f0c057c96d7d8faa4158766ef6082 Mon Sep 17 00:00:00 2001 From: Bram Date: Thu, 20 Nov 2025 18:49:57 +0100 Subject: [PATCH] complete bedscale ui --- ESPhome/bed-scale/bed_scale.yaml | 151 +++++++++++++++++++++++++++---- 1 file changed, 135 insertions(+), 16 deletions(-) diff --git a/ESPhome/bed-scale/bed_scale.yaml b/ESPhome/bed-scale/bed_scale.yaml index 83017de..6b95e4e 100644 --- a/ESPhome/bed-scale/bed_scale.yaml +++ b/ESPhome/bed-scale/bed_scale.yaml @@ -34,30 +34,142 @@ wifi: captive_portal: -# sensor: -# - platform: hx711 -# name: "HX711 Value" -# dout_pin: D4 -# clk_pin: D3 -# gain: 128 -# update_interval: 3s +# Web server for calibration UI +web_server: + port: 80 + include_internal: true +# Global variables for calibration globals: - id: constant_weight type: float + initial_value: "0.0" + - id: use_calibrated + type: bool + initial_value: "false" + - id: cal_raw1 + type: float + initial_value: "2290200.0" + - id: cal_value1 + type: float + initial_value: "0.0" + - id: cal_raw2 + type: float + initial_value: "2856500.0" + - id: cal_value2 + type: float + initial_value: "50.0" + +# Number inputs for calibration values +number: + - platform: template + name: "Calibration Raw Value 1" + id: cal_raw1_input + min_value: 0 + max_value: 10000000 + step: 1 + mode: box + optimistic: true + initial_value: 2290200 + on_value: + - lambda: |- + id(cal_raw1) = x; + + - platform: template + name: "Calibration Calibrated Value 1" + id: cal_value1_input + min_value: -1000 + max_value: 1000 + step: 0.1 + mode: box + optimistic: true + initial_value: 0 + on_value: + - lambda: |- + id(cal_value1) = x; + + - platform: template + name: "Calibration Raw Value 2" + id: cal_raw2_input + min_value: 0 + max_value: 10000000 + step: 1 + mode: box + optimistic: true + initial_value: 2856500 + on_value: + - lambda: |- + id(cal_raw2) = x; + + - platform: template + name: "Calibration Calibrated Value 2" + id: cal_value2_input + min_value: -1000 + max_value: 1000 + step: 0.1 + mode: box + optimistic: true + initial_value: 50 + on_value: + - lambda: |- + id(cal_value2) = x; + +# Switch to toggle between raw and calibrated output +switch: + - platform: template + name: "Use Calibrated Output" + id: use_calibrated_switch + optimistic: true + restore_mode: RESTORE_DEFAULT_ON + lambda: |- + return id(use_calibrated); + turn_on_action: + - lambda: |- + id(use_calibrated) = true; + turn_off_action: + - lambda: |- + id(use_calibrated) = false; sensor: + # Raw HX711 sensor - platform: hx711 - id: bed + id: bed_raw internal: true dout_pin: D4 clk_pin: D3 update_interval: 3s gain: 128 - filters: - - calibrate_linear: - - 2290200 -> 0 - - 2856500 -> 50 + + # Display raw value + - platform: template + name: "HX711 Raw Value" + id: hx711_raw_display + update_interval: 3s + lambda: "return id(bed_raw).state;" + + # Calibrated sensor using template with dynamic calibration + - platform: template + name: "Bedscale Calibrated" + id: bed_calibrated + icon: mdi:weight-kilogram + update_interval: 3s + unit_of_measurement: kg + accuracy_decimals: 2 + lambda: |- + float raw = id(bed_raw).state; + float raw1 = id(cal_raw1); + float value1 = id(cal_value1); + float raw2 = id(cal_raw2); + float value2 = id(cal_value2); + + // Linear calibration: y = m*x + b + // m = (value2 - value1) / (raw2 - raw1) + // b = value1 - m * raw1 + float m = (raw2 - raw1) != 0.0 ? (value2 - value1) / (raw2 - raw1) : 0.0; + float b = value1 - m * raw1; + float calibrated = m * raw + b; + + return calibrated; on_value: - if: condition: @@ -69,13 +181,20 @@ sensor: then: - lambda: "id(constant_weight) = x;" + # Final output sensor that switches between raw and calibrated - platform: template - name: Bedscale + name: "Bedscale" icon: mdi:weight-kilogram update_interval: 3s unit_of_measurement: kg - lambda: "return id(bed).state;" + accuracy_decimals: 2 + lambda: |- + if (id(use_calibrated)) { + float calibrated = id(bed_calibrated).state; + float adjusted = calibrated - id(constant_weight); + return adjusted >= 10.0 ? adjusted : 0.0; + } else { + return NAN; + } filters: - - lambda: "return x - id(constant_weight);" - - lambda: "return x >= 10.0 ? x : 0;" - delta: 0.5