# Week number as condition

I need a automation that runs every wednesday but only if the week number is even.

Post the automation youâ€™ve created and we can help you with it.

For example, this provides the week number:

``````now().isocalendar()[1]
``````

and this provides the weekday:

``````now().weekday()
``````

This condition checks the week number, if itâ€™s even. Change the `U` to `W` if you want your weeks to start on monday. Otherwise with the `U` they start on sunday.

``````- condition: template
value_template: "{{ not now().strftime('%U') | int % 2 }}"
``````

This condition does Wednesday.

``````- condition: time
weekday:
- wed
``````

EDIT, if you want only 1 conditionâ€¦

``````- condition: template
value_template: "{{ now().weekday() == 2 and not now().strftime('%U') | int % 2 }}"
``````

EDIT2: This appears to be 1 week off from `now().isocalendar()[1]`. So, if you want to check the iso weekâ€¦

``````- condition: template
value_template: "{{ not now().isocalendar()[1] % 2 }}"
``````

The kicker behind this is the % symbol. It means `mod`. When you mod a number, it returns the remainder if you were to divide the value by the modded number. I.e. if you divide 7 / 2, the remainder is 1. So if you had an even number, the remainder would be 0 and an odd number, the remainder is 1. 1 in jinja means true and 0 in jinja means false. So if we want to find even weeks, we want to look for `not 0`, as weird as it sounds.

3 Likes

In the spirit of helping others help themselves, donâ€™t overlook to use the forumâ€™s search function to find existing solutions.

For example, searching for â€śweek numberâ€ť reveals dozens of posts explaining how to calculate it and how to get even/odd weeks.
https://community.home-assistant.io/search?q=week%20number

1 Like

Iâ€™m pretty sure itâ€™s `now().weekday()`, not `now().weeday()`

weeday is always april 20th

I edited the post

3 Likes

really? why?

that was a crappy 420 joke, i try keep up with the memes

1 Like

So if I need to find uneven/odd weeks I should just remove the not?

``````- condition: template
value_template: "{{ not now().isocalendar()[1] % 2 }}"

``````

yep, thats it

But this gets never activated

``````- id: '57301120985673214'
alias: DALS change evening tuesdays uneven weeks
trigger:
- at: '17:45'
platform: time
condition:
- condition: state
state: home
- condition: template
value_template: "{{ now().isocalendar()[1] % 2 }}"
- condition: time
weekday:
- tue
action:
entity_id:
data:
message: Det Ă¤r skiftbyte snart, jag lĂĄser upp
language: sv
- service: input_boolean.turn_on
data:
entity_id: input_boolean.dals_change_period
- service: lock.unlock
data:
entity_id: lock.polycontrol_danalock_v2_btze_locked_2
- service: input_boolean.turn_off
data:
- service: input_boolean.turn_on
data:
``````

how could you have tested that already? You added this yesterday, which was thrusday and this week is the odd week. You have to wait 2 weeks if you keep the tuesday condition.

I asked yesterday because I noticed that it wasnâ€™t working in Thursday.

Itâ€™ll only work Tuesday because of your condition. It will not work Thursday.

Hey hoping I can add to this thread and get some help. Im trying to have my lights turn on at different times on alternating weeks. At the moment I have two automations with different conditions. It could probably be done in one but in any case this simple addition of the template condition causes the automations to fail to trigger. The script was working fine before splitting in two and addidng the template condition. Any suggestions?

``````- id: weekday_bedroom_light_1
alias: Weekday Light Before Sunrise 1
trigger:
- at: 05:50:00
platform: time
condition:
- before: sunrise
before_offset: -00:30:00
condition: sun
- condition: state
entity_id: binary_sensor.workday_sensor
state: 'on'
- condition: template
value_template: {{(now().strftime('%U') | int % 2) == 1}}
action:
- service: script.morning_routine

- id: weekday_bedroom_light_2
alias: Weekday Light Before Sunrise 2
trigger:
- at: 05:20:00
platform: time
condition:
- before: sunrise
before_offset: -00:30:00
condition: sun
- condition: state
entity_id: binary_sensor.workday_sensor
state: 'on'
- condition: template
value_template: {{(now().strftime('%U') | int % 2) == 0}}
action:
- service: script.morning_routine
``````

The automation is triggered at a specific time (05:50:00) then the first condition checks if the current time is a specific offset of sunrise. Unless that also happens to be 05:50:00 the first condition evaluates to `false` and the action isnâ€™t executed.

tl;dr
Youâ€™ve created a nearly impossible combination of trigger and (first) condition.

Thanks for your reply but its only the template condition that has broken this. Its been working for years otherwise. The â€śbeforeâ€ť condition is true if the current time is at least 30 mins before sunrise.

Where I live, sunrise is currently at 06:15. Thirty minutes before sunrise is 05:45. When the automation triggers at 05:50, thatâ€™s 5 minutes too late to satisfy the first condition.

Anyway, your template condition requires outer quotes. Like this:

``````value_template: "{{(now().strftime('%U') | int % 2) == 1}}"
``````

Hi Gues.
I am hoping to be able to use the week number in a slightly different way, than all the above and I can not see how to derive the right solution from the above.

I wan tto use the week number as a condition, like this:

``````- id: '1601361501256'
alias: Lys_Laura - arbejdsdag 0700 - tĂ¦nd planterne
description: ''
trigger:
- platform: time
at: 07:00:00
condition:
- condition: state
entity_id: binary_sensor.workday_sensor
state: 'on'

### AND only if WEEK_NUMBER is not (7,26-30,42) ###

action:
- service: light.turn_on
data: {}
entity_id: light.planterne
mode: single

``````

I believe something like this:

``````{{ now().isocalendar()[1] not in [7,26,27,28,29,30,42] }}
``````

This returns false now since itâ€™s week 42.
And that should make the condition false and thus not trigger.