HA requires m3 for Gas - I converted daily kWh reports to total_increasing m³

(Note: this text may need some corrections as I still can not add to consumption to energy - I tried units m3 and m³ - there is no longer an error message for the latter in the log, so I hope it will work out)

Possibly, HA should accept kWh for gas directly and do the “total_increase” itself, but here is how I did it for French gazpar.

Set up a counter which will be the sum of all kWh consumption seen up to current day to provide total_increasing sensor state value later.

counter:
  gas_consumption:
    initial: 0
    restore: true

Energy wants a sensor in m³ for gas. I also created a sensor for kWh, but I am afraid that this will mess up the graphs so I am not using it. I use a fixed conversion of 11.16 down to m³ as seen in my latest invoice.

- platform: template
  sensors:
    gas_consumption_kwh:
      friendly_name: "Gaz (kWh)"
      unit_of_measurement: "kWh"
      value_template: "{{ states('counter.gas_consumption') }}"
    gas_consumption_m3:
      friendly_name: "Gaz (m³)"
      unit_of_measurement: "m³"
      value_template: "{{ states('counter.gas_consumption') | float / 11.16 }}"

I customize the values in ‘customize.yaml’ to add “total_increasing”, the “unit_of_measurement” are likely not usefull and they stem from a test using the counter directly which “Energy” did not accept as it apparently requires a counter.

sensor.gas_consumption_kwh:
  state_class: total_increasing
  unit_of_measurement: kWh
sensor.gas_consumption_m3:
  state_class: total_increasing
  unit_of_measurement: 'm³'

The integration for Gazpar provides daily consumptions, and we need a total_increasing consumption. The following automation makes the sum every morning at 8, supposing that the new daily value is known à at time (with some margin for several trials trying to get it on a bad network). sensor.compteur_gazpar is the daily value provided by the integration :

alias: Accumuler consommation Gazpar
description: Ajoute la consommation du jour précédent au compteur global
trigger:
  - platform: time
    at: '08:00:00'
condition: []
action:
  - service: counter.configure
    target:
      entity_id: counter.gas_consumption
    data:
      value: >-
        {{ states('counter.gas_consumption') | float +
        states('sensor.compteur_gazpar') | float }}
mode: single

customize.yaml is not loaded until you add this configuration.yaml:

homeassistant:
  customize: !include customize.yaml

You probably have to restart HA after these configurations.

Well done, but I understand this will be added in 2021.10.

We’ll see, this method may be useful in other situations.

The automation works, the total was increased at 8 am.

Unfortunately I can’t add the gas consumption to the Energy console anyway. The gas value in “kWh” can be added as “grid”, but the “m³” can’t be added :unamused:

Just found another kind of use for this kind of method: device consumption.

It can be applied to devices controlled by switches that are not reporting consumption. For instance, you have a light that you know consumes 20W when it is on, you could add a counter for it, and add Wh for as long as the light is on.

Maybe it’s coming in a next version of HA, but if not, it could be done based on this. One would have to take into account the on/off state of the device, and update the counter once every minute for instance and at turn-off time ( a formula like (currenttime-max(starttime,startofminute)*power)).

Apparently the right way to do it will be/is to use a state_class “total” and ensure that the “last_reset” value is properly updated: Sensor Entity | Home Assistant Developer Docs

Hi on my side both m3 ( very small 3) and kWh are working.
The current issue is that with kWh, price calculation is absent but this should be corrected in 20121.11 release
And that sum of kWh from grid ( electricity) and kWh from gaz is not made ( no idea if/when it will be corrected )
See my post with my hardware and software configuration
Sorry this is in French but should be self understandable !

Phil

1 Like