Monday through Thursday and Every Other Friday

I have an automation that I would like to run Monday through Thursday and every other Friday. I can accomplish this using two separate automations, but I challenged myself to make it happen with just one automation.

I created this condition, but I am unsure if it will work.

  condition:
  - condition: or
    conditions:
    - condition: time
      weekday:
      - mon
      - tue
      - wed
      - thu
    - condition: template
      value_template: '{{(as_timestamp(now())|timestamp_custom (''%U'') | int % 2)
        == 0 }}'
    - condition: and
      conditions:
      - condition: time
        weekday:
        - fri

If you’re open to using a Template Condition then this ought to do what you want:

condition:
- condition: template
  value_template: "{{ now().isoweekday() in [1,2,3,4] or (now().isoweekday() == 5 and now().isocalendar()[1] % 2 == 1) }}"

BTW, the mistake in your condition is that the condition: and is in the wrong place.

1 Like

could you elaborate on what that does? thanks in advanced

Sure, isocalendar() is one of the methods of a datetime object. It’s described here.

Basically it returns a list containing three integers representing the:

  1. Year
  2. ISO week of the year
  3. ISO day of the week

Screenshot from 2021-05-02 17-17-07

If we want year or day of the week, it’s easier to do this:

  1. now().year
  2. now().isoweekday()

However, ‘ISO week of the year’ requires we get it from the value returned by isocalendar(). It’s the second item in the list so we use an index of [1] because lists are zero-based (first item is the zeroth index).

now().isocalendar()[1]

In this case, we want to determine if the ‘week of the year’ is an even or odd number so we divide by 2 and examine the remainder. If it’s an even number, the remainder will be 0 whereas for an odd number the remainder will be 1. To perform this calculation, we use the % modulo operator.

NOTE

This:

{{ now().timestamp() | timestamp_custom ('%U') }}

and this:

{{ now().isocalendar()[1] }}

will return the current ‘week of the year’ number but they calculate it differently so the two values are not necessarily the same.

For example:

Screenshot from 2021-05-02 17-32-12

2 Likes

I appreciate