Make gas metering easier to add to the Energy dashboard

Hello,

My gas meter counts cubic meters, but I’m billed in kWh, and the gas company uses a kWh/m3 coefficient. This coefficient varies from time to time due to factors like pressure, gas type, and so on.

In HomeAssistant’s Energy dashboard, we can add gas consumption using a (k)Wh or m3 or ft3 sensor, but I cannot add my cubic-meter-based sensor directly to it and provide a price per m3, as I’m billed in kWh. I think it’s the same in quite a few countries.

I created a sensor.gas_kwh_per_cubic_meter that fetches the up-to-date conversion coefficient from my gas provider’s website to do the conversion, and then imagined to create a template sensor that takes {{ sensor.gas_cubic_meters * sensor.gas_kwh_per_cubic_meter }} as a value.

However, this is naive, as it would apply a new coefficient to the whole index. If my gas meter reads 1000 cubic meters and the coefficient goes from 11 to 10, the kWh sensor would drop from 11000kWh to 10000kWh.

Home Assistant is configurable enough, so I found a workaround using a trigger. But it would be most user-friendly to accept cubic-meter based sensors in the energy dashboard, and add a setting for the conversion coefficient, in the same way as there is one to derive the price from the energy (either using a fixed factor, or an entity).

For what it’s worth, this is how I implemented the kWh-based metering:

- trigger:
  - platform: state
    entity_id: sensor.gas_cubic_meters
  sensor:
    name: gas_kwh
    state: "{{ states('sensor.gas_kwh') | float(0) + ((trigger.to_state.state | float - trigger.from_state.state | float) | float * states('sensor.gas_kwh_per_cubic_meter') | float) }}"
    unit_of_measurement: kWh
    device_class: energy
    state_class: total_increasing

Thanks!
Colin