From f853bd1c30506f25a9999bb42fb2747e35ea12bc Mon Sep 17 00:00:00 2001 From: Tizian Haider Date: Sun, 17 May 2026 16:27:15 +0200 Subject: [PATCH] add global values for per-sensor calibration table; --- esphome_b4_controller.yaml | 61 ++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/esphome_b4_controller.yaml b/esphome_b4_controller.yaml index dd28a15..c9394da 100644 --- a/esphome_b4_controller.yaml +++ b/esphome_b4_controller.yaml @@ -49,6 +49,43 @@ ads1115: i2c_id: bus_ads address: 0x48 +globals: + - id: hb1_wet_voltage + type: float + restore_value: yes + initial_value: '1.20' + - id: hb1_dry_voltage + type: float + restore_value: yes + initial_value: '2.70' + + - id: hb2_wet_voltage + type: float + restore_value: yes + initial_value: '1.20' + - id: hb2_dry_voltage + type: float + restore_value: yes + initial_value: '2.70' + + - id: hb3_wet_voltage + type: float + restore_value: yes + initial_value: '1.20' + - id: hb3_dry_voltage + type: float + restore_value: yes + initial_value: '2.70' + + - id: hb4_wet_voltage + type: float + restore_value: yes + initial_value: '1.20' + - id: hb4_dry_voltage + type: float + restore_value: yes + initial_value: '2.70' + switch: - platform: gpio name: "Bewässerung Hochbeet 1" @@ -206,10 +243,10 @@ sensor: update_interval: 2s icon: "mdi:water-percent" lambda: |- - const float wet_voltage = 1.20; - const float dry_voltage = 2.70; + float wet_voltage = id(hb1_wet_voltage); + float dry_voltage = id(hb1_dry_voltage); float v = id(b4_sensor1).state; - if (isnan(v)) return NAN; + if (isnan(v) || dry_voltage <= wet_voltage) return NAN; float p = (dry_voltage - v) * 100.0f / (dry_voltage - wet_voltage); if (p < 0) p = 0; if (p > 100) p = 100; @@ -225,10 +262,10 @@ sensor: update_interval: 2s icon: "mdi:water-percent" lambda: |- - const float wet_voltage = 1.20; - const float dry_voltage = 2.70; + float wet_voltage = id(hb2_wet_voltage); + float dry_voltage = id(hb2_dry_voltage); float v = id(b4_sensor2).state; - if (isnan(v)) return NAN; + if (isnan(v) || dry_voltage <= wet_voltage) return NAN; float p = (dry_voltage - v) * 100.0f / (dry_voltage - wet_voltage); if (p < 0) p = 0; if (p > 100) p = 100; @@ -244,10 +281,10 @@ sensor: update_interval: 2s icon: "mdi:water-percent" lambda: |- - const float wet_voltage = 1.20; - const float dry_voltage = 2.70; + float wet_voltage = id(hb3_wet_voltage); + float dry_voltage = id(hb3_dry_voltage); float v = id(b4_sensor3).state; - if (isnan(v)) return NAN; + if (isnan(v) || dry_voltage <= wet_voltage) return NAN; float p = (dry_voltage - v) * 100.0f / (dry_voltage - wet_voltage); if (p < 0) p = 0; if (p > 100) p = 100; @@ -263,10 +300,10 @@ sensor: update_interval: 2s icon: "mdi:water-percent" lambda: |- - const float wet_voltage = 1.20; - const float dry_voltage = 2.70; + float wet_voltage = id(hb4_wet_voltage); + float dry_voltage = id(hb4_dry_voltage); float v = id(b4_sensor4).state; - if (isnan(v)) return NAN; + if (isnan(v) || dry_voltage <= wet_voltage) return NAN; float p = (dry_voltage - v) * 100.0f / (dry_voltage - wet_voltage); if (p < 0) p = 0; if (p > 100) p = 100;