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
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:
Year
ISO week of the year
ISO day of the week
If we want year or day of the week, it’s easier to do this:
now().year
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.