ESPHome + Pulscounter with IN-Z61 (Gascounter)

I changed to binary_sensor as well. The pulses where count in HA using history_stats.
From that I use template sensor to calculate gas consumption in m³. This sensor I add to the energy dashboard.
Works good for now

cheers

- platform: history_stats
  name: GasVerbrauchPulser
  entity_id: binary_sensor.gasverbrauch_pulser
  state: "on"
  type: count
  start: "{{ now().replace(hour=0, minute=0, second=0) }}"
  end: "{{ now() }}"
template:
  - sensor:
      - name: "gasverbrauchaktuell_m3"
        unit_of_measurement: "m³"
        state: "{{ states('sensor.GasVerbrauchPulser')|float(0) * 0.01 }}"
        device_class: gas
        state_class: total_increasing

Are you shure ?

Bildschirmfoto 2022-03-03 um 07.47.10

I use:

{{ states('sensor.GasVerbrauchPulser')|float(0) * 0.1 }}

I have an BK-G4M and for me 1 imp is 0.01m3.
It looks like the counters are different.

Yes, they are different, my BK-G4 has

1 imp ≙ 0.01m³

I would like to use this, too. I also use an ESP32 but with the IN-Z65. I would like to know how to install this in Home Assistant? Or do i just have to copy the files and folders to the /config/esphome-directory?

My current code works, but the recorded values and the real value on the meter are more and more different while it records.

This is my current Yaml-Code in ESPhome:

esphome:
  name: gassensor

esp32:
  board: esp32dev
  framework:
    type: arduino

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "<OTA_password>"

wifi:
  ssid: !secret wifi_ssid
  password: !secret wifi_password

  # Enable fallback hotspot (captive portal) in case wifi connection fails
  ap:
    ssid: "Gassensor Fallback Hotspot"
    password: "<Fallback_Password>"

captive_portal:

sensor:  
  - platform: pulse_counter
    pin:
      number: GPIO19
      mode: INPUT_PULLDOWN
      inverted: false
    name: "Gasverbrauch"
    update_interval : 15s
    filters:
      - lambda: |-
          static float total_value = 6258.56;
          total_value += x * 0.01;
          return total_value;
    unit_of_measurement: m³
    state_class: total_increasing
    device_class: gas
    accuracy_decimals: 2
    icon: 'mdi:fire'

Wirering:
ESP32 → GPIO19 → IN-Z65 → GND → ESP32
i do not use an hardware-resistor, cause i read that the ESP32 has build-in resistors…right?

This is my first experience with ESPhome and any help would be appreciated. Thank you.

I had no success with the pulse_counter, so use the binary sensor.

See: ESPHome - Home Assistant
Go to integration and add ESPHome

Bildschirmfoto 2022-03-10 um 19.18.56

Bildschirmfoto 2022-03-10 um 19.19.04

Thank you for your answer. i had installed esphome via settings > esphome. it is up and running and i was able to flash my esp32 with the yaml-code i posted.

My Question was how i could install this for my sensor?

Just copy the files and folders to the /config/esphome-directory…

