Template sensor: calculation often “drops”

`Preformatted text``Hi,

I have a simple template sensor defined as:

‘’’
template:

  • sensor:
    • name: “P1 meter Total Gas kWh”
      unique_id: “sensor.p1_meter_total_gas_kwh”
      icon: mdi:gas-cylinder
      unit_of_measurement: “kWh”
      device_class: energy
      state_class: total_increasing
      state: >
      {% set gasm3 = states(‘sensor.p1_meter_total_gas’) | float %}
      {{ (gasm3 * 10.2346) | round(2, default=0) }}
      ‘’’

But often, the calculated value becomes “0”.
This can have big consequences, see also

Any resolution for this?

Kind regards,
Bart

The first step is to add an availability…

template:
  - sensor:
      - name: "P1 meter Total Gas kWh"
        unique_id: "sensor.p1_meter_total_gas_kwh"
        icon: mdi:gas-cylinder
        unit_of_measurement: "kWh"
        device_class: energy
        state_class: total_increasing
        state: >
          {% set gasm3 = states('sensor.p1_meter_total_gas') | float %}
          {{ (gasm3 * 10.2346) | round(2, default=0) }}
        availability: >
          {{ states('sensor.p1_meter_total_gas') not in ['unknown', 'unavailable', 'None'] }}
2 Likes

Interesting!
Added this to my config. Curious if thus will resolve the issue.

Thanx for the help!
B

Running for one week now without any problems!

Thanx for the solution!

Kind regards
Bart Plessers