Template binary_sensor + time_pattern doesn't seem to work

What am I doing wrong here?

I’m trying to make a binary_sensor that goes dirty=True after some count of days post vacuum. I want it to update daily (like hours:0 and minutes:0, which I had) but I noticed that the sensor values never changed from Off. I even went in and swapped the less-than to a greater-than and back. Because it’s not an automation, I can’t tell if it’s even triggering. I tried making the trigger every ten minutes but it still doesn’t change. I’ve verified that the equation evaluates to True in the template editor… I’m stuck now. :expressionless:

template:
  - trigger:
      - platform: time_pattern
        #hours: 0
        minutes: /10
    binary_sensor:
      - name: Vacuum Living Room Dirty
        state: >
          as_timestamp(strptime(states('input_datetime.vacuum_living_room_last_cleaning'), '%Y-%m-%d') + timedelta(days=states('input_number.vacuum_days_before_dirty')|int)) - as_timestamp(now()) > 0
      - name: Vacuum Living Room Forced Clearning
        state: >
          as_timestamp(strptime(states('input_datetime.vacuum_living_room_last_cleaning'), '%Y-%m-%d') + timedelta(days=states('input_number.vacuum_days_before_dirty')|int) + timedelta(days=states('input_number.vacuum_days_before_forced_cleaning')|int)) - as_timestamp(now()) > 0

I don’t see how that could be true… Your templates don’t have any curly brackets, so they’re not being interpreted as expressions or statements that need to be rendered, just as normal strings.

Also you’re doing a bunch of unnecessary conversions.

template:
  - trigger:
      - platform: time
        at: "00:01:00"
    binary_sensor:
      - name: Vacuum Living Room Dirty
        state: >
          {% set diff = timedelta(days = states('input_number.vacuum_days_before_dirty') | int(0)) %}
          {% set last = states('input_datetime.vacuum_living_room_last_cleaning') | as_datetime | as_local %}
          {{ now() >= last + diff }}
      - name: Vacuum Living Room Forced Cleaning
        state: >
          {% days_sum = states('input_number.vacuum_days_before_dirty') | int(0) + states('input_number.vacuum_days_before_forced_cleaning') | int(0) %}
          {% set diff = timedelta(days = days_sum) %}
          {% set last = states('input_datetime.vacuum_living_room_last_cleaning') | as_datetime | as_local %}
          {{ now() >= last + diff }}

Thanks for the response! This message sounded so great, it had to be right but I just tried it like this (both with greater-than and less-than signs) and it still only shows Off state. :frowning:

template:
  - trigger:
      - platform: time_pattern
        #hours: 0
        minutes: /10
    binary_sensor:
      - name: Vacuum Living Room Dirty
        state: >
          {{ as_timestamp(strptime(states('input_datetime.vacuum_living_room_last_cleaning'), '%Y-%m-%d') + timedelta(days=states('input_number.vacuum_days_before_dirty')|int)) - as_timestamp(now()) < 0 }}
      - name: Vacuum Living Room Forced Clearning
        state: >
          {{ as_timestamp(strptime(states('input_datetime.vacuum_living_room_last_cleaning'), '%Y-%m-%d') + timedelta(days=states('input_number.vacuum_days_before_dirty')|int) + timedelta(days=states('input_number.vacuum_days_before_forced_cleaning')|int)) - as_timestamp(now()) < 0 }}

Do you see anything else amiss?

BTW: I need the conversions because otherwise the statement throws errors about mismatched datatypes. Try it in the Developer Tools->Template thingie.

Am I doing “a bunch of unnecessary conversions”? I’m using int a couple of times here. I guess I just don’t see what you mean by that comment. Please expound on this comment for my understanding, if you please.

{{ (states('input_datetime.vacuum_living_room_last_cleaning') | as_datetime | as_local + timedelta(days=states('input_number.vacuum_days_before_dirty') | int(0)) - now()).days < 0 }}

or

{{ ((states('input_datetime.vacuum_living_room_last_cleaning') | as_datetime | as_local).date() + timedelta(days=states('input_number.vacuum_days_before_dirty') | int(0)) - now().date()).days < 0 }}

wait… I think this did work. For some reason it just took a lot longer than one 10 minute cycle to apply the change. Eventually, the sensor flipped. I guess it was because I didn’t use curly braces.

Thank you!