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
  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 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.

Permalink


2 comments

avatar
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
avatar
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.

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