Add availability template to the new template sensor helper

It is great that template sensors can now be added through the UI. However, for numerical sensors it is often very important to add an availability template. If you do not, often the template will revert to returning 0 values that will result in a mess when the template sensor is later used in derivatives, integrals, energy dashboards etc.

+1 More context here.

Will this help you?

1 Like

Even (or better: also because, cause the missing feature of this FR is blocking me a bit of doing so) I’m not using all my utility meters through the UI yet but still YAML based, I encourage this FR.

Coming from broken energy dashboard incidents.


@Sir_Goodenough

Even I don’t get the ⚠️ DANGER OF ELECTROCUTION ⚠️ disclaimer at the end (that’s my way of telling you: yes, I’ve read that page to the bottom :laughing: ), that sounds quite interesting.

  1. What was your motivation of creating this repo / custom integration / macro?
  2. What’s the actual benefit of this macro?
    My understanding of the added benefit of this macro:
    Making it syntactically a bit easier (well, the importing line adds a bit of chars…) to consistently filter unwanted states - right?
  3. Why is HA 2023.11.0 needed to install it (using HACS)? What’s the specific dependency, is it using something only introduced with HA 2023.11.0?

  1. Because I like to share with the community. Sorry if that’s a problem.
  2. As described in the docs you read.
    The main reason for using this template is not because it's complicated, it's because availability is something you will be using over and over when you are dealing with sensors, so being able to repeat the same action over and over is better if there is 1 place in your project that the code exists.
  3. The list test is added added in 2023.11.0 new list test version

If you don’t like the sensor or don’t like your system to be less than 3 months behind in updates, you can look at prior versions and use the template with or without the macro functionality. The code is there and shared with license for you to use.

I’ll definitely give it a try, it’s on my todo list once HA got upgraded. Currently my existing template sensors either already have an availability template or didn’t need one. For the latter ones your macro looks like an appropriate solution.

1 Like

You can go into the github releases and install the one before the version listed above. It worked fine and should work with your older version. I just wanted to clean up the code a bit and use the new list test that was released.
Then when you pull HA up you can just update the macro as well.

I think this would be a great idea. I had issues with a binary_sensor that I need to invert, and I couldn’t work out how to ignore unknown and unavailable values from the original sensor, since None values are automatically converted to false for binary sensors.

Currently, the only way I found to make it work is to purposefully raise an error, but I end up with lots of TemplateError('ZeroDivisionError: division by zero') in the logs.

{% if states('binary_sensor.12_000000000000_sensed_a') in ('unknown','unavailable') %}
{{ 0/0 }}
{% else %}
{{ not states('binary_sensor.12_000000000000_sensed_a') }}
{% endif %}

Working on it in Add Availability to sensor template config flow by epenet · Pull Request #126806 · home-assistant/core · GitHub

I am disappointed to see the HA team has decided to reject this feature.

It seems like a mandatory function for any reasonable template sensor derived from a total or total_increasing sensor. :person_shrugging:

1 Like

Yeah it’s ridiculous. They don’t want to crowd the UI? Put it under a collapsed advanced section or something.

1 Like

The recommended solution is to do the check inside the template, and return None. Then you end up with unknown instead of unavailable but at least it doens’t error.

It seems that crowding the template instead of the UI is the recommended option…

I don’t know if it’s worth discussing more on the arch discussion Add Availability to UI templates · home-assistant/architecture · Discussion #1149 · GitHub but I too am dis-heartened by the replies I got from the core team on this topic :frowning:

Note: the workaround doesn’t even work for binary templates as they can’t return None

Can you give an example how to do that the proper way?

Something like this is what is expected:

            {% if states('sensor.other') in ('unknown','unavailable') %}
            {{ None }}
            {% else %}
            {{ states('sensor.other') }}
            {% endif %}
1 Like