Template sensors are temporarily unavailable when starting Home Assistant

Hi

I’ve create a template sensor to be used as input for the gas consumption in the Energy dashboard. This is the code:

template:
    sensor: 
    - name: "Gas Meter"
      unique_id: gas_meter
      state: "{{ (states('counter.gas_counter') | float(default=0) * 0.01) | round(2) }}"
      unit_of_measurement: "m³"
      icon: "mdi:fire"
      device_class: gas
      state_class: total_increasing

It works perfectly except when restarting Home Assistant. The template sensor temporarily gets the state of unavailable during 0.015 seconds. This is the state of the sensor during startup:

|sensor.gas_meter|unavailable|2024-12-30T13:54:14.445Z|
|sensor.gas_meter|8355.48|2024-12-30T13:54:14.451Z|

I guess that this unavailable state is causing problems to the Energy dashboard because the gas consumption gets very weird values.

I’ve tested to set the state of the template sensor to 0 and it still gives me the state of unavailable during startup of Home Assistant. I guess, this is something related to the initialization process of Home Assistant.

I’ve checked the rest of the template sensors and all the template sensors have the state unavailable at startup during 0.015 seconds.

Does anybody have the same problem with template sensors? Has anybody solved the problem with the unavailable state of the entity configured in the gas consumption of the Energy dashboard?

Thanks
Xavi

template:
  - sensor: 
      - name: "Gas Meter"
        unique_id: gas_meter
        state: "{{ (states('counter.gas_counter') | float(0) * 0.01) | round(2) }}"
        unit_of_measurement: "m³"
        icon: "mdi:fire"
        device_class: gas
        state_class: total_increasing
        availability: "{{ has_value('counter.gas_counter') }}"

hi tom

thank you for your answer. I’ve used your code and the behavior is the same. The state of the template sensor is unavailable at startup during few microseconds:

|sensor.gas_meter|unavailable|2024-12-30T14:32:48.377Z|
|sensor.gas_meter|8355.52|2024-12-30T14:32:48.385Z|

I think that when the state is unavailable at startup, the code of the template sensor is not executed. Then, it doesn’t matter what I put in the state or the availability parameters of the template sensor. After few microseconds, the template sensor is executed correctly. Am I right?

Every entity is marked unavailable at startup until its integration receives or restores a value. There is nothing you can do about this. It is how home assistant works.

The availability template will prevent this template sensor from rendering as zero (your default for the counter). It will remain unavailable until a valid value is received.

The unavailable state wasn’t messing with your graph, the default zero value was.

0 → some value = add that whole value to the total
Unavailable → some value = do nothing.

I have seen this issue more times than I can count. Trust me the availability template will fix your strange readings after a restart. That’s why I wrote the Community Guide.

Thank you for clarifying this! I hadn’t considered how the default zero value was impacting the total calculations and graphs.

I’ve implemented the availability template as you suggested to avoid these issues after a restart.

1 Like