Gas meter consumption

Hello. I created a sensor to measure gas consumption according to several instructions on the Internet. With the help of a reed switch (magnetic sensor), esp8266 and UPS with measuring the battery charge in the event of a power failure.

What we need:
Required:

  • ESP8266 or ESP32 (I used ESP8266 wemos D1 for)
    -Magnetic sensor (no matter what you use I used the cheapest available on aliexpress.)
  • USB mini connector with charger

Optional:

  • External antenna if necessary and the gas meter is far from the router and the internal antenna does not reach)
  • IPEX to SMA connector (depending on antenna type)
  • RGB LED
  • 1000kohm resistor and 220kohm resistor
  • UPS whit 18650 battery
  • box and cabling

Approach:

  1. Flash ESP8266 to esphome.
  2. Connect reed switch conncet to GPIO12 and GND.
  3. attach the sensor to the gas meter.
    (I tried different positions. The best is more to the right according to the picture)
    I didn’t use a 3D printer but I cut a small module from polystyrene and I mounted a reed switch on it, I connected everything with electrical tape).
  4. Connect rgb led to GPIO5 status led and GPIO13 counter led.
  5. Create battery status whit 1000kohm and 220kohm resistor
    (instructions) .
  6. Connect the ESP to the UPS module (it doesn’t matter which one you use. Important that the output must be 5V or 3.3V for ESP).

ESPHome code: (If you have a suggestion for improving the code then write to me.)

substitutions:
  friendly_name: ESP-Gas
  device_description: "Measure your natural gas consumption with the magnetic pulse of gas meter."
  status_led: GPIO5
  pulse_pin: GPIO12
  pulse_rate: '100' # imp/m3

esphome:
  name: esp-gas
  platform: ESP8266
  board: d1_mini_pro

# Enable logging
logger:

# Enable Home Assistant API
api:

ota:
  password: "...."

wifi:
  ssid: "wifiname"
  password: "wifipass"

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

captive_portal:
# Sensors for ESP WIFI information
# Status LED for connection
status_led:
  pin:
    # Blue LED
    number: ${status_led}

text_sensor:
  - platform: wifi_info
    ip_address:
      name: "${friendly_name} - IP Address"
      icon: mdi:wifi

#binary_sensor:

light:
  - platform: binary
    internal: true
    id: led_red
    name: Red
    output: output_red

output:
  - platform: gpio
    pin:
      number: D7
    id: output_red

sensor:
# Battery status
  - platform: adc
    pin: A0
    name: "ESP-Gas Battery"
    accuracy_decimals: 3
    update_interval: 120s
    filters:
      - multiply: 9.6

# Wifi signal 
  - platform: wifi_signal
    name: "ESP-Gas Signal"
    update_interval: 120s

# Gas meter
  - platform: pulse_meter
    pin:
      number: ${pulse_pin}
      mode: INPUT_PULLUP
    on_value:
      then:
        - light.turn_on:
            id: led_red
        - delay: 0.5s
        - light.turn_off:
            id: led_red
    internal_filter: 400ms
    name: "${friendly_name} - Gas consumption"
    filters:
      - multiply: 0.06
    unit_of_measurement: "mÂł/ min"
    id: gas
    accuracy_decimals: 2
    icon: 'mdi:fire'
    total:
      name: '${friendly_name} - Gas total'
      state_class: total_increasing
      device_class: gas
      unit_of_measurement: "mÂł"
      accuracy_decimals: 2
      filters:
        - lambda: return x * (1.0 / ${pulse_rate});

Picture:

In Home Assistant:

The output of daily monthly and yearly consumption shows me bad value. Can you advise how to produce code for displaying correct values in m3. Well thank you.
EDIT:
(I modified the code (incorrectly entered sensor)
Home Assistant configuration.yaml code:

# Ultility meter m3
utility_meter:
#Gas consumption (day, month, year)
  gas_consumption_daily:
    source: sensor.esp_gas_gas_total
    cycle: daily
  gas_consumption_monthly:
    source: sensor.esp_gas_gas_total
    cycle: monthly
  gas_consumption_yearly:
    source: sensor.esp_gas_gas_total
    cycle: yearly

sensor:
  - platform: template
    sensors:
# Gas consumption (ESP sensor gas total after restart send zero value)
      gas_consumption:
        unit_of_measurement: 'mÂł'
        value_template: "{{ (states('sensor.esp_gas_gas_total') | float * 1) | round(2) }}" 
# Price eur
      gas_month_pric:
        unit_of_measurement: '€'
        value_template: "{{ (states('sensor.gas_consumption_monthly') | float * 0.512) | round(2) }}" 
      gas_year_pric:
        unit_of_measurement: '€'
        value_template: "{{ (states('sensor.gas_consumption_yearly') | float * 0.512) | round(2) }}" 

Other projects:
Energy meter whit zigbee and bluetooth gateway

3 Likes

Hi
What do you mean with “ The output of daily monthly and annual consumption” ?
On my side I use the same protocol on the same ESP32 for gas and Water.
For gas il is a simple 433 “remote control” emitter that sends one 200 ms pulse every 10 liters. This one is perfect
For water it is an inductive sensor that sends a pulse every liter. This one gives a total higher that the sum of pulses. It seems that total increases correctly at each pulse but also increases as well without pulses
Any help …
Phil

I am currently working on water meters, I already have everything equipped with this sensor.

But it also shows me bad values but I think it will be fixed with a better internal_filter value

Can you tell me how you fixed the daily/weekly/monthly sensor? It is not workin for me, which entity have you used there?

Also, can you show me the code for the m3/min?

Thanks, diky! :wink:

For the daily monthly and yearly, I used an entity from the esp script

esp-gas-gas-total

And per m3 / min also from esp script

esp-gas-gas-consumption

The ESP script is at the top of the manual.

How can we modify this when working with LPG tanks to display the % on the tank?

My issue fully explained here : Help an ideas on a Gas tank measument dashboard