Hoping to get help with an template entity error that is showing in my logs 'input "unavailable when rendering template..."

Here is the error:

Logger: homeassistant.components.template.template_entity
Source: components/template/template_entity.py:173
Integration: Template (documentation, issues)
First occurred: 8:00:24 PM (1 occurrences)
Last logged: 8:00:24 PM

TemplateError('ValueError: Template error: int got invalid input 'unavailable' when rendering template '{%- if states('sensor.lightning_near_home_lightning_counter') | int > 0 %} Unsafe {% else %} Safe {%- endif %}' but no default was specified') while processing template 'Template("{%- if states('sensor.lightning_near_home_lightning_counter') | int > 0 %} Unsafe {% else %} Safe {%- endif %}")' for attribute '_attr_native_value' in entity 'sensor.lightning_warning'

Here is the template sensor

      lightning_warning:
        friendly_name: "Lightning Warning"
        value_template: >-
          {%- if states('sensor.lightning_near_home_lightning_counter') | int > 0 %}
            Unsafe
          {% else %}
            Safe
          {%- endif %}

This is the only place “sensor.lightning_warning” shows up in my whole home assistant setup

  - alias: Lightning Detected
    trigger:
      - platform: state
        entity_id: sensor.lightning_warning
        to: 'Unsafe'
    condition:
      - condition: state
        entity_id: input_boolean.lightning_warning
        state: 'off'
    action:
      - service: script.text_notify
        data_template:
          title: "Lightning Detected!"
          message: "Lightning has been detected within 25 miles of Turner House. Nearest storm is {{ states('sensor.lightning_near_home_lightning_distance') }} miles away."
      - service: input_boolean.turn_on
        entity_id: input_boolean.lightning_warning
      - service: script.lightning_warning_audible

and here:

  - alias: Lightning Warning Off
    trigger:
      - platform: state
        entity_id: sensor.lightning_warning
        to: 'Safe'
        for:
          minutes: 20
    condition:
      - condition: state
        entity_id: input_boolean.lightning_warning
        state: 'on'
    action:
      - service: script.text_notify
        data_template:
          who: "weston"
          title: "Lightning Threat Over."
          message: "No more Lightning Strikes detected."
      - service: input_boolean.turn_off
        entity_id: input_boolean.lightning_warning
      - service: script.lightning_clear_audible

Here are the results in developer tools:

What am I missing, I guess I just don’t understand what the error is trying to tell me because I don’t see anything wrong.

The error likely occurs when you restart home assistant as the template is trying to render before sensor.lightning_near_home_lightning_counter has a value.

If you place a default value it will no longer error.

to do that you simply change int to int(x) where x is an integer. You have two choices as far as i see it. int(0) will be safe and int(1) will be unsafe. This will impact/trigger your automations potentially.

You could add a third status to your template.

lightning_warning:
        friendly_name: "Lightning Warning"
        value_template: >-
          {%- if states('sensor.lightning_near_home_lightning_counter') == 'unavailable' %}
            Unavailable
          {%- elif states('sensor.lightning_near_home_lightning_counter') | int(0) > 0 %}
            Unsafe
          {% else %}
            Safe
          {%- endif %}

@SgtBatten Thank you so much for helping me with this @SgtBatten . I am getting better with home assistant in terms of using others code etc. but I’m still not quite there yet when it comes to fixing my own issues just yet, codewise. Thank you so much for your help with this. I’m sure what you have done will more than likely fix this.

Thank you so much!!

1 Like