Virtual Gas Consumption Meter Confirguration Help

Hello everyone,

Hoping someone can help me resolve sensor / configuration setup puzzle that I’ve been working on. I ask for your pardon since my grasp on how templates work and how data states are recognized within HA is shaky at best.

Problem
I have a manual gas meter that doesn’t have any of the cool ways to read data from it (someone has to do a manual read every cycle), but I would like to set up a virtual gas meter based on measured consumption rates from two of my major appliances that I can know the states of within HASS: My HVAC furnace which is driven by a Nest thermostat, and my fireplace which I control with a custom controller built with ESPhome. Using the device states and known information about consumption rates, I’d like to calculate the usage and feed this into the energy panel.

Specifics about the devices
For the HVAC, this uses a consistent rate which I have calculated to be approximately 1.42857144 ft³ / minute or 85.7142864 ft³ / hour. To this effect, I found this post somewhat helpful but wondered if the solution was as elegant as it could be considering it needed three separate entities to do the job. It also only focuses on one device, where I’m trying to combine at least two with different math.

For the fireplace, things are a little more complicated because the consumption rate adjusts in a linear fashion based on how open the valve feed is, which I can read as a number from 0 (pilot) to 10 (full blast). With the valve open all the way, it’s basically 30 ft³ / hour and decreases linearly as you close each step. If the valve is set to 5, the flow is 50% of max. So my math says [30 ft³/hour * (valve value * 0.1)].

Why I need help?
While I can appreciate that any problem has a number of potential solutions, I’m getting a little lost in my own head about how to go about this as well as failing to understand how to pass information templates and effectively use things like [total_increasing]. I don’t quite understand how to properly trigger a sensor to ‘pulse’ a usage number into another sensor and integrate those values over time in a way that the gas consumption feature in the energy panel can understand.

So far, I only have

- platform: template  
  sensors:
    total_natural_gas_usage:
      unit_of_measurement: "ft³"
      value_template: "{{ states('sensor.current_natural_gas_usage') | float }}"
      device_class: gas
      attribute_templates:
        state_class: total_increasing

…which seems to load into the Energy Panel without issue, but the values being generated by…

    - name: current_natural_gas_usage
      unit_of_measurement: "ft³/s"
      device_class: gas
      icon: mdi-gas-cylinder
      state_class: measurement
      state: >
        {% if DEVICE_IS_ON_AND_CONSUMING %}
          {{ DO_MATH }}
        {% endif %}

… and while this makes the Gas Monitor do things, the numbers are clearly not right.

At this point I’m questioning if I’m going about this all wrong and before I spin my wheels on it for days and days, I figured I’d ask if there was a straightforward way and get a hint on where to go next. I considered doing things in NodeRed or MQTT but maybe that complicates things? I don’t know…

To that end, any guidance or if anyone has solved this is different ways, I’d love to compare notes and would appreciate the engagement!

Why are you doing this in cu ft? 1 therm is 100,000 BTU’s. For natural gas 1 therm = 100 cu ft of distribution line quality gas (majority of liquids processed out). So your Heater is burning 85,714.3 btu/hr. Your fireplace is burning 0 to 30,000 btu/hr.

Gas is sold by MMBTU (million btu) in the US(NYMEX and henry hub). Therms from most gas utilities. It’s just easier to keep in the sale units in my opinion.

I would think a trigger template would be easiest for the heater and fireplace. Once a day for heater and after each on/off valve change cycle for the fireplace.

Hope this helps

Something else to consider…
Since your HVAC and fireplace are being measured in energy per unit-of-time (ft³/hr, ft³/s), and you want to use the Energy Panel which likes to display energy used (ft³), and it displays energy used over each clock hour interval, which means you would like to get energy samples every hour or less, you may want to first try using the Rieman sensor. I have not used this, but I’ve seen many posts on this Forum concerning conversion of kW to kWh and recommendations were to use this sensor.

If that works, you could then create a state-based template to add the two rieman sensors together.

Thanks for the tips everyone.

RE: BTUs, well noted and that’s where I will eventually go, because as you say, that’s how I’m billed. But my meter is in CU FT so that’s where I started. The conversion isn’t so tricky, it’s getting HA to move the way I’d like that I’m getting tripped up on.

RE: using an Rieman sensor, I actually have that working on my energy meter successfully doing exactly what was pointed out: converting kW to kWh. It works perfect … on that situation. I tried to replicate those sensors on the gas side and it just doesn’t behave the same way and the numbers that come out seem wrong.

Here’s one of my kWh sensors. This works fine in the energy panel

- platform: integration
  name: Grid Import Energy
  source: sensor.grid_import_power
  unit_prefix: k
  unit_time: h
  method: left