/config/esphome
|-- _static
|   |-- webserver-v1.min.css
|   `-- webserver-v1.min.js
|-- common
|   |-- base.yaml
|   |-- base_global.yaml
|   |-- base_webserver.yaml
|   `-- secrets.yaml
`-- gasmeter.yaml

starte esp-home dashboard

esphome-dashboard=‘cd ~/ && esphome dashboard ./’

Compile and install the app to the ESP

i will try it and give feedback if it works. Thank you so much!

i have an Gasmeter with imp = 0.01 m³ so i will have to change this

substitutions:
  platform: ESP32
  board: esp32dev
  device_name_short: "gasmeter" # used by esp-home config
  friendly_name: "GM2021"
  device_description: "Gasmeter"
  update_interval: 60s
  impulsfactor: "0.100"
  appversion: "1.1.7"

to this

substitutions:
  platform: ESP32
  board: esp32dev
  device_name_short: "gasmeter" # used by esp-home config
  friendly_name: "GM2021"
  device_description: "Gasmeter"
  update_interval: 60s
  impulsfactor: "0.010"
  appversion: "1.1.7"

right?

why are there 2 different values?

globals:
  - id: z61count
    type: int
    restore_value: yes
    initial_value: "0"
  - id: z61counter
    type: float
    restore_value: yes
    initial_value: "0.0"
  - id: m3kw
    type: float
    restore_value: yes
    initial_value: "10.94"
  - id: gas_meter_displayinital
    type: float
    restore_value: yes
    initial_value: "29060.654"  <--- this
  - id: gas_meter_displayvalue
    type: float
    restore_value: yes
    initial_value: "29246.433"   <--- and this
  - id: gas_meter_totalm3
    type: float
    restore_value: yes
    initial_value: "0.0"

gas_meter_displayinital = the intial value on the gasmeter
gas_meter_displayvalue = the current value on the gasmeter

gas_meter_displayvalue = id(gas_meter_displayvalue) += ${impulsfactor};

yes: impulsfactor: “0.010”

IT WORKS! THANK YOU SO MUCH! :slight_smile:

So the “initialvalue” ist my value NOW where i set it up and the “displayvalue” ist the value to put in when i update the configuration via esphome?

1 Like

I’ve just taken this code (thanks) to implemented my own gas sensor. I had to make some minor changes (text translation to English, adjusting impulse factor etc.) and thought it was working fine. Then after a while I noticed the meter display kept ‘drifting’ from the actual reading. At first I thought the sensor was missing pulses so I watched it for while and it didn’t miss a beat. Then I noticed the following in the ESPHome logs.
Gasmeter Display’: Sending state 30500.49805 m³ with 2 decimals of accuracy

The Gasmeter display variable (gas_meter_displayvalue) seems as if its being stored to 5 decimal places. Even more strange is I only set it to 2 decimal places in my code (e.g. 30500.49), only increment by 0.01 (pulse factor) on each pulse so no idea why this is.

When I track the updates to Gasmeter Display in the log file on each update it is incremented by .00997 or 00998 even though I have the pulse factor to 0.01.

This explains why the figure drifts after a while in HA as this only stores/displays to decimal places but I’ve no idea why ESPHOME has it to 5 decimal places and the fractional increments,

Anyone else have this problem ?

The web server settings are a little bit surprising to me. The web page wasn’t loading, so I checked the yaml code.

Web server defined twice
It seems to me like there are two definition of the web server. The first one is used, changed to the webserver.yaml are ignored. I propose to remove the lines in base.yaml

  • common/base.yaml: line 67ff
  • common/base_webserver.yaml: line 1ff

CSS and Javascript causes web page to loop forever
The web site wasn’t loading, to I removed the settings for the local CSS (css_include and css_url) and JS (js_include and js_url) files. Then the web page is displayed.
This might be caused by my ESP8266 instead of an ESP32, but I am wondering why the extra style sheets are required.

The double definition does not cause an error, the last one are always taken.
But it comes down to the version of ESPHome, previous versions had problems with the web server.
With the new version you can do the following:

esphome version
Version: 2022.12.3

## ----------------------------------------------------------------------
## WEBSERVER Settings
## ----------------------------------------------------------------------
web_server:
  port: 80
  version: 2
  local: true

see: Web Server Component — ESPHome

but I am wondering why the extra style sheets are required.

css_include (Optional, local file): Path to local file to be included in web server index page. Contents of this file will be served as /0.css and used as CSS stylesheet by internal webserver. Useful when building device without internet access, where you want to use built-in AP and webserver.

Thank you @petsie for your prompt answer.

It seems to me that the first definition is leading. Nevertheless I would remove one to reduce the complexity.

So I have to enable the local: true setting in order to use the local style sheets? In my setting, the css_include wasn’t enough.

I’m having the exact same problem. I created a minimal working example with only one global total counter which is increased by 0.1 each time.

Interestingly the initial value will already change when performing a firmware update with the exact same configuration. These are the outputs from two different runs:

Sending state 8434.40039 m³ with 2 decimals of accuracy

Sending state 8434.38965 m³ with 2 decimals of accuracy

Here is my config:

# ----------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------
globals:
  - id: gas_totalm3
    type: float
    restore_value: true
    initial_value: '8434.39'


binary_sensor:
  - platform: gpio
    pin:
      number: D4
      mode: INPUT_PULLUP ## set ESP32 pin to input pull-up mode
      inverted: false
    name: ${friendly_name} Gasmeter Z-62 Impuls
    id: gassensor_state
    device_class: window
    filters:
      - delayed_off: 10ms
    ## This automation will be triggered when a button press ends,
    ## or in other words on the falling edge of the signal.
    on_release:
      then:
        - lambda: |-
            ESP_LOGI("main", " -------  Z-62 SET NEW VALUES !!!!!");
            id(gas_totalm3) = id(gas_totalm3) + 0.1;

# ----------------------------------------------------------------
# ALL SENSORS
# ----------------------------------------------------------------
sensor:
  - platform: template
    name: ${friendly_name} Gas total (28820238)
    id: gas_total_m3
    accuracy_decimals: 2
    unit_of_measurement: "m³"
    device_class: gas
    state_class: total_increasing
    lambda: |-
      return id(gas_totalm3);

Both versions work, only with the local: true setting the latest css version is used for each build and stored locally on the device.

With include, this, yes after something has changed, must be adjusted.

I finally found a solution, after finding this thread:

Instead of using a float value to count, I’m now using two individual int values, which I convert to float afterwards. This way, counting will not be affected by float precision.

Here is my MWE:

# ----------------------------------------------------------------
# Global variables
# ----------------------------------------------------------------
globals:
  - id: gas_total_m3_integer_part
    type: int
    restore_value: yes
    initial_value: "8437"
  
  - id: gas_total_m3_fraction_part
    type: int
    restore_value: yes
    initial_value: "22"


binary_sensor:
  - platform: gpio
    pin:
      number: D4
      mode: INPUT_PULLUP ## set ESP32 pin to input pull-up mode
      inverted: false
    name: ${friendly_name} Gasmeter Z-62 Impuls
    id: gassensor_state
    device_class: window
    filters:
      - delayed_off: 10ms
    ## This automation will be triggered when a button press ends,
    ## or in other words on the falling edge of the signal.
    on_release:
      then:
        - lambda: |-
            id(gas_total_m3_fraction_part) += 10; // (Set to impuls_factor * 100, e.g. 0.01 = 1; 0.1 = 10, etc...)
        
            if (id(gas_total_m3_fraction_part) >= 100) {
             ESP_LOGD("main", " *** CARRY OVER DETECTED ... compensating...");
             id(gas_total_m3_integer_part) +=1;
             id(gas_total_m3_fraction_part) -= 100;
            }

            id(gas_total_m3).update();


# ----------------------------------------------------------------
# ALL SENSORS
# ----------------------------------------------------------------
sensor:
  - platform: template
    name: ${friendly_name} Gas total
    id: gas_total_m3
    accuracy_decimals: 2
    unit_of_measurement: "m³"
    device_class: gas
    state_class: total_increasing
    lambda: |-
      return ( id(gas_total_m3_integer_part) + (id(gas_total_m3_fraction_part)*0.01) );