Hello everyone,
I need your help, as I’ve been searching various forums for several days and simply can’t find a solution.
I have mounted a reed switch with external pull-up on my gas meter using an ESP32. When gas is consumed, a 3.1-second pulse is generated approximately every 14.5 seconds.
This pulse is also visualized with an LED over GPIO33.
The current meter reading is also calculated.
Unfortunately, a few pulses are not counted, so the calculated meter reading deviates downward from the actual meter reading on the gas meter.
Here is my yaml file:
esphome:
name: logomatic
friendly_name: Logomatic
esp32:
board: esp32dev
framework:
type: esp-idf
# Enable logging
logger:
# Enable Home Assistant API
api:
encryption:
key: "1234..."
ota:
- platform: esphome
password: "1234..."
wifi:
ssid: !secret wifi_ssid
password: !secret wifi_password
# Enable fallback hotspot (captive portal) in case wifi connection fails
ap:
ssid: "Logomatic Fallback Hotspot"
password: "1234..."
captive_portal:
uart:
id: uart_bus
tx_pin: GPIO2
rx_pin: GPIO4
baud_rate: 2400
external_components:
- source: github://the78mole/esphome_components@main
components: [ km271_wifi ]
km271_wifi:
- id: budoil
uart_id: uart_bus
switch:
# -------------------------------
# LED für Reedkontakt Impulse
# -------------------------------
- platform: gpio
id: peak_output
name: "Peak Output GPIO33"
pin:
number: GPIO33
mode: OUTPUT
restore_mode: ALWAYS_OFF
# -------------------------------
binary_sensor:
- platform: gpio
id: gas_impuls
name: "Gas Impuls"
pin:
number: GPIO34
mode: INPUT # externes Pull-Up
inverted: True # logisch invertieren
filters:
- delayed_on: 20ms
- delayed_off: 20ms
on_press:
then:
- lambda: |-
id(gas_total_m3).publish_state(id(gas_total_m3).state + 0.01);
# GPIO33 LOW
- switch.turn_on: peak_output
on_release:
then:
# GPIO33 HIGH
- switch.turn_off: peak_output
- platform: template
id: gas_peak_active
name: "Gas Peak aktiv"
device_class: running
lambda: |-
// GPIO33 ist LOW während Peak → invertieren
return id(peak_output).state;
number:
# -------------------------------
# Persistenter Gesamtzähler
# -------------------------------
- platform: template
id: gas_total_m3
name: "Gaszähler Gesamt"
min_value: 0
max_value: 100000
step: 0.01
restore_value: true
optimistic: true
initial_value: 24179.87 # optional Startwert
# -------------------------------
sensor:
# -------------------------------
# Gesamtverbrauch Sensor (Energy Dashboard kompatibel)
# -------------------------------
- platform: template
name: "Gaszähler Gesamt"
unit_of_measurement: "m³"
icon: "mdi:fire"
device_class: gas
state_class: total_increasing
accuracy_decimals: 2
lambda: |-
return id(gas_total_m3).state;
# -------------------------------
# Aktueller Verbrauch m³/h
# -------------------------------
- platform: template
name: "Gasverbrauch m³/h"
unit_of_measurement: "m³/h"
icon: "mdi:fire"
accuracy_decimals: 2
update_interval: 60s
lambda: |-
static float last_value = 0;
static unsigned long last_time = millis();
float current_value = id(gas_total_m3).state;
unsigned long current_time = millis();
float delta_m3 = current_value - last_value;
float delta_hours = (current_time - last_time) / 3600000.0; // ms → h
last_value = current_value;
last_time = current_time;
if (delta_hours > 0) {
return delta_m3 / delta_hours;
} else {
return 0.0;
}
# -------------------------------
Does anyone have any ideas on what I could optimize?