🧯 Availability-Template

Custom Template for checking the availability of an entity.

hacs_badge
Version

:fire_extinguisher: Availability-Template

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.

This requires HomeAssistant version 2023.11.0 or greater due to the use of the list test in the code.

Installation

Install this in HACS or download the availability_template.jinja from this repository and place the files into your config\custom_templates directory.
Open your Home Assistant instance and open a repository inside the Home Assistant Community Store.

If you cannot see templates in your HACS listing

You may need to enable ‘experimental features’ mode. To do this find the HACS section of the Home Assistant Integration page. Click on the ‘>’ arrow to bring you to the custom integration page. Then click on configure, and select ‘enable experimental features’ check box. click submit, then float out and restart the HA server to make sure it takes. When you come back HACS will look slightly different, but the Templates section is now visible and you will be able to select it. At some point in time HACS will update and the new interface and options will be the only available interface.

Other help

Before you report a ‘Bug’, you need to make sure you have read the accompanying Descriptions on the templates and this README and have followed all the settings required here.
This is important because if these instructions are not followed, you will likely have a bad day and be forced to contact me for help. Not that I don’t want to help, but personal interaction takes me a while to respond and is generally non-productive.

Another good thing to do before you ask for help is try testing what you have in the Developer Tools Template Tab in Home Assistant. There you can adjust this and that until you figure out the answer to your question yourself. Open your Home Assistant instance and show your template developer tools.


Availability Test

avail([entity1, entity2, /])

This expects a list of entities.

  • The number of entities are counted.
  • The number of entities that are NOT unknown, unavailable, empty, or none are counted.
  • If the 2 counts are the same, true is returned, else false, defaults to false.

REMEMBER!!

This always returns text, so cast to bool on the other end to be certain of the result.

Use of the - character in the return template ensures no unwanted spacing is pulled back with your answer.

Examples

Generically, this can be dropped into many templates to be sure the result will render properly.

availability: >-
    {% from 'availability_template.jinja' import avail %}
    {{- avail(['entity_1','entity_2']) | bool -}}

Here is a full example that uses this. It will give you percent sunshine estimate based on data from sun angle and cloud coverage if you have those integrations in your config. (Found the state statement somewhere a while ago, sorry there is no attribution. I use it in my personal config.)

- template:
  - sensor:
    - name: "sunlight_pct"
      unique_id: 9a7586c0-0947-4b41-97e0-c0d2150bd0bb
      unit_of_measurement: "%"
      state: >-
        {%- set elevation = state_attr('sun.sun','elevation') | float %}
        {%- set cloud_coverage = states('sensor.openweathermap_cloud_coverage') | float %}
        {%- set cloud_factor = (1 - (0.75 * ( cloud_coverage / 100) ** 3 )) %}
        {%- set min_elevation = -6 %}
        {%- set max_elevation = 60 %}
        {%- set adjusted_elevation = elevation - min_elevation %}
        {%- set adjusted_elevation = [adjusted_elevation,0] | max %}
        {%- set adjusted_elevation = [adjusted_elevation,max_elevation - min_elevation] | min %}
        {%- set adjusted_elevation = adjusted_elevation / (max_elevation - min_elevation) %}
        {%- set adjusted_elevation = adjusted_elevation %}
        {%- set adjusted_elevation = adjusted_elevation * 100 %}
        {%- set brightness = adjusted_elevation * cloud_factor %}
        {{ brightness | round }}
      availability: >-
        {% from 'availability_template.jinja' import avail %}
        {{- avail(['sun.sun','sensor.openweathermap_cloud_coverage']) | bool -}}
      icon: mdi:sun-angle
      attributes:
        friendly_name: "Sunlight Percentage"

Other Info

Location of this code: GitHub - SirGoodenough/Availability-Template: Custom Template for checking the availiability of an entity.

Let me know if you have a suggestion.

If you have any questions, comments or additions be sure to add an issue in the above listed github repo and bring them up on my Discord Server:

:man_juggling:t5: Contact Links or see my other work

What are we Fixing Today Homepage / Website: https://www.WhatAreWeFixing.Today/

Channel Link URL: (WhatAreWeFixingToday): https://bit.ly/WhatAreWeFixingTodaysYT

What are we Fixing Today Facebook page (Sir GoodEnough): WhatAreWeFixingToday | Waukesha WI

What are we Fixing Today Twitter Account (Sir GoodEnough): https://bit.ly/WhatAreWeFixingTodayTW

Discord Guild: (Sir_Goodenough#9683): WhatAreWeFixingToday?

BluePrint Library: HA_Blueprints/README.md at master · SirGoodenough/HA_Blueprints · GitHub

#WhatAreWeFixingToday

#SirGoodEnough

Disclaimer

:warning: DANGER OF ELECTROCUTION :warning:

If your device connects to mains electricity (AC power) there is danger of electrocution if not installed properly. If you don’t know how to install it, please call an electrician (Beware: certain countries prohibit installation without a licensed electrician present). Remember: SAFETY FIRST. It is not worth the risk to yourself, your family and your home if you don’t know exactly what you are doing. Never tinker or try to flash a device using the serial programming interface while it is connected to MAINS ELECTRICITY (AC power).

2 Likes

Hi there,

I just came across this Availability template in HACS…I’m always struggling with availability on my sensors on start ups…so if i use this in my sensor yaml code, the sensors will NOT try to update if the return is False…is that right?

Thanx a lot…

1 Like

Hi Chris,

That’s the idea.

1 Like

Not to be a downer, but I’m not sure why this is even needed?

{{ ['sensor.entity_1','sensor.entity_2'] | reject('has_value') | list | length == 0 }}

I’ve had problems with has_value, it gives weird answers on edge cases.
I tried to use the has_value in the code for that template and it didn’t work right, for instance.
I think it’s meant for sensors and not other things or something like that.

It had a bug in it for awhile that was fixed about 6 or 7 months ago. There shouldn’t be any edge cases. Simply unavailable or unknown from the states will produce false.

I can test it again, but I thought this was after they said it was fixed.
I think it has a problem with this stuff…


    availability: >-
      {% from 'availability_template.jinja' import avail %}
      {{- avail(['climate.living_room_2']) | bool -}}

has_value doesn’t work there I think.

would need to know what you consider an edge case.

Also, that |bool isn’t needed as the resolver handles the typecasting.

slow internet up north this week…

{{ 'climate.living_room_2' | has_value }}

if you have it in a list…

{{ ['climate.living_room_2'] | reject('has_value') | list | count == 0 }}

Ok, I’ll plug has value back in when I get home and test it in my stuff.
I’ll kill it if indeed it no longer is useful, but I know I had a problem with something when I tested it. Not certain on timing, but I thouthr it was after it had settled that initial hick-up.