Gas meter and the result

Hello. Since Months (!) i try to read my Gas meter. I got a result, so the ESP8266 works. But what I get just doesn’t make sense.

My Configuration:

esphome:
  name: "esp8266-gas-keller"

esp8266:
  board: nodemcuv2

# Enable logging
logger:

# Enable Home Assistant API
api:
  encryption:
    key: "###"

ota:
  password: "###"

# Example configuration entry
wifi:
  ssid: !secret privatewifi_ssid
  password: !secret privatewifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Gas-Fallback Hotspot"
    password: "###"


captive_portal:


globals:
  - id: total_pulses
    type: int
    restore_value: false
    initial_value: '0' # mit diesem Wert startet der Gaszählerstand

  - id: imp_ratio
    type: float
    restore_value: false
    initial_value: '0.01'


binary_sensor:
  - platform: gpio
    id: internal_pulse_counter
    pin:
      number: D1
      mode: INPUT_PULLUP
    name: "Live-Impuls"
    filters:
      - delayed_on: 10ms
    on_press:
      then:
        - lambda: id(total_pulses) += 1;

  - platform: status
    name: "Status Gassensor"

sensor: 
  - platform: template
    name: "Gasvebrauch Puls"
    device_class: gas
    unit_of_measurement: "m³"
    state_class: "total_increasing"
    icon: "mdi:fire"
    accuracy_decimals: 2
    lambda: |-
      return id(total_pulses) * id(imp_ratio);
      ESP_LOGD("Gaszähler Impulse Tag", "Pulse bisher %d", id(total_pulses));

  - platform: template
    name: "Gasverbrauch in kWh"
    device_class: gas
    unit_of_measurement: "kWh"
    state_class: "total_increasing"
    icon: "mdi:fire"
    accuracy_decimals: 2
    lambda: |-
      return id(total_pulses) * id(imp_ratio) * 10;
      ESP_LOGD("Gaszähler kWh Tag", "Pulse bisher %d", id(total_pulses));

The result is “0”. Without exception! And when I look at the source code, I just don’t understand why. Because i can´t finde any error. Whould you please help me? Thanks!

What’s the output of your ESP_LOGD’s (you should put them before the return statements)?
Are those the mysterious “result” you speak about?

1 Like

Cool, thank you! :slight_smile: I learned something. I put the ESP_LOGD in front of the return statement and now it is displayed.

However, the problem still prevails. I have attached a screenshot. The counter just doesn’t increase. Although there is a reaction to the relay responding to the counter’s magnet, nothing is counted. And when I look at the code, I can’t find any error. And if you look at the tutorials online, they only show this solution.

Although I don’t understand what you’re supposed to multiply by 0.01 anyway… If I specify a fixed value, i.e. the counter reading, and then calculate +0.01 for each revolution, then it must be correct… But that is another question.

What kind of physical device are you using to register the pulses? And are you sure the on_press in your binary_sensor is activated? You can also add a LOG_D message to this part of your yaml to see if it is triggered.

Why not using Pulse Meter Sensor — ESPHome, I think this component does everything you want to achieve in your code.

I’ll take a look at this tomorrow, thanks for your help! But now it’s almost 8 p.m. (in Germany) and New Year’s Eve! So have a GREAT evening (later) and a great new year! :slight_smile:

I use a Reed-Contact on D1. Something like this. Well… As you can see in my Screenshot, the sensor seems to work. I become a feedback. The Value is “0”, but there ist any answer. Or am i wrong?

In the log screenshot, I only see the (automatic) update from your template sensors, not the on/off switching of your binary sensor, so I assume there is no increment of pulses and the value stays zero. Did you test your setup before you used in on the gasmeter?