core: Template sensor value_template will generate 'no default' errors if availability_template renders false.

The problem

With templates sensors, I used to be able to do input validation exclusively via the availability_template: template feature.

Prior to 2021.10 if availability_template renders false, what value_template renders to was irrelevant because availability_template took precedence and forced the state to unavailable. The rendering of value_template at least from the user’s perspective was seemingly blocked.

After 2021.10 if availability_template renders false value_template is still rendered and checked for any filter default value errors and logged when the expected behavior is to completely disregard the rendered value or not render the template at all.

This new behavior requires input validation/default filter values to be added to value_template for the sole purpose of avoiding spamming the log when availability_template was enough to handle error checking on its own.

What is version of Home Assistant Core has the issue?

2021.10.0

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant Core

Integration causing the issue

template

Link to integration documentation on our website

No response

Example YAML snippet

- platform: template
    sensors:
      br_humidity_delta:
        friendly_name: "Bedroom Humidity Delta"
        unit_of_measurement: 'Δ%rh'
        value_template: "{{ (states('sensor.bedroom_humidity')|float - states('sensor.upstairs_relative_humidity')|float) |round(1) }}"
        availability_template: "{{states('sensor.bedroom_humidity')|is_number and states('sensor.upstairs_relative_humidity')|is_number}}"

Anything in the logs that might be useful for us?

Logger: homeassistant.helpers.template
Source: helpers/template.py:1210
First occurred: 15:49:56 (2 occurrences)
Last logged: 15:49:56

Template warning: 'float' got invalid input 'unavailable' when rendering template '{{ (states('sensor.bedroom_humidity')|float - states('sensor.upstairs_relative_humidity')|float) |round(1) }}' but no default was specified. Currently 'float' will return '0', however this template will fail to render in Home Assistant core 2021.12

Additional information

No response

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 23 (17 by maintainers)

Most upvoted comments

Template errors during startup is fixed by #59827 which will be included in Home Assistant Core 2021.12.

This is an attempt of suppressing errors when the availability template doesn’t render as True: https://github.com/home-assistant/core/pull/58477

The availability template does not block rendering of other templates, it just means the entity’s state will be set to unavailable if the availability template returns False. It seems reasonable to suppress rendering of other templates if the availability template returns False, but not sure if that can be implemented in a simple way.