Is there any way I can make a template sensor true every last Wednesday of the month (for my bin schedule).
I saw this post but I can’t adapt it to work in my situation: Turn on switch on every 4th sunday at 7:30am
Is there any way I can make a template sensor true every last Wednesday of the month (for my bin schedule).
I saw this post but I can’t adapt it to work in my situation: Turn on switch on every 4th sunday at 7:30am
Not super pretty… but this should work
{% set current_month = now().strftime("%m") %}
{% set month_in_a_week = (as_timestamp(now()) + 604800) | timestamp_custom("%m") %}
{% if now().strftime("%a") == "Wed" and current_month != month_in_a_week %}
True
{% else %}
False
{% endif %}
Thank you so much for your help. It works incredibly well.
Very nice! I like the way you solved how to determine if the current day lies within the last week of the month (by checking if in 7 days it’s a new month). Clever!
All I can offer is to trim away a few stray hairs:
{% set month_in_a_week = (as_timestamp(now()) + 604800) | timestamp_custom("%m") %}
{{ now().isoweekday() == 3 and now().month != month_in_a_week }}
@123 Just in quick note, in your template “now().month” has to be replaced with “now().strftime(”%m")" to work (otherwise it compares a string with a number and it return always true)
You’re right about it being an invalid comparison between an integer and a string. I would just cast the string to an integer using int
.
now().month != month_in_a_week | int
However, this topic is over 2 years old and there are now other ways to calculate the last Wednesday of the month.
{{ now().isoweekday() == 3 and now().month != (now() + timedelta(days=7)).month }}