Electronics Home Automation

Adding Gas Meter Energy logging to Home Assistant

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

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 for 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
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 sub folders 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
      value_template: "{{ states('sensor.gas_meter') | float / 100 }}"
      unit_of_measurement: m³
      device_class: gas
        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 the 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.



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
platform: rpi_gpio
3: gas_meter
invert_logic: true
pull_mode: up

Greetings Holger
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
value_template: "{{ (states('sensor.gastocubicmeter') | float)}}"
unit_of_measurement: "kWh"
friendly_name: "Incoming Gas"
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.
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.
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.


Leave a reply

Your email address will not be published. Required fields are marked *

Share this post

Support the blog

AB Electronics Raspberry Pi shop