ValueError: Template error: strptime got invalid input 'unknown' when rendering template (not the first time asked :) )

Hello,

Not the first time asked, but I can’t find the solution of my problems.
I have several HP templates and changed it to the new template way.
But still having errors in my log. Can somebody help me?


- sensor:
      # The number of allowance pages remaining for this period
      # Calculated when each page is printed, and at the start of each new Instant Ink period
      # "{% (if states('input_datetime.hp_officejet_8020_this_period_start_date') %}" is a workaround to set a listener)    
    - name: "hp_officejet_8020_pages_allowance_remaining"
      state: >
        {% if states('input_datetime.hp_officejet_8020_this_period_start_date') %}
          {% if states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) - states('sensor.hp_printer_printer')|int(0) >= 0 %}
            {{ states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) - states('sensor.hp_printer_printer')|int(0) }}
          {% else %}
            0
          {% endif %}
        {% endif %}
      availability: >
        {{ not ['unknown', 'unavailable', 'None'] in 
          [ states('input_datetime.hp_officejet_8020_this_period_start_date'), 
            states('input_number.hp_officejet_8020_pages_at_month_start'),
            states('input_number.hp_officejet_8020_pages_monthly_allowance'),
            states('sensor.hp_printer_printer') ] }}

      # Used to track days remaining of HP Ink Printer Pages period
      # Needs to have the date sensors installed as per: https://www.home-assistant.io/integrations
      # Use of sensor.date ought to force a daily update, according to https://www.home-assistant.io/integrations/template/  
    - name: "hp_officejet_8020_allowance_days_remaining"
      state: >
        {{ ((as_timestamp(strptime(states('sensor.hp_officejet_8020_next_renewal_date'), '%Y-%m-%d'))|int(0) - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int(0) ) / 86400)|int(0) }}
      availability: >
        {{ not ['unknown', 'unavailable', 'None'] in 
          [ states('sensor.hp_officejet_8020_next_renewal_date') ] }}

    # The number of rollover pages remaining for this period.
    # Calculated when each page is printed, when the Rollover Allowance is reset, and at the start of each new Instant Ink period
    - name: "hp_officejet_8020_pages_rollover_remaining"
      state: >
        {% if states('input_datetime.hp_officejet_8020_this_period_start_date') %}
          {% if states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) - states('sensor.hp_printer_printer')|int(0) < 0 and states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) + states('input_number.hp_officejet_8020_pages_rollover_allowance')|int(0) - states('sensor.hp_printer_printer')|int(0) >= 0 %}
            {{ states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) + states('input_number.hp_officejet_8020_pages_rollover_allowance')|int - states('sensor.hp_printer_printer')|int(0) }}
          {% elif states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) - states('sensor.hp_printer_printer')|int(0) < 0 and states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) + states('input_number.hp_officejet_8020_pages_rollover_allowance')|int(0) -   states('sensor.hp_printer_printer')|int(0) < 0 %}
            0
          {% else %}
            {{ states('input_number.hp_officejet_8020_pages_rollover_allowance')|int }}
          {% endif %}
        {% endif %}
      availability: >
        {{ not ['unknown', 'unavailable', 'None'] in 
          [ states('input_datetime.hp_officejet_8020_this_period_start_date'), 
            states('input_number.hp_officejet_8020_pages_at_month_start'),
            states('input_number.hp_officejet_8020_pages_monthly_allowance'),
            states('input_number.hp_officejet_8020_pages_rollover_allowance'),
            states('sensor.hp_printer_printer') ] }}

    # The number pages printed over and above the Monthly Allowance plus the Rollover Allowance
    # Calculated when each page is printed, and at the start of each new Instant Ink period
    - name: "hp_officejet_8020_pages_overprint"
      state: >
        {% if states('input_datetime.hp_officejet_8020_this_period_start_date') %}
          {% if states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) + states('input_number.hp_officejet_8020_pages_rollover_allowance')|int(0) < states('sensor.hp_printer_printer')|int(0) %}
            {{ states('sensor.hp_printer_printer')|int(0) - states('input_number.hp_officejet_8020_pages_rollover_allowance')|int(0) - states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) - states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) }}
          {% else %}
            0
          {% endif %}    
        {% endif %}
      availability: >
        {{ not ['unknown', 'unavailable', 'None'] in 
          [ states('input_datetime.hp_officejet_8020_this_period_start_date'), 
            states('input_number.hp_officejet_8020_pages_at_month_start'),
            states('input_number.hp_officejet_8020_pages_monthly_allowance'),
            states('input_number.hp_officejet_8020_pages_rollover_allowance'),
            states('sensor.hp_printer_printer') ] }}

    # The number of pages remaining for this month in the chargeable Over Allowance area
    - name: "hp_officejet_8020_pages_overprint_remaining"
      state: >
        {% if states('sensor.hp_officejet_8020_pages_overprint'), 0 == 0 or (states('sensor.hp_officejet_8020_pages_overprint'), 0 % states('input_number.hp_officejet_8020_pages_overprint_block_size'), 0 == 0) %}
          0
        {% else %}
          {{ states('input_number.hp_officejet_8020_pages_overprint_block_size'), 0 - (states('sensor.hp_officejet_8020_pages_overprint'), 0 % states('input_number.hp_officejet_8020_pages_overprint_block_size'), 0) }}
        {% endif %}
      availability: >
        {{ not ['unknown', 'unavailable', 'None'] in 
          [ states('sensor.hp_officejet_8020_pages_overprint'), 
            states('input_number.hp_officejet_8020_pages_overprint_block_size') ] }}


    # Next Renewal Date
    # WILL NOT WORK FOR 29th, 30th, 31st OF THE MONTH
    - name: "hp_officejet_8020_next_renewal_date"
      state: >
        {% if (states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[1]|int(0) + 1 > 12) %}
          {{ (states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[0]|int(0) + 1)|string + "-01-" + states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[2]|int|string }}
        {% else %}
          {{ states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[0]|int|string + "-" +  ('%02d' % (states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[1]|int(0) + 1)) + "-" + states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[2]|int|string }}
        {% endif %}
      availability: >
        {{ not ['unknown', 'unavailable', 'None'] in 
          [ states('input_datetime.hp_officejet_8020_this_period_start_date') ] }}


