Template Error: float got invalid input

How can i solve this error, i can’t do it i’ve been working on it for two days. Thank you

TemplateError('ValueError: Template error: float got invalid input ‘unknown’ when rendering template

Thank you

Logger: homeassistant.helpers.event
Source: helpers/template.py:425
First occurred: 09:04:07 (10 occurrences)
Last logged: 09:04:07

Error while processing template: Template("{{ (((states('sensor.p_importata_enel')|float) + (states('input_number.energia_prelevata_iniziale')|float)) | round(2) ) }}")
Error while processing template: Template("{{ (((states('sensor.p_importata_fotovoltaico')|float) + (states('input_number.energia_prodotta_iniziale')|float)) | round(2) ) }}")
Error while processing template: Template("{{ (((states('sensor.p_esportata_enel')|float) + (states('input_number.energia_scambiata_iniziale')|float)) | round(2) ) }}")
Error while processing template: Template("{{ (((states('sensor.energia_prodotta')|float) - (states('sensor.energia_scambiata')|float)) | round(2) ) }}")
Error while processing template: Template("{{ (((states('sensor.energia_prelevata')|float + states('sensor.energia_prodotta')|float - states('sensor.energia_scambiata')|float) + (states('input_number.energia_assorbita_iniziale')|float)) | round(2) ) }}")
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1710, in forgiving_float_filter
    return float(value)
ValueError: could not convert string to float: 'unknown'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 423, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1950, in _render_with_context
    return template.render(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.10/site-packages/jinja2/environment.py", line 936, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 1, in top-level template code
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1713, in forgiving_float_filter
    raise_no_default("float", value)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1411, in raise_no_default
    raise ValueError(
ValueError: Template error: float got invalid input 'unknown' when rendering template '{% if ( (states('sensor.p_instantanea_fotovoltaico')|float ) | int) > ( (states('sensor.p_instantanea_casa')|float ) | int) %}
   0.0
{% else %}
   {{ states('sensor.p_instantanea_enel')|float }}
{% endif %}' but no default was specified

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 540, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 425, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: float got invalid input 'unknown' when rendering template '{% if ( (states('sensor.p_instantanea_fotovoltaico')|float ) | int) > ( (states('sensor.p_instantanea_casa')|float ) | int) %}
   0.0
{% else %}
   {{ states('sensor.p_instantanea_enel')|float }}
{% endif %}' but no default was specified

############ INPUT #############################################################
input_number:
  acquisto_costo_energia:
    name: "E. Acquisto"
    # initial: 1
    min: 0.000
    max: 10.001
    mode: box
    step: 0.001
    unit_of_measurement: "€/kWh"
    icon: mdi:currency-eur

  vendita_costo_energia:
    name: "E. Vendita"
    #initial: 0
    min: 0.000
    max: 10.001
    mode: box
    step: 0.001
    unit_of_measurement: "€/kWh"
    icon: mdi:currency-eur
    
  energia_prelevata_iniziale:
    name: "E. Prelevata - Iniziale"
    #initial: 0.000
    min: -1000000.00
    max: 1000000.00
    mode: box
    step: 0.01
    unit_of_measurement: 'kWh'
    icon: mdi:fire-circle

  energia_assorbita_iniziale:
    name: "E. Assorbita - Iniziale"
    #initial: 0.000
    min: -1000000.00
    max: 1000000.00
    mode: box
    step: 0.01
    unit_of_measurement: 'kWh'
    icon: mdi:fire-circle

  energia_prodotta_iniziale:
    name: "E. Prodotta - Iniziale"
    #initial: 0.000
    min: -1000000.00
    max: 1000000.00
    mode: box
    step: 0.01
    unit_of_measurement: 'kWh'
    icon: mdi:fire-circle

  energia_scambiata_iniziale:
    name: "E. Scambiata - Iniziale"
    #initial: 0.000
    min: -1000000.00
    max: 1000000.00
    mode: box
    step: 0.01
    unit_of_measurement: 'kWh'
    icon: mdi:fire-circle



############ SENSOR ############################################################
sensor:
  - platform: template
    sensors:
      tensione_enel:
        friendly_name_template: "Tensione (V)"
        icon_template: mdi:lightning-bolt
        unit_of_measurement: 'V'
        value_template: "{{ states('sensor.iammeter_voltage_a') }}"
      corrente_enel:
        friendly_name_template: "Corrente (A)"
        icon_template: mdi:lightning-bolt-outline
        unit_of_measurement: 'A'
        value_template: "{{ states('sensor.iammeter_current_a') }}"
      p_instantanea_enel:
        friendly_name_template: "P. Instantanea"
        icon_template: mdi:home-lightning-bolt-outline      
        unit_of_measurement: 'W'
        value_template: "{{ states('sensor.iammeter_power_a') }}"
      p_importata_enel:
        friendly_name_template: "P. Importata"
        icon_template: mdi:transmission-tower-import      
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.iammeter_importenergy_a') }}"
      p_esportata_enel:
        friendly_name_template: "P. Esportata"
        icon_template: mdi:transmission-tower-export      
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.iammeter_exportgrid_a') }}"

      tensione_casa:
        friendly_name_template: "Tensione (V)"
        icon_template: mdi:lightning-bolt      
        unit_of_measurement: 'V'
        value_template: "{{ states('sensor.iammeter_voltage_b') }}"
      corrente_casa:
        friendly_name_template: "Corrente (A)"
        icon_template: mdi:lightning-bolt-outline    
        unit_of_measurement: 'A'
        value_template: "{{ states('sensor.iammeter_current_b') }}"    
      p_instantanea_casa:
        friendly_name_template: "P. Instantanea"
        icon_template: mdi:home-lightning-bolt-outline     
        unit_of_measurement: 'W'
        value_template: "{{ states('sensor.iammeter_power_b') }}" 
      p_importata_casa:
        friendly_name_template: "P. Importata"
        icon_template: mdi:transmission-tower-import      
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.iammeter_importenergy_b')}}"
      p_esportata_casa:
        friendly_name_template: "P. Esportata"
        icon_template: mdi:transmission-tower-export
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.iammeter_exportgrid_b') }}"

      tensione_fotovoltaico:
        friendly_name_template: "Tensione (V)"
        icon_template: mdi:lightning-bolt
        unit_of_measurement: 'V'
        value_template: "{{ states('sensor.iammeter_voltage_a')}}"
      corrente_fotovoltaico:
        friendly_name_template: "Corrente (A)"
        icon_template: mdi:lightning-bolt-outline
        unit_of_measurement: 'A'
        value_template: "{{ states('sensor.iammeter_current_c') }}"    
      p_instantanea_fotovoltaico:
        friendly_name_template: "P. Instantanea"
        icon_template: mdi:home-lightning-bolt-outline
        unit_of_measurement: 'W'
        value_template: "{{ states('sensor.iammeter_power_c') }}"      
      p_importata_fotovoltaico:
        friendly_name_template: "P. Importata"
        icon_template: mdi:transmission-tower-import
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.iammeter_importenergy_c') }}"    
      p_esportata_fotovoltaico:
        friendly_name_template: "P. Esportata"
        icon_template: mdi:transmission-tower-export
        unit_of_measurement: 'kWh'
        value_template: "{{ states('sensor.iammeter_exportgrid_c')}}"  

  - platform: template
    sensors:
       # POTENZA ISTANTANEA PRELIEVO ENEL (feedin_power)
      p_prelevata:
        friendly_name_template: "P. Prelevata"
        icon_template: mdi:home-lightning-bolt-outline
        unit_of_measurement: 'W'
        #value_template: "{{ states('sensor.p_instantanea_enel')|float * (-1.0) }}"
        #value_template: "{{ states('sensor.p_instantanea_enel')|float }}"
        value_template: >
          {% if ( (states('sensor.p_instantanea_fotovoltaico')|float ) | int) > ( (states('sensor.p_instantanea_casa')|float ) | int) %}
             0.0
          {% else %}
             {{ states('sensor.p_instantanea_enel')|float }}
          {% endif %}

     # POTENZA ISTANTANEA ASSORBITA (load_power)
      p_assorbita:
        friendly_name_template: "P. Assorbita"
        icon_template: mdi:home-lightning-bolt
        unit_of_measurement: 'W'
        value_template: "{{ (states('sensor.p_instantanea_enel')|float) + (states('sensor.p_instantanea_fotovoltaico')|float) }}" 

      # POTENZA ISTANTANEA FOTOVOLTAICO (inverter_power)
      p_produzione:
        friendly_name_template: "P. Produzione"
        icon_template: mdi:solar-power-variant
        unit_of_measurement: 'W'
        #value_template: "{{ states('sensor.p_instantanea_fotovoltaico')|float }}"
        value_template: >
          {% if ((states('sensor.p_instantanea_fotovoltaico')|float) | int) > 0 %}
             {{ states('sensor.p_instantanea_fotovoltaico')|float }}
          {% else %}
             0.0
          {% endif %}

      # POTENZA ISTANTANEA AUTOCONSUMATA
      p_autoconsumata:
        friendly_name_template: "P. Autoconsumata"
        icon_template: mdi:transmission-tower-off
        unit_of_measurement: 'W'
        value_template: >
          {% if ((states('sensor.p_assorbita')|float) | int) > ((states('sensor.p_produzione')|float) | int) %}
             {{ states('sensor.p_produzione')|float }}
          {% elif ((states('sensor.p_assorbita')|float) | int) < ((states('sensor.p_produzione')|float) | int) %}
             {{ states('sensor.p_assorbita')|float }}
          {% else %}
             0.0
          {% endif %}

      # POTENZA ISTANTANEA SCAMBIATA
      p_scambiata:
        friendly_name_template: "P. Scambiata"
        icon_template: mdi:transmission-tower-export
        unit_of_measurement: 'W'
        value_template: >
          {% if ((states('sensor.p_produzione')|float) | int) > ((states('sensor.p_autoconsumata')|float) | int) %}
             {{ states('sensor.p_produzione')|float - states('sensor.p_autoconsumata')|float }}
          {% else %}
             0.0
          {% endif %}

      # ENERGIA PRELEVATA DALLA RETE ENEL (grid_consumption_energy)
      energia_prelevata:
        friendly_name_template: "Energia Prelevata"
        icon_template: mdi:fire
        unit_of_measurement: 'kWh'
        value_template: "{{ (((states('sensor.p_importata_enel')|float) + (states('input_number.energia_prelevata_iniziale')|float)) | round(2) ) }}"

      # ENERGIA PRODOTTA DAL FOVOLTAICO (yield_energy)
      energia_prodotta:
        friendly_name_template: "Energia Prodotta"
        icon_template: mdi:fire
        unit_of_measurement: 'kWh'
        value_template: "{{ (((states('sensor.p_importata_fotovoltaico')|float) + (states('input_number.energia_prodotta_iniziale')|float)) | round(2) ) }}"

      # ENERGIA ESPORTATA SCAMBIATA (exported_energy)    
      energia_scambiata:
        friendly_name_template: "Energia Scambiata"
        icon_template: mdi:fire
        unit_of_measurement: 'kWh'
        value_template: "{{ (((states('sensor.p_esportata_enel')|float) + (states('input_number.energia_scambiata_iniziale')|float)) | round(2) ) }}"

      # ENERGIA AUTOCONSUMATA (selfuse_energy)
      energia_autoconsumata:
        friendly_name_template: "Energia Autoconsumata"
        icon_template: mdi:fire
        unit_of_measurement: 'kWh'
        value_template: "{{ (((states('sensor.energia_prodotta')|float) - (states('sensor.energia_scambiata')|float)) | round(2) ) }}" 
        
      # ENERGIA ASSORBITA (load_energy)
      energia_assorbita:
        friendly_name_template: "Energia Assorbita"
        icon_template: mdi:fire
        unit_of_measurement: 'kWh'
        value_template: "{{ (((states('sensor.energia_prelevata')|float + states('sensor.energia_prodotta')|float - states('sensor.energia_scambiata')|float) + (states('input_number.energia_assorbita_iniziale')|float)) | round(2) ) }}"

      
      # ENERGIA AUTOCONSUMATA GIORNALIERA PERCENTUALE      
      #energia_autoconsumata_rate_giornaliera:
      #  friendly_name_template: "Percentuale Giornaliera"
      #  icon_template: mdi:percent
      #  unit_of_measurement: '%'
      #  value_template: "{{ (states('sensor.autoconsumata_giornaliera')|float / states('sensor.prodotta_giornaliera')|float) | round(2) }}"

      # ENERGIA AUTOCONSUMATA MENSIALE PERCENTUALE            
      #energia_autoconsumata_rate_mensile:
      #  friendly_name_template: "Percentuale Mensile"
      #  icon_template: mdi:percent
      #  unit_of_measurement: '%'
      #  value_template: "{{ (states('sensor.autoconsumata_mensile')|float / states('sensor.prodotta_mensile')|float) | round(2) }}"
        
          
      # ENERGIA AUTOCONSUMATA GIORNALIERA PERCENTUALE      
      #energia_autoconsumata_rate_giornaliera:
      #  friendly_name_template: "Percentuale Giornaliera"
      #  icon_template: mdi:percent
      #  unit_of_measurement: '%'
      #  value_template: "{{ (states('sensor.autoconsumata_giornaliera')|float / states('sensor.prodotta_giornaliera')|float) | round(2) }}"

      # ENERGIA AUTOCONSUMATA MENSIALE PERCENTUALE            
      #energia_autoconsumata_rate_mensile:
      #  friendly_name_template: "Percentuale Mensile"
      #  icon_template: mdi:percent
      #  unit_of_measurement: '%'
      #  value_template: "{{ (states('sensor.autoconsumata_mensile')|float / states('sensor.prodotta_mensile')|float) | round(2) }}"
          

      # PRELIEVO
#      consumo_giornaliero_prelievo:
#        friendly_name_template: "Prelievo - C. Giornaliero"
#        icon_template: mdi:currency-eur
#        value_template: "{{ ((states.sensor.prelievo_giornaliero.state | float) * (states.input_number.prelievo_costo_energia.state | float)) | round(2) }}"
#        unit_of_measurement: '€'
        
      # FOTOVOLTAICO
#      consumo_giornaliero_produzione:
#        friendly_name_template: "Fotovoltaico - C. Giornaliero"
#        icon_template: mdi:currency-eur
#        value_template: "{{ ((states.sensor.fotovoltaico_giornaliero.state | float) * (states.input_number.fotovoltaico_costo_energia.state | float)) | round(2) }}"
#        unit_of_measurement: '€'


############ METER ############################################################
utility_meter:
  prelevata_giornaliera:
    source: sensor.energia_prelevata
    cycle: daily
  prelevata_mensile:
    source: sensor.energia_prelevata
    cycle: monthly
  prelevata_annuale:
    source: sensor.energia_prelevata
    cycle: yearly

  assorbita_giornaliera:
    source: sensor.energia_assorbita
    cycle: daily
  assorbita_mensile:
    source: sensor.energia_assorbita
    cycle: monthly
  assorbita_annuale:
    source: sensor.energia_assorbita
    cycle: yearly

  prodotta_giornaliera:
    source: sensor.energia_prodotta
    cycle: daily
  prodotta_mensile:
    source: sensor.energia_prodotta
    cycle: monthly
  prodotta_annuale:
    source: sensor.energia_prodotta
    cycle: yearly

  scambiata_giornaliera:
    source: sensor.energia_scambiata
    cycle: daily
  scambiata_mensile:
    source: sensor.energia_scambiata
    cycle: monthly
  scambiata_annuale:
    source: sensor.energia_scambiata
    cycle: yearly

  autoconsumata_giornaliera:
    source: sensor.energia_autoconsumata
    cycle: daily
  autoconsumata_mensile:
    source: sensor.energia_autoconsumata
    cycle: monthly
  autoconsumata_annuale:
    source: sensor.energia_autoconsumata
    cycle: yearly

You need to provide default values for float and int. Sometimes, the sensor value is unknown, and HA cannot turn that into a float value.

As a start, for every float and int in your template, change it to float(0) and int(0).

thank you, the errors have disappeared, but if I restart hassio, the energy, daily, monthly and yearly values are duplicated

thank you

Correct code, but if I restart hassio the daily, monthly and yearly values double

change it to float(0) and int(0)