# Calculate Gas Flowrate from meter readings

I’m trying to create a template sensor for gas flowrate, but i don’t know how.

i have a sensor wich updates the gas meter reading in m3 every 5 minutes

what i’m trying to acomplish is

but i don’t now if this is possible?

1 Like

It’s possible but it’s the difficult way. Just use this integration instead:

The derivative of volume with respect to time is flow rate.

Hi Tom,

i’ve tried the ‘easy’ way using derivative, but that doesn’t work correctly.
There is more to the problem…

the sensor is updated every ~10 sec but the value actually only changes every 5 minutes.
using derivative and setting the time_window option to 5 minutes makes it sometime miss the actual change because its not synced to the update frequency of the source sensor.

So actually i’m trying:
(gas meter reading.current - gas meter reading.“last changed value”) * 12 = m3/hour

Did you try setting the time window longer than 5 minutes?

Yes, but because the timespan is not always exactly 5:00 minutes the sensor gives inacurate readings.

@tom_l Could you elaborate on the difficult way?

@mafradon the algorithm you give would fail if there is no gas consumed in the 5 minute interval, it would then skip that value and read the next change, so the interval between reading.current and reading.“last changed value” would be 10 minutes in this case.

I’m struggling with the same issue. I have a smart utility meter reader (this one), which reports every 10 sec or so, but the utility meter only reports a new value every 5 minutes (which may be the same as the previous value when no gas is consumed in that interval)

The derivative integration is not the best approach I think, as setting a longer time_window smooths everything out, which is not what I am looking for. I actually want the difference between two consecutive reports of the original utility meter, which may be 0.

What I can think of is the following:

• wait for a change in value, save the value
• wait for a new change, for at most 5.5 minutes
• if changed, report the change and wait again for a change for at most 5.5 minutes
• if not changed within that 5.5 minutes, report 0 change at the 5 minute mark and wait again for at most 5.5 minutes since that mark

Some questions I have

• is it possible to report a value for half a minute in the past at all?
• what integrations would help with this?

I had the same issue, my workaround was to create a template sensor from the DSMR gas sensor. Because the template sensor only updates on a change in the source sensor the derivative from it can actually see 2 consecutive changing readings.

``````template:
sensor:
- name: "Gas Consumption Total"
unique_id: gas_consumption_total
state: "{{ states('sensor.gas_consumption') | float }}"
device_class: gas
state_class: measurement
unit_of_measurement: 'm³'
sensor:
- platform: derivative
name: gas consumption hourly
source: sensor.gas_consumption_total
unit_time: h
time_window: "00:01:00"
unit: 'm³/h'
round: 3
``````

I’ve not tested this method without a pilot light on at our fireplace, so I can’t say for sure if it works with no consumption at all

I don’t understand this at all. It is just an exact copy of the source sensor.

I’ve only been using HomeAssistant for a few weeks, so I’m certainly no expert. But what I understood during my `InfluxDB` integration is that HomeAssistant might only sends data when the value change. So I assumed it also only writes values to it’s internal `sqlite` database when the data is changed and therefor the template sensor only sees a change when the value from the source is actually different that the previous reading.

But I’ve not looked at the code yet. But when I attached the derivative sensor to the `gas_consumption` (`DSMR` integration) the graph looked strange and continuously went back to 0 since the gas sensor was updated once every 5min instead of the `DSMR` 10sec read interval I suppose.