Problem with templating (if, and) in sensor

Hello everyone! I have a bit of a problem with a template sensor. I’m not great with jinja but i managed to get a sensor up and running for my trash pickup which is a bit special.

We have pickup every other week during most of the year, but during the week numbers 25-34 the trash is picked up every week. The sensor changes status when i select that the trash is taken out via the input boolean. Its been working great, until week 25 this year, now no matter if i select trash taken out the sensor still says “Put out the trash”.

Really thought i nailed the template when i created it. I have spent several hours trying to figure out how to write it correctly, would gladly appreciate any help!:frowning:

sensor:
  - platform: template
    sensors:
      sophamtning:
        friendly_name: Trash Pickup
        value_template: >
          {% set wk = strptime(states('sensor.date'), '%Y-%m-%d').strftime('%V')|int %} 
          {% if is_state('input_select.trash_taken_out', 'No') and (now().isocalendar()[1]/2)|round(0)==(now().isocalendar()[1]/2) or wk >= 25 and wk <= 34 %}
          Put out the trash!
          {% else %}
          No
          {% endif %}   
          
  - platform: time_date
    display_options:
      - time
      - date
      - date_time
      - time_date

input_select:
  trash_taken_out:
    name: Are the trash taken out?
    options:
     - No
     - Yes
sensor:
  - platform: template
    sensors:
      sophamtning:
        friendly_name: Trash Pickup
        entity_id:
          - sensor.date
          - input_select.trash_taken_
        value_template: >
          {% set wk = now().isocalendar()[1] %} 
          {{ 'Put out the trash!'
             if is_state('input_select.trash_taken_out', 'No')
             and (wk % 2 == 0 or (25 <= wk <= 34))
             else 'No' }}

The template works like this:

  • It sets wk to week of the year.
  • It reports Put out the trash! if the input_select is No AND wk is an even number OR if it’s between 25 and 34 (inclusive).
  • Otherwise, it reports No.

EDIT
Correction. Added entities to monitor as per Phil’s suggestion (see below). Listeners are assigned to entities whereas now() is a function, not an entity.

Probably need to add:

        entity_id:
          - sensor.time
          - input_select.trash_taken_out

Do you have a precedence issue in your if statement?

{% if is_state('input_select.trash_taken_out', 'No') and ((now().isocalendar()[1]/2)|round(0)==(now().isocalendar()[1]/2) or (wk >= 25 and wk <= 34)) %}

Why do you use two different ways to get the week number?

Long way:

set wk = strptime(states('sensor.date'), '%Y-%m-%d').strftime('%V')|int

Short way:

now().isocalendar()[1]

FYI, the following expression to determine if the week number is an even number:

now().isocalendar()[1]/2)|round(0)==(now().isocalendar()[1]/2

is the same as this one which uses integer division:

wk % 2 == 0

Lastly, this format:

          {% if is_state('switch.something', 'on') %}
            It is on.
          {% else %}
            It is off
          {% endif %}

is the same as this:

          {{ 'It is on.' if is_state('switch.something', 'on') else 'It's off' }}

or this:

          {{ 'It is on.'
              if is_state('switch.something', 'on')
              else 'It is off' }}

OMG sorry! Did not see your post when i viewed it on my phone before only saw pnbruckners post and andrewdolphin. I’m now using your example instead. So much easier to read, and i now understand what i did wrong! Thanks a million! :smiley: