How to Integrate a Gas Meter to Home Assistant

This is a low-cost solution for tracking home gas consumption in Home Assistant. Ideal for those who want to drill down into their consumption of gas but their utility provider does not offer smart metering. 


The solution works with those residential diaphragm gas meters that come 'smart ready'. The gas utility company providing service in my home installed the model
Itron-Flogiston G4 RF1 which is a typical example of these devices. According to the specification the device is equipped with a totalizer that includes a "pulse generator (option)... with a permanent magnet in counter roller. Can be refitted with a pulse transmitter (Reed switch)."  
The optional Reed switch based pulse transmitter is available both from the manufacturer of the gas meter and also from others for a fairly high price. I decided to go on a low cost route.

While I am aware of Reed-swich based DIY solutions to this problem, I finally ended up using a Hall-sensor which is an alternative way of detecting magnetic field. For measuring impulse a Hall sensor is perhaps not as accurate as a Reed switch, but still my current solution is far smarter than my first attempt that was based on Google's Vision AI optical recognition. However the overall purpose remained the same: to better understand the dynamics of the gas consumption of my DIY smart heating control.

I had to answer the following challenges:

  • Reading the magnetic impluses of the totalizer.
  • Converting the data and storing it in Home Assistant.
  • Visualization, analysis

Reading the magnetic impulses of the totalizer

Gas meter
The Hall-sensor module (KY-024) is connected to a NodeMcu ESP8266 development board powered over USB. Luckily in my home there is an outlet quite close to the gas meter thus power source was not an issue. This KY-024 sensor module offers both digital and analogue output, however after a couple of experiments I decided to use the analogue source connected to the Analogue PIN of the development board. 

Similar attempts show that the method does not neccesarily work in all cases, but it worked for me with a total hardware investment under $5.

Converting the data and storing it in Home Assistant

The NodeMcu microcontroller was installed with ESPHome firmware so that I could make use of its feature rich integration to Home Assistant. (ESPHome is an open source project by Nabu Casa, the team behind Home Assistant.) 

The configuration file of ESPHome allows the setup of reading and transmitting analogue sensor data without any coding. Let me copy here only that part of the firmware configuration that covers the sensor reading:

sensor:
    - platform: adc
         pin: A0
         name: "Hallsensor"
         update_interval: 1s
         raw: True
         unit_of_measurement: "value"
         accuracy_decimals: 0

The above configuration invokes the analogue-to-digital converter of ESPHome to measure voltage level on GPIO PIN A0 at every second and transfers it HomeAssistant without any conversion using the friendly name "Hallsensor".

The ESPHome integration creates a HomeAssistant entity for this data. The number of pulses is counted as a counter defined in Home Assistant counter helper with the current total usage set as the 'initial value' of the counter. A simple HomeAssistant automation is triggered if the raw voltage data goes below a certain threshold value. The trigger calls the service counter.increment.

alias: Gas meter
description: ""
trigger:
  - platform: numeric_state
    entity_id:
      - sensor.esphome_web_cf8530_hallsensor
    below: 810
condition: []
action:
  - service: counter.increment
    target:
      entity_id: counter.gas_counter
    data: {}
mode: single

The threshold value was defined by analysing the patterns in the ESPHome logs. Most probably this depends on the sensitivity setting of the Hall-sensor. (The KY-024 sensor board includes a potentiometer for setting the sensitivity.)   

Visualization

In order to compute the total and daily gas consumption from the raw data of the impulse counter a virtual sensor must be created in HomeAssistant. HomeAssistant provides a dedicated helper wizard for creating such sensors, the Utility Meter integration. As I want to track both the total status and the daily change, I created two helpers. The template section of the helper is used to configure the computation:

As in my gas meter each impulse equals to 0.01 cubic meter of consumed gas the actual cubic meter total requires a simple calculation. In HomeAssistant terms this calculation is a function as a template:

{{ states('counter.gas_counter')|float*0.01}} 

At this point HomeAssistant starts collecting data of the gas usage and a couple of days later it is available for presenting on the dashboard. My heating system has three circuits, each having its own circulation pump also controlled by HomeAssistant. The hourly statistics graph card gives an indication of the share of each circuit within the total gas consumption. (The consumption outside of the heating periods is generated by the gas boiler.) 

Comparing the daily usage to the mean outdoor temperature one can track on a daily level the impact of the outdoor temperature on the total gas consumption.  

Summary: setup step-by-step

  1. Connect the KY-024 sensor with the ESP8266: 3V3, GND and AO PINS.
  2. Install ESPHome to Home Assistant.
  3. Setup and install the ESPHome firmware to the microcontroller.
  4. Define a counter helper in HomeAssistant.
  5. Create an automation triggered by the ESPHome counter. 
  6. The automation calls counter increment service.
  7. Define virtual sensors with the utility meter integration.
  8. Add a template to compute the cubic meter value.
  9. Start collecting data.
  10. Define hourly, daily, weekly dashboard charts and analyse.

Next steps

The current setup does not track the impulses of the totalizer with 100% accuracy even after a longer finetuning of the threshold value of the analogue to digital conversion. This is perhaps due to the fact that the conversion is happening in the HomeAssistant server instead of following the 'IoT edge' concept and running it in the microcontroller. I will dive deep into the built-in templating options of ESPHome - the Lambdas - and will try to keep the conversion within the IoT device. This will of course make the above steps no.4 to no.7 needless.