Adding Gas Meter Energy logging to Home Assistant

Adding new MQTT sensors to Home Assistant to log Gas meter usage data

Date 03 September 2021
Views 25,256
Time to read 3 minutes read

With the release of the 2021.9 of Home Assistant, the software can now show your gas usage on the new Energy screen in addition to mains consumption and solar electric production. The new update also added battery storage to the system.

There seems to be a lot of confusion online about how to add the new gas data to the Energy screens and so below is the code I have used and is currently working on my Home Assistant install.

Gas Meter pulse sensor
The gas Meter pulse sensor

Our gas meter is monitored via a Gas Meter Hall Effect Sensor which sends pulses to a datalogger ESP8266 Mains Energy Monitor which in turn sends the data to our MQTT server.

The current documentation on the Home Assistant website home-assistant.io/docs/energy/gas/ doesn’t show any examples of the code needed and only refers to using third-party P1 readers.

I have my Home Assistant config files separated into subfolders and in my sensors.yaml file I have the following to retrieve the gas meter number from the MQTT server:

- platform: mqtt
  state_topic: "/home/power/gasmeter"
  name: "Gas Meter"

To be able to access the data with the new Energy screen the data needs additional attributes, and these are added using a template sensor:

- platform: template
  sensors:
    gastocubicmeter:
      value_template: "{{ states('sensor.gas_meter') | float / 100 }}"
      unit_of_measurement: m³
      device_class: gas
      attribute_templates:
        state_class: total_increasing

For the sensor to be used by the Energy screen it needs the unit of measurement attribute to be in cubic meters, the device_class needs to be "gas" and the state class needs to be "total_increasing" I tried other values on the Home Assistant documentation for these attributes, but they did not work.

Our gas meter records one pulse for every 0.01 of a cubic meter of gas used and so we need to divide the gas meter reading by 100 to give the correct cubic meters used of gas. This is done in the value_template attribute.

Energy Config Page
Energy Config Page
Adding new sensor to Home Assistant
Adding new sensor to Home Assistant
Energy page with gas data
Energy page with gas data

With the new template sensor added you need to restart Home Assistant and then in the Energy settings page, select the new sensor on the Energy config screen at /config/energy

On the Energy screen, the new graph for Gas Consumption will appear with a message saying: "There is no data to show. It can take up to 2 hours for new data to arrive after you configure your energy dashboard."

After a few hours, your gas consumption should appear on the graph.

9 Comments

  • User Image

    H Luettich

    18 September 2021 at 6:16 pm

    Hi Brian, nice Example but some Beginner Questions:
    do you have a line sensors ! include sensors.yaml in configuration.yaml ?
    in wich File is the template defined ?
    is it possible to use a reed contact via gpio as
    binary_sensor:
    platform: rpi_gpio
    ports:
    3: gas_meter
    invert_logic: true
    pull_mode: up

    Greetings Holger

    • User Image

      Brian

      18 September 2021 at 7:43 pm

      Hi Holger, thank you for your comments . I am including the sensor folder with this line in configuration.yaml :

      sensor: !include_dir_merge_list sensors/

      In the sensors folder I have a file called 2template.yaml which contains the template sensor:
      - platform: template
      sensors:
      energy_gas_in:
      value_template: "{{ (states('sensor.gastocubicmeter') | float)}}"
      unit_of_measurement: "kWh"
      friendly_name: "Incoming Gas"
      attribute_templates:
      state_class: total_increasing
      device_class: energy

      Using a reed switch should work as a binary sensor, you may need a pullup resistor to keep the pin high using something like a 10K value and have the reed switch go to ground to pull it low when activated.

  • User Image

    Christian

    30 September 2021 at 3:22 pm

    Hi, Thanks for that proposal. I m a newbie at HA und tried to build it also for my Smarthome. Im struggling with some things. Maybe you could build up picture of your configuration folder from HA, or show me how it look like if I do not separate the sensor in an extra file.
    sensor: !include_dir_merge_list sensors/. is not working in my application. It is always underlined red in visual studio code. The configuration checker will not stop when I check the configuration. Maybe you can give me a hint. Thanks a lot.
    Cheers
    Christian

    • User Image

      Brian

      30 September 2021 at 3:30 pm

      Hi Christian

      If you have the default setup on Home Assistant you can add the sensors into the main configuration file. I have uploaded my config to https://github.com/briandorey/MyHomeAssistantConfiguration for you to view.

      I hope this helps to get your setup working.

      Brian

  • User Image

    David

    11 July 2022 at 2:40 pm

    Hello Brian,
    there is an even easier way how to achieve this. It isn't necessary to buy the official sensor and connect that through an ESP chip. There is just a magnet inside the gas meter so you can use almost any magnetic sensors which is used for checking doors or windows opening. :)

    Like here https://medium.com/p/8f659df5fdb2 - that's based on a ZigBee magnetic sensor. :)

    David

    • User Image

      Brian

      11 July 2022 at 4:41 pm

      hello David

      We would use a Zigbee sensor if we started with the data logging now. When we built the ESP8266 Mains Energy Monitor 8 years ago the Zigbee devices were not available.

  • User Image

    david

    19 July 2022 at 4:20 am

    hi,

    can i pay you to build me one?

  • User Image

    david

    19 July 2022 at 4:41 am

    so how does a pulse sensor work? does it just grab future values or everything used? could this work in the same way? https://www.ecodhome.com/en/shop-online/smart-home-myvirtuoso-home/magnetic-optical-meter-for-gas-electric-water-meters-detail.html

    • User Image

      Brian

      19 July 2022 at 8:59 am

      The ecodhome device should work in the same way. It only counts the pulses from the electric and gas meters and increases an internal counter.


Leave a comment

Your email address will not be published.

Please enter the text from the image below to prove you are a human
Verify you are human

Share

Share to Bluesky

Share to X / Twitter

Share to Facebook

Share to linkedin

Share to Pinterest

Copy Link