Why an availability template is important for energy template sensors

removed as bad advice.

21 Likes
The Home Assistant Cookbook - Index
Energy dashboard becoming more and more unreliable
Template sensors don't appear
Energy Dashboard Solar Consumption
Individual devices energy values are way too high on default energy dashboard
Summation of sensors
Get the value of current month energy consumed from PV
Providing a value for "Return to Grid" in the Energy dashboard
Home Assistant Cookbook - Discussion Thread
No zero values in statistics
Template sensors are temporarily unavailable when starting Home Assistant
Need to send initial data on startup and then only upon meeting a threshold
WTH isn't the "adjust a statistic" developer tool easier to use
WTH doesn't Home Assistant stop data glitches at startup or reboot
Energy directly used from solar
Tracking down Energy Spikes
How to read a template in yaml / the why and how of all those delimiters
Configure total increasing template to avoid "float got invalid input" template error
Template Error Sensor in configuration.yaml
Remove a glitched day from Energy?
Having troubles adding 2 sensor values together in a sensor. it buggers up my other sensors
Syntax for if else template statement in a file
Issue with Utility Meter after upgrade to 2025.07.2
Store entity with its attributes
After update 2025.4.0 I do receive a state_class error for template sensor
Tasmota sensor values set back to 0 randomly
Looking to add & write to a new database table from within HA
Power sensor template without negativ power
Sensor spikes coinciding with MQTT connection loss
Energy dashboard shows cumulative values iso today's after every restart (2026.7.0)
How to prevent a trigger template sensor to unavailable
Template sensor with numeric unit still crashes on `{{ none }}` – issue #93386 still relevant
Home Assistant Cookbook - Discussion Thread
Energy Dashboard shows random high costs after reboot
Home Assistant Cookbook - Discussion Thread
Creating a Sensor
Sensor template availability
Energy tab showing nonsense
Utility meter adding to total after HA restart (sometimes)
Utility meter readings jump unexpectedly and rise at a greater rate than expected
[SOLVED] How to ā€žsecureā€œ a template against failures?
Filtering utility meter
No enitie value during start
Logger: "unsupported operand type(s) for +=: 'float' and 'datetime.timedelta'" when reloading template sensors (ALL template sensors broken!)
Issues with an increasing template sensor
Freint Electricity Meter Interface (Develco ZHEMI101)
Displaying combined sensors
Problem with utility meter template (for Emporia Vue data)

Here is an alternative Availability Sensor method for your consideration.

thanks for this, we should point to this wherever the question/issue arises… and it does a lot.

btw, here’s my example for calculating the unaccounted energy daily:

      - unique_id: ongemeten_energie_per_dag
        state: >
          {{states('sensor.energy_consumed_daily')|float(0) -
            states('sensor.gemeten_energie_per_dag')|float(0)}}
        <<: &energy_total
          unit_of_measurement: kWh
          device_class: energy
          state_class: total
        availability: >
          {{has_value('sensor.energy_consumed_daily') and
            has_value('sensor.gemeten_energie_per_dag')}}

ofc, you need to set up the 2 main entities, and everyone can do that as detailed as they would like, I have a lot setup via Powercalc integration for fixed and un smart-plugged devices.

Powercalc does all the work for you, only need to feed it with a device or a fixed value.

btw, having these availability templates make the ā€˜default’ requirement superfluous (when uses as a regular template, not when used as a trigger based template.

I’ve never liked to set a default because it obfuscates the truth in case of issues… this is better

      - unique_id: ongemeten_energie_per_dag
        state: >
          {{states('sensor.energy_consumed_daily')|float -
            states('sensor.gemeten_energie_per_dag')|float}}
        <<: &energy_total
          unit_of_measurement: kWh
          device_class: energy
          state_class: total
        availability: >
          {{has_value('sensor.energy_consumed_daily') and
            has_value('sensor.gemeten_energie_per_dag')}}

I’ve also found the is_number() test to be useful for availability templates

      - unique_id: saldo_totaal_dag_energie_afname_of_levering
        state: >
           {{states('sensor.grid_energy_total')|float|abs }}
        attributes:
          phrase: >
            {{'levering' if states('sensor.grid_energy_total')|float < 0 else 'afname'}}
          saldo: >
            {{states('sensor.grid_energy_total')}}
        <<: *energy_total
        availability: >
          {{is_number(states('sensor.grid_energy_total'))}}
1 Like
  1. What is the learning of has_value filter not consistent in its output Ā· Issue #91607 Ā· home-assistant/core Ā· GitHub regarding the ā€œhas_valueā€ usage?

  2. According to 2023.4: Custom template macros, and many more new entity dialogs! - Home Assistant has_value ā€œonlyā€ filters unavailable and unknown - right? Is that really sufficient for an availability template? So far I always checked with several other states like:

{{ states('sensor.abc') not in ['NULL', 'null', 'none', 'undefined', 'unavailable', 'unknown', ''] }}

Got it. Unless @Sir_Goodenough has good arguments to check for other states :slight_smile: I’ll likely migrate all my availability templates to the way more simple has_value function.

If you use it in other places that for instance may not be available at startup, or that might be from a zigbee sensor that might go offline, etc.
I am actually testing ATM has_value to replace the ā€˜unknown or unavailable’ part of what I had, leaving the other 3 possibles.

is_number would not work in mine as it tests booleans and about every other kind of entity.

this might be outdated by edits, but sorry I missed it, somehow I didnt get a tag.

where in my examples do I use the incorrect syntax you paste there?

In multiple places you are using:

{{ ('binary_sensor.boiler_on')|has_value }}

That is incorrect. You need to use the states() function with the has_value filter, like this:

{{ states('binary_sensor.boiler_on')|has_value }}
Only if you use the has_value() function can you do:
{{ has_value('binary_sensor.boiler_on') }}

Hmm. I can’t find the templates you say I am using .
Do seem to recall I had made that mistake before, but not in this thread ? Odd.
just checked my complete config, but dont even use a has_value in either syntax at all on the binary_sensor.boiler_on

(I just like to know, because I can then correct a fluke)

right…

that is old…

I was looking in this topic, and checking my contributions here…

and yes, that was what I tried to remember, but couldnt find.

not sure why you would link to that though, as it wasnt part of this discussion at all?
anyways, glad we’ve got that out of the way.

Yes it was, in the post I was directly replying to:

1 Like

Is it somehow possible to use availability check with new template sensor helper?

2 Likes

Is it possible to use a list of entities with the has_value function? or is the solution from Sir_Goodenough the best option?

I can’t believe they’d release this half-baked.

Finish the baking then

Thanks for the suggestion. I had seen availability in some examples but didn’t know what it was for.

I know it’s a contrived example, but you cause the problem by using default zero in your float conversions. Leave out the zeros and the template would be unavailable when either sensor is unavailable.

state: "{{ states('sensor.one') | float() + states('sensor.two') | float() }}"

1 Like

That would fill your log with errors and may disable your sensor. The availability template method won’t.

A post was split to a new topic: Filtering utility meter