Templating Error - Duration of Sensor State

Hi All,

I want to create a sensor that goes ON depending on how long another sensor has been ON.

I have this YAML code for a binary sensor in my template.yaml:

  - name: "Wasserzähler Problem bereinigen"
    unique_id: "Wasserzaehler_Problem_bereinigen"
    state: >
      {% if is_state('binary_sensor.wasserzaehler_problem', 'on') and (states.wasserzaehler_problem.last_changed < (now() - timedelta(minutes=120))) %}
        {{ 1 }}
      {% else %}
        {{ 0 }}
      {% endif %}   

Unfortunately, I get the following error message in the log - where is my error?

TemplateError(‘TypeError: ‘<’ not supported between instances of ‘NoneType’ and ‘datetime.datetime’’) while processing template ‘Template<template=({% if is_state(‘binary_sensor.wasserzaehler_problem’, ‘on’) and (states.wasserzaehler_problem.last_changed < (now() - timedelta(minutes=120))) %} {{ 1 }} {% else %} {{ 0 }} {% endif %}) renders=8>’ for attribute ‘_state’ in entity ‘binary_sensor.wasserzahler_problem_bereinigen’

Thanks a lot

Missing binary_sensor.:

states.binary_sensor.wasserzaehler_problem.last_changed
------^^^^^^^^^^^^^^

Also, if this is a binary sensor, you can just do:

    state: >
      {{ is_state('binary_sensor.wasserzaehler_problem', 'on') and 
         (states.binary_sensor.wasserzaehler_problem.last_changed < (now() - timedelta(minutes=120))) }}

Oh, thank you very much - a stupid mistake!

It works !