haussteuerung:esphome:gaszaehler
Differences
This shows you the differences between two versions of the page.
| Next revision | Previous revision | ||
| haussteuerung:esphome:gaszaehler [2025/11/02 14:53] – created dominik | haussteuerung:esphome:gaszaehler [2025/11/11 06:57] (current) – [ALT] dominik | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| ====== Gaszähler ====== | ====== Gaszähler ====== | ||
| + | * über Hall Sensor | ||
| + | * https:// | ||
| + | * SS49E.PDF | ||
| + | * https:// | ||
| + | |||
| + | * https:// | ||
| ===== Aufbau ===== | ===== Aufbau ===== | ||
| + | * Sensor : SS49E | ||
| ===== Kalibrieren ===== | ===== Kalibrieren ===== | ||
| - | OLD | + | |
| + | ===== ALT ===== | ||
| <code | download> | <code | download> | ||
| substitutions: | substitutions: | ||
| Line 28: | Line 35: | ||
| status_led: | status_led: | ||
| pin: 32 | pin: 32 | ||
| + | |||
| + | globals: | ||
| + | - id: total_pulses | ||
| + | type: int | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: impuls_factor | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: initial_consumption | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| # Enable Home Assistant API | # Enable Home Assistant API | ||
| Line 33: | Line 54: | ||
| encryption: | encryption: | ||
| key: !secret api_encryption_key | key: !secret api_encryption_key | ||
| - | | + | |
| + | - service: set_initial_gas_reading | ||
| + | variables: | ||
| + | reading: float | ||
| + | then: | ||
| + | - lambda: |- | ||
| + | id(initial_consumption) = reading; | ||
| + | ESP_LOGI(" | ||
| + | - homeassistant.service: | ||
| + | service: persistent_notification.create | ||
| + | data: | ||
| + | title: " | ||
| + | message: "Neuer Startwert: {{ reading }} m³" | ||
| ota: | ota: | ||
| Line 43: | Line 76: | ||
| password: !secret wifi_password | password: !secret wifi_password | ||
| ap: | ap: | ||
| - | ssid: "Schaltschrank | + | ssid: "Gaszaehler |
| password: " | password: " | ||
| #manual_ip: | #manual_ip: | ||
| Line 50: | Line 83: | ||
| # subnet: 255.255.255.0 | # subnet: 255.255.255.0 | ||
| | | ||
| - | globals: | ||
| - | #counter für die impulse des Gaszählers | ||
| - | - id: total_pulses | ||
| - | type: int | ||
| - | restore_value: | ||
| - | initial_value: | ||
| - | |||
| - | # vom Gaszaehler der Faktor pro Impuls; bei mir 1 imp = 0,01m³ Also wieviel m³ pro Impuls berechnet werden soll | ||
| - | - id: impuls_ratio | ||
| - | type: float | ||
| - | restore_value: | ||
| - | initial_value: | ||
| - | |||
| binary_sensor: | binary_sensor: | ||
| - | # Sensor zählt, ob der Reed Kontakt ein Klick erkannt hat. | ||
| - platform: gpio | - platform: gpio | ||
| - | id: internal_pulse_counter | ||
| pin: | pin: | ||
| - | number: | + | number: |
| - | mode: INPUT | + | mode: INPUT_PULLUP |
| - | name: "Gaszaehler_Impuls" | + | inverted: true |
| + | name: "Gaszähler Impuls" | ||
| + | id: gas_pulse | ||
| filters: | filters: | ||
| - | - delayed_on: 100ms # Verringerte Verzögerung | + | - delayed_on: 100ms # Entprellung |
| - | - delayed_off: | + | |
| on_press: | on_press: | ||
| then: | then: | ||
| - | - lambda: id(total_pulses) += 1; # hier zählt er, wenn der Magnet erkannt wird. | + | - lambda: |
| - | | + | |
| - | then: | + | |
| - | - lambda: id(total_pulses) | + | - homeassistant.service: |
| - | # schaut, ob der ESP32 online ist | + | |
| - | - platform: status | + | data: |
| - | name: "Gaszaehler Status" | + | |
| sensor: | sensor: | ||
| Line 95: | Line 114: | ||
| state_class: | state_class: | ||
| icon: mdi:chip | icon: mdi:chip | ||
| - | # zählt alle Impulse hoch und rechnet diese um in m³ | + | # === SENSOR: Verbrauch |
| - platform: template | - platform: template | ||
| - | name: " | + | name: " |
| + | id: gas_m3 | ||
| device_class: | device_class: | ||
| + | state_class: | ||
| unit_of_measurement: | unit_of_measurement: | ||
| - | | + | |
| - | | + | update_interval: |
| + | lambda: |- | ||
| + | return id(initial_consumption) + (id(total_pulses) * id(impuls_factor)); | ||
| + | # === Optional: Energie in kWh (mit Brennwert) === | ||
| + | - platform: template | ||
| + | name: "Gas Energie kWh" | ||
| + | | ||
| + | state_class: total_increasing | ||
| + | unit_of_measurement: | ||
| accuracy_decimals: | accuracy_decimals: | ||
| + | update_interval: | ||
| lambda: |- | lambda: |- | ||
| - | return id(total_pulses) * id(impuls_ratio); | + | |
| - | # | + | float zustandszahl = 0.95; // Aus Rechnung |
| - | # zählt alle Impulse hoch, umrechnung in kWh (mal 10) | + | |
| + | </ | ||
| + | |||
| + | ===== Neu ===== | ||
| + | <code | download> | ||
| + | substitutions: | ||
| + | devicename: " | ||
| + | |||
| + | esphome: | ||
| + | name: iot-kl-wl-sc-gaszaehler | ||
| + | friendly_name: | ||
| + | |||
| + | esp32: | ||
| + | board: esp32-c3-devkitm-1 | ||
| + | framework: | ||
| + | type: esp-idf | ||
| + | |||
| + | |||
| + | preferences: | ||
| + | flash_write_interval: | ||
| + | |||
| + | # Enable logging | ||
| + | logger: | ||
| + | baud_rate: 115200 | ||
| + | level: DEBUG | ||
| + | |||
| + | # Enable Home Assistant API | ||
| + | api: | ||
| + | encryption: | ||
| + | key: !secret api_encryption_key | ||
| + | # services: | ||
| + | # | ||
| + | # variables: | ||
| + | # reading: float | ||
| + | # then: | ||
| + | # - lambda: |- | ||
| + | # id(initial_consumption) = reading; | ||
| + | # ESP_LOGI("calib", "Initialwert gesetzt: %.3f m³", | ||
| + | # - homeassistant.service: | ||
| + | # service: persistent_notification.create | ||
| + | # data: | ||
| + | # title: " | ||
| + | # message: "Neuer Startwert: {{ reading }} m³" | ||
| + | |||
| + | ota: | ||
| + | - platform: esphome | ||
| + | password: !secret ota_password | ||
| + | |||
| + | wifi: | ||
| + | ssid: !secret wifi_ssid | ||
| + | password: !secret wifi_password | ||
| + | ap: | ||
| + | ssid: " | ||
| + | password: " | ||
| + | # | ||
| + | # static_ip: 192.168.30.133 | ||
| + | # gateway: 192.168.30.1 | ||
| + | # subnet: 255.255.255.0 | ||
| + | |||
| + | # Enable Web server | ||
| + | web_server: | ||
| + | port: 80 | ||
| + | version: 3 | ||
| + | |||
| + | button: | ||
| + | - platform: restart | ||
| + | name: " | ||
| + | | ||
| + | icon: " | ||
| + | |||
| + | # DEBUGGING | ||
| + | # https:// | ||
| + | debug: | ||
| + | update_interval: | ||
| + | |||
| + | status_led: | ||
| + | pin: GPIO8 | ||
| + | |||
| + | # Time component to sync with Home Assistant | ||
| + | time: | ||
| + | - platform: homeassistant | ||
| + | id: ha_time | ||
| + | |||
| + | # Define global variables for thresholds (adjust these as needed) | ||
| + | globals: | ||
| + | - id: low_threshold | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: high_threshold | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: impulse_value | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | |||
| + | # Persistent impulse counter | ||
| + | - id: gas_total_impulses | ||
| + | type: int | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | |||
| + | # Period tracking globals (last_period restore no to reinitialize on boot) | ||
| + | - id: last_hour | ||
| + | type: int | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: last_day | ||
| + | type: int | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: last_week | ||
| + | type: int | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: last_month | ||
| + | type: int | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: last_year | ||
| + | type: int | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | |||
| + | # Start totals for each period (restore true to persist values) | ||
| + | - id: hourly_start_total | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: daily_start_total | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: weekly_start_total | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: monthly_start_total | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | - id: yearly_start_total | ||
| + | type: float | ||
| + | restore_value: | ||
| + | initial_value: | ||
| + | |||
| + | # Editable number for initial consumption (persistent, | ||
| + | number: | ||
| - platform: template | - platform: template | ||
| - | name: "Gasverbrauch-kWh" | + | name: "Initial Gas Consumption" |
| - | | + | |
| - | unit_of_measurement: | + | optimistic: true |
| - | | + | restore_value: |
| - | | + | initial_value: |
| + | min_value: 0 | ||
| + | max_value: 1000000 | ||
| + | step: 0.01 | ||
| + | unit_of_measurement: | ||
| + | | ||
| + | | ||
| + | |||
| + | sensor: | ||
| + | - platform: adc | ||
| + | pin: GPIO3 | ||
| + | name: "Hall Sensor ADC" | ||
| + | id: hall_adc | ||
| + | update_interval: | ||
| + | attenuation: | ||
| + | unit_of_measurement: | ||
| accuracy_decimals: | accuracy_decimals: | ||
| + | internal: True | ||
| + | filters: | ||
| + | - median: | ||
| + | window_size: | ||
| + | send_every: 3 | ||
| + | |||
| + | # Template sensor for total impulses (exposes the global) | ||
| + | - platform: template | ||
| + | name: "Total Gas Impulses" | ||
| + | id: gas_total_impulses_sensor | ||
| + | unit_of_measurement: | ||
| + | icon: " | ||
| + | accuracy_decimals: | ||
| + | update_interval: | ||
| lambda: |- | lambda: |- | ||
| - | return id(total_pulses) * id(impuls_ratio) * 10; | + | return id(gas_total_impulses); |
| - | # | + | |
| + | # Template sensor for total consumption | ||
| + | - platform: template | ||
| + | name: "Total Gas Consumption" | ||
| + | id: total_gas_consumption | ||
| + | unit_of_measurement: | ||
| + | icon: " | ||
| + | accuracy_decimals: | ||
| + | update_interval: | ||
| + | lambda: |- | ||
| + | return | ||
| + | |||
| + | # Period checker | ||
| + | - platform: template | ||
| + | name: "Period Checker" | ||
| + | id: period_checker | ||
| + | update_interval: | ||
| + | lambda: |- | ||
| + | auto now = id(ha_time).now(); | ||
| + | if (!now.is_valid()) return NAN; | ||
| + | |||
| + | float current_total = id(total_gas_consumption).state; | ||
| + | |||
| + | // Hourly | ||
| + | int current_hour = now.hour; | ||
| + | if (current_hour != id(last_hour)) { | ||
| + | id(hourly_start_total) = current_total; | ||
| + | id(last_hour) = current_hour; | ||
| + | } | ||
| + | |||
| + | // Daily | ||
| + | int current_day = now.day_of_month; | ||
| + | if (current_day != id(last_day)) { | ||
| + | id(daily_start_total) = current_total; | ||
| + | id(last_day) = current_day; | ||
| + | } | ||
| + | |||
| + | // Weekly (Monday-based week number) | ||
| + | char week_buf[3]; | ||
| + | now.strftime(week_buf, | ||
| + | int current_week = atoi(week_buf); | ||
| + | if (current_week != id(last_week)) { | ||
| + | id(weekly_start_total) = current_total; | ||
| + | id(last_week) = current_week; | ||
| + | } | ||
| + | |||
| + | // Monthly | ||
| + | int current_month = now.month; | ||
| + | if (current_month != id(last_month)) { | ||
| + | id(monthly_start_total) = current_total; | ||
| + | id(last_month) = current_month; | ||
| + | } | ||
| + | |||
| + | // Yearly | ||
| + | int current_year = now.year; | ||
| + | if (current_year != id(last_year)) { | ||
| + | id(yearly_start_total) = current_total; | ||
| + | id(last_year) = current_year; | ||
| + | } | ||
| + | |||
| + | return 0.0; // Dummy value, hide this sensor in HA if possible | ||
| + | |||
| + | # Periodic consumption sensors (update every 60s) | ||
| + | - platform: template | ||
| + | name: "Gas Consumption Hourly" | ||
| + | id: hourly_gas | ||
| + | unit_of_measurement: | ||
| + | icon: " | ||
| + | accuracy_decimals: | ||
| + | update_interval: | ||
| + | lambda: |- | ||
| + | return | ||
| + | |||
| + | - platform: template | ||
| + | name: "Gas Consumption Daily" | ||
| + | id: daily_gas | ||
| + | unit_of_measurement: | ||
| + | icon: " | ||
| + | accuracy_decimals: | ||
| + | update_interval: | ||
| + | lambda: |- | ||
| + | return id(total_gas_consumption).state - id(daily_start_total); | ||
| + | |||
| + | - platform: template | ||
| + | name: "Gas Consumption Weekly" | ||
| + | id: weekly_gas | ||
| + | unit_of_measurement: | ||
| + | icon: " | ||
| + | accuracy_decimals: | ||
| + | update_interval: | ||
| + | lambda: |- | ||
| + | return id(total_gas_consumption).state - id(weekly_start_total); | ||
| + | |||
| + | - platform: template | ||
| + | name: "Gas Consumption Monthly" | ||
| + | id: monthly_gas | ||
| + | unit_of_measurement: | ||
| + | icon: " | ||
| + | accuracy_decimals: | ||
| + | update_interval: | ||
| + | lambda: |- | ||
| + | return id(total_gas_consumption).state - id(monthly_start_total); | ||
| + | |||
| + | - platform: template | ||
| + | name: "Gas Consumption Yearly" | ||
| + | id: yearly_gas | ||
| + | unit_of_measurement: | ||
| + | icon: " | ||
| + | accuracy_decimals: | ||
| + | update_interval: | ||
| + | lambda: |- | ||
| + | return id(total_gas_consumption).state - id(yearly_start_total); | ||
| + | |||
| + | ##### ESP Werte | ||
| + | - platform: wifi_signal | ||
| + | name: "ESP32 WiFi Signal" | ||
| + | id: esp32_wifi_signal | ||
| + | update_interval: | ||
| + | - platform: uptime | ||
| + | name: "ESP32 Uptime (s)" | ||
| + | unit_of_measurement: | ||
| + | update_interval: | ||
| + | icon: mdi: | ||
| + | - platform: internal_temperature | ||
| + | name: "ESP32 CPU Temperatur" | ||
| + | unit_of_measurement: | ||
| + | update_interval: | ||
| + | device_class: | ||
| + | state_class: | ||
| + | icon: mdi:chip | ||
| + | |||
| + | ##### DEBUGGING | ||
| + | - platform: debug | ||
| + | free: | ||
| + | name: "Heap Free" | ||
| + | # | ||
| + | # name: "Heap Fragmentation" | ||
| + | block: | ||
| + | name: "Heap Max Block" | ||
| + | loop_time: | ||
| + | name: "Loop Time" | ||
| + | #psram: | ||
| + | # name: "Free PSRAM" | ||
| + | cpu_frequency: | ||
| + | name: "CPU Frequency" | ||
| + | |||
| + | binary_sensor: | ||
| + | - platform: template | ||
| + | name: "Hall Impulse Detector" | ||
| + | id: hall_detector | ||
| + | device_class: | ||
| + | #internal: True | ||
| + | lambda: |- | ||
| + | static bool last_state = false; | ||
| + | float current_value = id(hall_adc).state; | ||
| + | bool current_state = (current_value > id(high_threshold)); | ||
| + | bool impulse_detected = (!last_state && current_state); | ||
| + | last_state = current_state; | ||
| + | return impulse_detected; | ||
| + | # | ||
| + | filters: | ||
| + | - delayed_off: | ||
| + | on_press: | ||
| + | then: | ||
| + | - lambda: |- | ||
| + | id(gas_total_impulses) += 1; | ||
| + | id(gas_total_impulses_sensor).publish_state(id(gas_total_impulses)); | ||
| + | id(total_gas_consumption).publish_state(id(initial_consumption).state + (id(gas_total_impulses) * id(impulse_value))); | ||
| + | |||
| + | text_sensor: | ||
| + | - platform: uptime | ||
| + | name: "ESP32 Uptime" | ||
| + | format: | ||
| + | separator: " " | ||
| + | days: " | ||
| + | hours: | ||
| + | minutes: | ||
| + | |||
| + | - platform: wifi_info | ||
| + | ip_address: | ||
| + | name: "ESP32 IP Address" | ||
| + | ssid: | ||
| + | name: "ESP32 Connected SSID" | ||
| + | bssid: | ||
| + | name: "ESP32 Connected BSSID" | ||
| + | mac_address: | ||
| + | name: "ESP32 Mac Address" | ||
| + | scan_results: | ||
| + | name: "ESP32 Latest WiFi Scan" | ||
| + | dns_address: | ||
| + | name: "ESP32 DNS Address" | ||
| + | |||
| + | # DEBUGGING | ||
| + | - platform: debug | ||
| + | device: | ||
| + | name: " | ||
| + | reset_reason: | ||
| + | name: "Reset Reason" | ||
| </ | </ | ||
| + | |||
haussteuerung/esphome/gaszaehler.1762091624.txt.gz · Last modified: by dominik
