esphome: name: bedscale esp8266: board: d1_mini # Enable logging logger: level: DEBUG # Enable Home Assistant API api: password: "" ota: password: "" platform: esphome wifi: ssid: "ItHurtsWhenIP" password: "BKl@3660" manual_ip: # Set this to the IP of the ESP static_ip: 10.1.0.24 # Set this to the IP address of the router. Often ends with .1 gateway: 10.1.0.1 # The subnet of the network. 255.255.255.0 works for most home networks. subnet: 255.255.255.0 # Enable fallback hotspot (captive portal) in case wifi connection fails ap: ssid: "Bed-Scale Fallback Hotspot" password: "bINLLate9rIA" captive_portal: # 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_raw internal: true dout_pin: D4 clk_pin: D3 update_interval: 3s gain: 128 # 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: - lambda: |- static float last = 0.0; float diff = x - last; last = x; return x < 10.0 && diff < 1.0 && diff > - 1.0; then: - lambda: "id(constant_weight) = x;" # Final output sensor that switches between raw and calibrated - platform: template name: "Bedscale" icon: mdi:weight-kilogram update_interval: 3s unit_of_measurement: kg 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: - delta: 0.5