Convert gas meter value

Hello everyone,
I have the following situation: I get a current m3 value from the gas meter.
gas1

This is converted into a kWh value using several helpers because I want to enter it under energy.
gas2

Content Template Helper:

{{ states('sensor.gaszahler_aktuell_helper') | float(0) * states('input_number.gas_burning_value') | float(0) * states('input_number.gas_state_number') | float(0) }}

So far everything works and I can include the value of the helper under Energy.
gas3

Now to my problem, usually after restarting the Home Assistant the value is not determined correctly by the Template Helper and shows 0.

So all the statistics are no longer correct. This now shows consumption that is higher then the current gas meter reading.
gas5

Can I change the helper so that it only calculates or changes a value if it is not 0?

It’s likely because alt least one of the sensor you use is not available, yet, so the result is zero due to defaults.

Implement the availability part of the template so that is any of the source sensor is unavailable, the template will be unavailable as well and the value ignored.

@koying

sounds good, but I have no idea how to convert my template sensor.
I have made several attempts to incorporate availability. I can not do it. can you please show me an example?

{{ states('sensor.gaszahler_aktuell_helper') | float(0) * states('input_number.gas_burning_value') | float(0) * states('input_number.gas_state_number') | float(0) }}

EDIT://
Thanks for your answer

This is an example template of mine

    - name: "chambre_temperature"
      unique_id: "chambre_temperature"
      # friendly_name: 'Chambre temperature'
      state: '{{ (states("sensor.temperature_humidity_sensor_0cb4_temperature")|round(1)) }}'
      unit_of_measurement: "°C"
      device_class: temperature
      state_class: measurement
      availability: '{{ states("sensor.temperature_humidity_sensor_0cb4_temperature")|is_number }}'

In your case, all sensors should be available, so something like

availability: "{{ states('sensor.gaszahler_aktuell_helper') |is_number and states('input_number.gas_burning_value') | isnumber and states('input_number.gas_state_number') | isnumber}}"

(although we can probably assume the input_number helpers will always be available)

Hello, thanks. Ive tried to use this Template in my configuration.yaml

#-----------------------------------------------------
# Gaszählersensor von m3 nach kWh
#-----------------------------------------------------

  - template: sensor
    name: "Gaszähler_aktuell_in_kWh"
    unique_id: "Gaszähler_aktuell_in_kWh"
    friendly_name: "Gaszähler aktuell in kWh"
    state: "{{ (states('sensor.gaszahler_aktuell_helper') | float(0) * states('input_number.gas_brennwert') | float(0) * states('input_number.gas_zustandszahl') | float(0)) | round(2) }}"
    unit_of_measurement: "kWh"
    device_class: energy
    state_class: measurement
    availability: "{{ states('sensor.gaszahler_aktuell_helper') |is_number and states('input_number.gas_brennwert') | isnumber and states('input_number.gas_zustandszahl') | isnumber}}"

But receive an Error:

In configuration.yaml, it should be

template:
  - sensor:
    - name: "Gaszähler_aktuell_in_kWh"
[...]

where do you enter your code?

I use, in configuration.yaml

template: !include_dir_merge_list platforms/templates/

Then, every yaml under the templates subdirectory are merged as a list.

My Config now:

includeyaml

And the Error after Restart:

I’m doing something wrong. I’m desperate.

in the templates.yaml, you need

- sensor:
  - name: ...

But don’t use features you don’t understand.

I put above how to insert it directly in configuration.yaml

Thank you very much, now it works!

woks

I only switched to Home Assistant 2 months ago and am currently trying to find my way around. I still have a lot to learn because it is different than my old system. I would like to thank you and all other users who have the patience to help us newbies.