Availability_template in sensor template

I try to make use of the newly introduced availability_template.
This should get rid of errors at startup of hass. I struggle with nesting the value_template in the availability_template.

 - platform: template
   sensors:
     days_gft:
       availability_template: >-
         {%- if not is_state("sensor.waste_gft", "unavailable") %}
           value_template: >-
             {% set days = (strptime(states('sensor.waste_gft'), '%Y-%m-%d').strftime('%j') | int) - now().strftime('%j') | int ) %}
             {% if days == 0 %}
               today
             {% elif days == 1 %}
               tomorrow
             {% elif days >= 2 %}
               in {{days}} days
             {% else %}
               not any time soon
             {% endif %}
           entity_id: date.time
         {% else %}
           not available
         {% endif %}

Can someone shine a light in the darkness?

Take a look at the example here, I think you don’t need the additional value_template nesting level?

2 Likes

Is this the example that you are refering to?

sensor:
  - platform: template
    sensors:
      my_sensor:
        availability_template: >-
          {%- if not is_state("switch.some_sensor", "unavailable") %}
            true
          {%- endif %}

Where should i put my value_template?

They’re both options of a Template Sensor so either before or after availability_template: is fine.

sensor:
  - platform: template
    sensors:
      my_sensor:
        value_template: "{{ stuff related to the value_template }}"
        availability_template: "{{ stuff related to the availability_template }}"

Thanks @Geoff_B and @123 for enlightening me!

@Cadster Bumping this up; did you ever get this sensor with availability_template working?

Could you provide a working example of a switch?

EDIT: Never mind, sorted it out :slight_smile:

Can anyone help with this? I have been trying to prevent errors at start up for my template sensors. I have an energy monitoring switch that I am keeping stats on. At startup the sensors needed for this sensor are not yet available, so I need this template sensor to be ‘unavailable’ until they are ready. I don’t understand why this isn’t working. I get ZeroDivisionError: float division by zero error at startup for this sensor.

I’ve tested the template in the dev tools and if states('sensor.s31_energy_total_days') evaluates to ‘unknown’ or ‘unavailable’ then states('sensor.s31_energy_total_days')|int == 0. So this should work. What am I doing wrong here??

s31_avg_kwh_per_day:
  value_template: >-
    {{ ((states('sensor.s31_energy_total_days')|float) / (states('sensor.s31_energy_total')|float)) | round(1) }}
  unit_of_measurement: 'kWh/Day'
  availability_template: >-
    {{ states('sensor.s31_energy_total_days')|int != 0 and states('sensor.s31_energy_total')|int != 0 }}

You’ll need to check if the sensor values are unknown before proceeding to convert their values to float and performing division.

  value_template: >-
    {% set td = states('sensor.s31_energy_total_days') %}
    {% set t = states('sensor.s31_energy_total') %}
    {{ 'unknown' if td == 'unknown' or t == 'unknown' else  (td|float / (t|float)) | round(1) }}

I leave to you the exercise of enhancing the availability_template.

2 Likes

Oh, is the value_template still getting processed even if the availability_template returns false? That would make sense. I was assuming that if availability was false then the value_template would be ignored. Seems like an oversight. :thinking:

I am struggling with exact the same point. I am trying to filter out enger spikes with the availibility template, but its state is simply ignored. (https://community.home-assistant.io/t/help-with-solving-engery-spike-with-availability-template-kindly-requested/509102/2
I wonder wheather a switch or somethings else needs to be implemented in order to make use of the availibilit template. The documentation for that switch is basically useless. As it is the sitch availability_template makes no sense at all to me.

Finnally solved the issue with the help of another forum member:

Is it possible to filter the unknow state of a sensor and view all other states of the template? Sometimes the sensor goes unknow for a few seconds, I would like it not to be displayed, perhaps leaving the field blank.

Sensor is sensor.tv_channel

Sure should be possible by “replacing > 1234” (the number after >) with “NOT ‘unknown’”