the grid_import_power sensor is as follows:

    - name: "Grid Import Power"
      state_class: measurement
      icon: mdi:transmission-tower
      unit_of_measurement: W
      device_class: power
      state: >
        {{ [0, states('sensor.CURRENT_POWER_USE) | int - states('sensor.CURRENT_POWER_MAKE') | int ] | max }}

The two power use/make values are provided by my power consumption monitor integration. Now I will admit that I don’t actually understand what the YAML for the state is actually saying other than it takes the difference of the two values. I clipped this from another thread and it worked.

But trying to mirror this, I’ve made the following for the gas:

- platform: integration
  name: Gas Usage
  source: sensor.current_natural_gas_usage
  unit_time: h
  method: left

Below is current_natural_gas_usage, as mentioned in the OP with the math in it. However, the above sensor doesn’t show up in the energy panel for selection, because it doesn’t appear as an entity (presumably because it never actually generates a real value?).

    - name: current_natural_gas_usage
      unit_of_measurement: "ft³/h"
      device_class: gas
      icon: mdi-gas-cylinder
      state_class: measurement
      state: >
        {% set total = 0 %}
        {% if is_state_attr('climate.thermostat', 'hvac_action', 'heating') %}
          {% set total = total + 85.7142864 %}
        {% endif %}
        {% if is_state('switch.fireplace_ignition', 'on') %}
          {% set total = total + 30 * (states('number.flame_control') | float * 0.1) %}
        {% endif %}
        {{ total }}

Again, I appreciate everyone throwing in their ideas or comments.

These are two distinct things. Not every entity is available for selection in the energy dashboard. One of the conditions is that it needs to have the proper state_class.

I have something very similar:
I do some maths to figure out the gas flow from “power” reported by my boiler. You don’t need to worry about the state template, yours will be different.

  - sensor:
      - name: Boiler gas flow
        unit_of_measurement: "m³/h"
        state: >-
          {% set power = states('sensor.boiler_power') | float(0) %}
          {# 16 % is minimum power #}
          {% set p1 = 16 %}  {# % #}
          {% set c1 = 0.0455625 %}  {# m^3 / h / % #}
          {# 80 % is maximum power #}
          {% set p2 = 80 %}  {# % #}
          {% set c2 = 0.0333375 %}  {# m^3 / h / % #}
          {# assume it's a linear function #}
          {% set a = (c2 - c1) / (p2 - p1) %}
          {% set b = (c1 - a*p1) %}
          {{ (power * (a*power + b)) | round(5) }}

Then, I integrate the value to get the amount of consumed gas:

sensor:
  - platform: integration
    source: sensor.boiler_gas_flow
    method: left
    name: Boiler gas flow integral

However, this sensor then has an incorrect unit of measurement:

state_class: total
source: sensor.kotlik_gas_flow
unit_of_measurement: m³/hh
icon: mdi:chart-histogram
friendly_name: Boiler gas flow integral

Open your Home Assistant instance and show your state developer tools.
So I use a template sensor to fix it, as well as set the correct device class and state class:

tempate:
  - sensor:
      - name: KOTLIK gas consumed
        state: "{{ states('sensor.boiler_gas_flow_integral') }}"
        unit_of_measurement: "m³"
        device_class: gas
        state_class: total_increasing

One thing you should realize about integration sensors is that it’s state is only updated on state changes of the source sensor. Keep that in mind when testing.

1 Like

I wasn’t able to put any time against this today, but just glancing through the above… I think you’ve connected all the parts for me. Noted on the integral update, that’s is probably key because since my gas usage is generally just a solid number (because the only state changes are ‘on’, ‘off’, and when I adjust the flame. I’ll need to think on that and I’ll get these a try when I have a chance!

Hi, here’s where I am so far.

After checking what I had against Ondra’s information, I realized I was actually really close and I just needed to move one of my items from my sensor config with the template platform to my template config as a sensor. I also had some various settings that needed cleaning up and as of this writing, it seems to be working. Basically what Ondra had above but with my math.

Now, the next item is the thing that was pointed out about the integration sensor not updating except on state change. Since my fireplace can run for two or three hours on the same setting, the state is the same and this causes the usage to not be tracked in real time and the final value to ‘hit the books’ when I finally turn it off. Not really good for visualization.

I tried to think a way to ‘kick’ the sensor regularly to have it update more frequently. After playing around with some ideas, I landed on adding a ‘jitter’ to the sensor. To do so, I added in the time_date integration and tie that to the source sensor so it would at least re-render every minute. From there, I basically mod now().minute and when it’s an odd minute, I add 0.1, and when it’s even, I take it away.

      state: >
        {% set total = 0 %}
        {% if now().minute % 2 + states('sensor.time') | int == 1 %}
            {% set tick = 0.1 %}
        {% else %}
            {% set tick = -0.1 %}
        {% endif %}
           [total = USAGE MATH]
        {{ total + tick | round(5) }}

The result is essentially what looks like it will be a much smoother climbing gas usage graph that renders every minute with a very small jitter when all appliances are idle. I’ll watch it for a few days and report back but this looks promising!

In my opinion, it would be better to only change the visualization instead of vandalizing the actual data (and saving many more states in the database).
With apexcharts-card, you can set curve to straight instead of stepline. It won’t fix the most recent run because the data point is not there yet, but the history should look right.

While the visualization adjustment isn’t a bad idea, it still would cause issues when the data renders in the energy panel, and I’m just sort of a sucker for that realtime data. That said, you are correct about filtering out unnecessarily database records, so I changed things so if nothing is running, there’s no need to record jitter (just register a 0 when everything is off).

After a day, I’m getting nice clean data!
image

The only last items are getting my conversion sensors working (therms and CU M) and getting the number to render right in the energy panel (it says CU FT as the unit but the numbers seem like they are displaying CU M). But for the time being, the core of my request is solved.

Thanks for everyone’s help!

Seems like you have a good working solution.

Another way I came at this was to use Powercalc - Virtual power sensors, with the fixed mode keyed to your monitoring sensors.

Are you letting powercalc calculate your furnace usage in “W” in name only, when it will really be in another unit to calculate your gas usage? Can you show an example of how you configured this with Powercalc?