Template warning - Float 'unavailable' - no default

Im using a code that tell me when my washingmachine is finished but a got att warning in logs.
What to add and where?

Tried to add {{ states(‘tvattmaskin_energi’)|round(default=0) }} above but still renders in warning.

The Warning:

Template warning: ‘float’ got invalid input ‘unavailable’ when rendering template ‘{% if states.sensor.tvattmaskin_energi.state | float > 3.0 %} {{ “Running” }} {% elif states.sensor.tvattmaskin_energi.state | float > 1 %} {{ “On” }} {% else %} {{ “Off” }} {% endif %}’ but no default was specified. Currently ‘float’ will return ‘0’, however this template will fail to render in Home Assistant core 2021.12

The Code:

homeassistant:
  customize_glob:
    "*tvattmaskin*":
      icon: mdi:washing-machine

input_text:
  tvattmaskin_utokad_status:
    name: 'Tvättmaskin Utökad Status'
    initial: Off
    
sensor:
  - platform: template
    sensors:
      tvattmaskin_status:
        friendly_name: 'Tvättmaskin Status'
        value_template:  >
          {% if states.sensor.tvattmaskin_energi.state | float > 3.0 %}
            {{ "Running" }}
          {% elif states.sensor.tvattmaskin_energi.state | float > 1 %}
            {{ "On" }}
          {% else %}
            {{ "Off" }}
          {% endif %}
      tvattmaskin_info:
        friendly_name: 'Tvättmaskin Info'
        #entity_id: sensor.time
        value_template:  >
          {%- macro as_formatted_elapsed_time(now, other_date_time) %}
          {% set duration = as_timestamp(now) - as_timestamp(other_date_time) %}
          {% set seconds = (duration % 60) | int %}
          {% set minutes = ((duration / 60) | int) % 60 %}
          {% set hours = (duration / 3600) | int %}
          {{ [hours, "hours", minutes, "minutes", seconds, "seconds"] | join(' ') }}
          {%- endmacro %}
          {% if states.input_text.tvattmaskin_utokad_status.state == "Running" %}
            Tvättmaskinen körd i: {{ as_formatted_elapsed_time(now(), states.input_text.tvattmaskin_utokad_status.last_changed)}}
          {% elif states.input_text.tvattmaskin_utokad_status.state == "On_After_Running" %}
            Kläder kvar i tvättmaskinen i: {{ as_formatted_elapsed_time(now(), states.input_text.tvattmaskin_utokad_status.last_changed)}}
          {% elif states.input_text.tvattmaskin_utokad_status.state == "On_After_Off" %}
            {{ "Tvättmaskinen är redo att startas" }}
          {% else %}
            {{ "Tvättmaskinen är avstängd" }}
          {% endif %}

automation:
  - id: '1556314846684'
    alias: Uppdatera Tvättmaskin Info
    initial_state: true
    trigger:
      platform: state
      entity_id: sensor.tvattmaskin_status
    action:
      - service: input_text.set_value
        data_template:
          entity_id: input_text.tvattmaskin_utokad_status
          value:  >
            {% if trigger.to_state.state == "Running" %}
              {{ "Running" }}
            {% elif trigger.to_state.state == "On" and trigger.from_state.state == "Running" %}
              {{ "On_After_Running" }}
            {% elif trigger.to_state.state == "On" and trigger.from_state.state == "Off" %}
              {{ "On_After_Off" }}
            {% else %}
              {{ "Off" }}
            {% endif %}

While evaluating the template, the sensor’s value was unavailable

states.sensor.tvattmaskin_energi.state | float

The float filter cannot convert unavailable to a number.

When this happened in previous versions of Home Assistant, float would simply convert unavailable to 0 and say nothing. However, now it tells you that this incident occurred and warns you that you should provide float with a default value (you decide what that value should be).

If you want float to use 0 as a default value, just specify it like this everywhere you use it in your templates:

float(0)

For example:

        value_template:  >
          {% if states.sensor.tvattmaskin_energi.state | float(0) > 3.0 %}
            {{ "Running" }}
          {% elif states.sensor.tvattmaskin_energi.state | float(0) > 1 %}
            {{ "On" }}
          {% else %}
            {{ "Off" }}
          {% endif %}

For more information, refer to:

4 Likes

Thanks! Fixed the warning

1 Like

You’re welcome!

If you’re interested, you can reduce the template to this:

      tvattmaskin_status:
        friendly_name: 'Tvättmaskin Status'
        value_template: >
          {% set e = states('sensor.tvattmaskin_energi') | float(0) %}
          {{ 'Running' if e > 3 else 'On' if e > 1 else 'Off' }}

EDIT

Correction. Replaced %} with }}

Always nice with a code cleanup! Thanks

Corrected a typo: %} should be }}

{% set e = states('sensor.tvattmaskin_energi') | float(0) %}
{{ 'Running' if e > 3 else 'On' if e > 1 else 'Off' }}

Im continuing this thread with the Template varning.
Im getting this i the logs:

Logger: homeassistant.helpers.template
Source: helpers/template.py:1706
First occurred: 10:58:55 (2 occurrences)
Last logged: 11:10:51

Template variable warning: 'dict object' has no attribute 'batt' when rendering '{{ value_json.batt }}'

Nothing more than this and I just dont know where this varning is located. Help plz.

This warning message is unrelated to the original topic (which was about the need for a default value for the float filter). This question belongs in a separate topic.

Anyway, check any Template Sensors you may have created that refer to battery levels.

thanks. checking.