Script for last 3rd business day of the month

I am looking for a script to display a countdown until the last 3rd business day of the month, each month. For example, this month would be the 29th, and February would be 26th, March would be the 27th, and so on and so forth.
Ideally, I would like to have a tile that displays a countdown for it. For this, I’ll call it “Month End” and the tile should look like:

Month End
in 8 days

Month End
Today

Month End
in 32 days

If anyone can help me with this, it would be greatly appreciated! :slight_smile:

Also, I am using the TrashCard integration from HACS, and considered using a chip to display it if I can figure out a way to make it work.

I adapted @petro’s answer to a slightly different question to get the result you are looking for:

This template will get the number of days until the next 3rd last business day of the month. For the other formatting you described you can use a markdown card, or add it to this template.

{% set biz_day_offset = [-4, -4, -2, -2, -2, -3, -4] %}
{% set next_month = now().date().replace(day=28) + timedelta(days=4) %}
{% set third_month = next_month + timedelta(days=32) %}
{% set last_day_this = next_month - timedelta(days=next_month.day) %}
{% set last_day_next = third_month - timedelta(days=third_month.day) %}
{% set until_last_biz_day_this = (last_day_this + timedelta(days=biz_day_offset[last_day_this.weekday()]) - now().date()).days %}
{% set until_last_biz_day_next = (last_day_next + timedelta(days=biz_day_offset[last_day_next.weekday()]) - now().date()).days %}
{{ until_last_biz_day_this if until_last_biz_day_this >= 0 else until_last_biz_day_next }}

The business day offset basically says that if the last day of the month is Monday, you have to subtract 4 days to get to the third last business day (Thursday, in that case). Same with Tuesday (Friday), then Wednesday only needs to subtract 2 (Monday), … and so on.

FYI you can use easy time to do this.

{% from 'easy_time.jinja' import last_day_in_month %}
{{ last_day_in_month(now().month, 3) }}

if you want to count the days to that date

{% from 'easy_time.jinja' import last_day_in_month, count_the_days %}
{{ count_the_days(last_day_in_month(now().month, 3) | as_datetime) }}
1 Like

Isn’t that always going to return a Wednesday, not the third-to-last business day of the month?

For example April 2025 returns the 30th, not Monday the 28th…

I only checked January and Feburary and it seemed to work. Shame I didn’t check further. Anyways,I can probably add a new macro, but OP can use arts in combination with count_the_days.