Logger: homeassistant.helpers.event
Source: helpers/template.py:565
First occurred: 01:12:52 (2 occurrences)
Last logged: 01:12:52

Error while processing template: Template<template=({{ ((as_timestamp(strptime(states('sensor.hp_officejet_8020_next_renewal_date'), '%Y-%m-%d'))|int(0) - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int(0) ) / 86400)|int(0) }}) renders=2>
Error while processing template: Template<template=({% if (states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[1]|int(0) + 1 > 12) %} {{ (states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[0]|int(0) + 1)|string + "-01-" + states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[2]|int(0) }} {% else %} {{ states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[0]|int|string + "-" + ('%02d' % (states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[1]|int(0) + 1)) + "-" + states('input_datetime.hp_officejet_8020_this_period_start_date').split("-")[2]|int(0) }} {% endif %}) renders=2>
Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1829, in strptime
    return datetime.strptime(string, fmt)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/_strptime.py", line 568, in _strptime_datetime
    tt, fraction, gmtoff_fraction = _strptime(data_string, format)
                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/_strptime.py", line 349, in _strptime
    raise ValueError("time data %r does not match format %r" %
ValueError: time data 'unknown' does not match format '%Y-%m-%d'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 563, in async_render
    render_result = _render_with_context(self.template, compiled, **kwargs)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 2179, in _render_with_context
    return template.render(**kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.11/site-packages/jinja2/environment.py", line 1301, in render
    self.environment.handle_exception()
  File "/usr/local/lib/python3.11/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/local/lib/python3.11/site-packages/jinja2/sandbox.py", line 393, in call
    return __context.call(__obj, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1832, in strptime
    raise_no_default("strptime", string)
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 1609, in raise_no_default
    raise ValueError(
ValueError: Template error: strptime got invalid input 'unknown' when rendering template '{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8020_next_renewal_date'), '%Y-%m-%d'))|int(0) - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int(0) ) / 86400)|int(0) }}' 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 684, in async_render_to_info
    render_info._result = self.async_render(variables, strict=strict, **kwargs)
                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/src/homeassistant/homeassistant/helpers/template.py", line 565, in async_render
    raise TemplateError(err) from err
homeassistant.exceptions.TemplateError: ValueError: Template error: strptime got invalid input 'unknown' when rendering template '{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8020_next_renewal_date'), '%Y-%m-%d'))|int(0) - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int(0) ) / 86400)|int(0) }}' but no default was specified

Welcome back, after four years! :+1:

You’re missing the default value in your strptime. :slight_smile:

In this line

{{ ((as_timestamp(strptime(states('sensor.hp_officejet_8020_next_renewal_date'), '%Y-%m-%d'))|int(0) - as_timestamp(strptime(states('sensor.date'), '%Y-%m-%d'))|int(0) ) / 86400)|int(0) }}

add a default value of your choice for both strptime and the error should be gone.

See here:

Hi Patrick,

Had long time no issues :slight_smile:
But still struggling with this. I know the problem, but don’t now where to put and in what format the defaults. I was searching a lot, but can you explain what and where in a different template below.

I found an other sulution for the earlier fault. Can’t found the place where I must put the defaults you told me.

Regard,
Raimond

    - name: "hp_officejet_8020_allowance_days_remaining"
      state: >
        {% set dt = states('sensor.hp_officejet_8022_next_renewal_date') | as_datetime or today_at() %}
          {{ (dt | as_local - today_at()).days }}
      availability: >
        {{ not ['unknown', 'unavailable', 'None'] in 
          [ states('sensor.hp_officejet_8020_next_renewal_date') ] }}
      
state: >
        {% if states('input_datetime.hp_officejet_8020_this_period_start_date'), 0 %}
          {% if states('input_number.hp_officejet_8020_pages_at_month_start'), 0|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) - states('sensor.hp_printer_printer')|int(0) < 0 and states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) + states('input_number.hp_officejet_8020_pages_rollover_allowance')|int(0) - states('sensor.hp_printer_printer')|int(0) >= 0 %}
            {{ states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) + states('input_number.hp_officejet_8020_pages_rollover_allowance')|int - states('sensor.hp_printer_printer')|int(0) }}
          {% elif states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) - states('sensor.hp_printer_printer')|int(0) < 0 and states('input_number.hp_officejet_8020_pages_at_month_start')|int(0) + states('input_number.hp_officejet_8020_pages_monthly_allowance')|int(0) + states('input_number.hp_officejet_8020_pages_rollover_allowance')|int(0) -   states('sensor.hp_printer_printer')|int(0) < 0 %}
            0
          {% else %}
            {{ states('input_number.hp_officejet_8020_pages_rollover_allowance')|int }}
          {% endif %}
        {% endif %}