ValueError: Template error: int got invalid input 'None' when rendering template

window2_last_changed:
      entity_id: sensor.time
      value_template: >-
          {%- set time = (as_timestamp(now()) - (state_attr('sensor.window2', 'lastUpdated') | int  / 1000 )) | int %}
          {%- set minutes = ((time % 3600) // 60) %}
          {%- set hours = ((time % 86400) // 3600) %}
          {%- set days = (time // 86400) %}
          {% if time <= 60 %}
          {# minimun or = 59 sec #}
            0m
          {% elif time <= 3600 %}
          {# minimum or = 59 min #}
           {{ minutes }}m
          {% elif time <= 86400 %}
          {# minimum or = 23h and 59 min #}
           {{ hours }}t
          {% elif time > 86400 %}
          {# more or = 1 day #}
           {{ days }}d 
          {% else %}
          {% endif %}

Here is a error

ValueError: Template error: int got invalid input 'None' when rendering template 'window2_last_changed:
      entity_id: sensor.time
      value_template: >-
          {%- set time = (as_timestamp(now()) - (state_attr('sensor.window2', 'lastUpdated') | int  / 1000 )) | int %}
          {%- set minutes = ((time % 3600) // 60) %}
          {%- set hours = ((time % 86400) // 3600) %}
          {%- set days = (time // 86400) %}
          {% if time <= 60 %}
          {# minimum or = 59 sec #}
            0m
          {% elif time <= 3600 %}
          {# minimum or = 59 min #}
           {{ minutes }}m
          {% elif time <= 86400 %}
          {# minimum or = 23h and 59 min #}
           {{ hours }}t
          {% elif time > 86400 %}
          {# more or = 1 day #}
           {{ days }}d 
          {% else %}
          {% endif %}' but no default was specified

Wrong lastUpdated value when use value " int (0) "

You have a few issues that need to be addressed.

  1. entity_id is not a valid configuration key for template sensors
  2. lastUpdated is not a valid attribute… in fact last_updated is not an attribute at all. As such it cannot be accessed using the state_attr(), you must use the state object method:
    When using the state object method it is necessary to assign an availability.
  3. You have included an else that returns nothing…

Additionally, you are using the legacy format instead of the current one and you are re-calculating values when you could just be using the variables you have defined.

template:
  - sensor:
      - name:  "window2_last_changed"
        state: >-
          {%- set time = (now() - states.sensor.window2.last_updated).total_seconds() %}
          {%- set minutes = ((time % 3600) // 60) %}
          {%- set hours = ((time % 86400) // 3600) %}
          {%- set days = (time // 86400) %}
          {% if days | bool %}
          {# more or = 1 day #}
          {{ days }}d 
          {% elif hours | bool %}
          {# minimum or = 23h and 59 min #}
          {{ hours }}t
          {% elif minutes | bool %}
          {# minimum or = 59 min #}
          {{ minutes }}m
          {% else %}
          0m
          {% endif %}
        availability: >
          {{ states('sensor.window2') not in [None, 'unavailable', 'unknown'] }}

Thanks a lot , template working without error , but lastUpdate time is wrong.
Now time is 14:12
sensor lastUpdate: 29.12.2022 13:57:66

But template sensor show 10.0m and after HA restart time reset and showing 0m. Template sensor must show time according to lastupdate time.

Time between 13:57 and 14:12 is 15 min.

template:
  - sensor:
      - name:  "shell_mortensrud_last_changed"
        state: >-
          {%- set time = (now() - states.sensor.shell_mortensrud_95.last_updated).total_seconds() %}
          {%- set minutes = ((time % 3600) // 60) %}
          {%- set hours = ((time % 86400) // 3600) %}
          {%- set days = (time // 86400) %}
          {% if days | bool %}
          {# more or = 1 day #}
          {{ days }}d 
          {% elif hours | bool %}
          {# minimum or = 23h and 59 min #}
          {{ hours }}t
          {% elif minutes | bool %}
          {# minimum or = 59 min #}
          {{ minutes }}m
          {% else %}
          0m
          {% endif %}
      availability: >
          {{ states('sensor.shell_mortensrud_95') not in [None, 'unavailable', 'unknown'] }}

If you don’t mind having the value displayed with a slightly different appearance, you can simply use the relative_time() function. Try it and see if it’s satisfactory.

template:
  - sensor:
      - name: "shell_mortensrud_last_changed"
        state: >-
          {% set dt = strptime(state_attr('sensor.shell_mortensrud_95', 'lastUpdate'), '%d.%m.%Y %H:%M:%S') | as_local %}
          {% set n = now() %}
          {{ relative_time(dt) }}
        availability: >
          {{ states('sensor.shell_mortensrud_95') not in [None, 'unavailable', 'unknown'] }}

Reference: Templating


EDIT

Correction. See petro’s post below.

2 Likes

need to have now() in it for minutely updates.

2 Likes

How can i change
hours to t
minutes to m
days to d

like right hand picture below

If you want to display time in that specific format, you can’t use relative_time and must calculate it.

template:
  - sensor:
      - name:  "shell_mortensrud_last_changed"
        state: >-
          {%- set dt = strptime(state_attr('sensor.shell_mortensrud_95', 'lastUpdate'), '%d.%m.%Y %H:%M:%S') | as_local -%}
          {%- set time = (now() - dt).total_seconds() %}
          {% set (minutes, hours, days) = ((time % 3600) // 60, (time % 86400) // 3600, time // 86400) %}
          {% if days | bool %} {{ days }}d 
          {% elif hours | bool %} {{ hours }}t
          {% elif minutes | bool %} {{ minutes }}m
          {% else %} 0m
          {% endif %}
        availability: >
          {{ states('sensor.shell_mortensrud_95') not in [None, 'unavailable', 'unknown'] }}

I want remove .0 in time format and tried {{ timetext.strip('.0') }} but .0 not removed. I want time like 1t, 16t, 7m, 30m( t called hours and m called minutes )
Screenshot_20221229_115118

{{ timetext.strip('.0') }}
{{ timestamp.strip('.0') }}

          {%- set dt = strptime(state_attr('sensor.shell_mortensrud_95', 'lastUpdate'), '%d.%m.%Y %H:%M:%S') | as_local -%}
          {%- set time = (now() - dt).total_seconds() %}
          {% set (minutes, hours, days) = ((time % 3600) // 60, (time % 86400) // 3600, time // 86400) %}
          {% if days | bool %} {{ days }}d 
          {% elif hours | bool %} {{ hours }}t
          {% elif time <= 3600 %} {{ minutes }}m
          {{ timetext.strip('.0') }}
          {{ timestamp.strip('.0') }}
          {% else %} 0m
          {% endif %}
template:
  - sensor:
      - name:  "shell_mortensrud_last_changed"
        state: >-
          {%- set dt = strptime(state_attr('sensor.shell_mortensrud_95', 'lastUpdate'), '%d.%m.%Y %H:%M:%S') | as_local -%}
          {%- set time = (now() - dt).total_seconds() %}
          {% set (minutes, hours, days) = ((time % 3600) // 60, (time % 86400) // 3600, time // 86400) %}
          {% if days | bool %} {{ days|int(0) }}d 
          {% elif hours | bool %} {{ hours|int(0) }}t
          {% elif minutes | bool %} {{ minutes|int(0) }}m
          {% else %} 0m
          {% endif %}
        availability: >
          {{ states('sensor.shell_mortensrud_95') not in [None, 'unavailable', 'unknown'] }}
1 Like