I have created a recurring event in my local calendar where I have added a payday event on the 25:th each month.
I would like to create a template sensor that tracks the days until the next payday. But I would also like to have it adjusted so that if the payday is on a saturday or sunday the payday will be on the previous friday.
Can anyone help me understand how to create this kind of template sensor ?
Sorry, it’s early for me. I guess i missed that part.
What I would suggest is to create a sensor that grabs the first next date from your calendar and stores that as a date + time.
Then check if it is a weekend day using the weekday() function. And I think it calculates from sunday being day 1. So if result is 1, to adjust that sensor to do take off 2 days and if its 7 to take off 1 day. That way you get the right reporting.
I am not at a computer right now, but if I manage to free up some time later today, I could provide something here (unless someone else beats me to it )
Thanks for the help, I tried the first example but I am to unskilled to understand how the code works.
Is this part correct ? {% set next_25 = (today + timedelta(days=(31-today.day))).replace(day=25).date() %}
As I said I don’t exactly understand the code but does 31-today.day always work ? I was thinking that a month also can have 30 or 28 days?
That actually should have been 32. The purpose of timedelta(days=(32-today.day)) is to add enough days to the current date to get to the next month, but not so many that is goes into the month after that. So we subtract the current day of the month from 32 than add the difference to the current day. Using 32 as the constant means it will always render as date between the 1st and 4th. After that the .replace(day=25) sets it to the 25th of the month.
However, I did find another issue with the template and have corrected it above. The previous version did not properly handle dates after payday this month but before the 1st of next month.
The calendar-based option is not affected since it’s getting the date value from the current or next calendar event. However, because of that, it will return incorrect values if you have matching calendar events on the wrong day.