Macro to be run on last Sunday of the month

mmjoshi

Member
I want to run a macro on every last Sunday of the month at a specific time. Is there any way to determine the last Sunday or for that matter any day which is the last day of the month?
 

dsnz

Well-known member
assume y = year, m = month, d = day
the following is the simplest that can be implemented in macrodroid to return day of week (as an integer 0..6, 0=Sunday)

if m < 3 then d = d + y, y = y - 1
else d = d + y - 2

day = (23*m/9+d+4+y/4-y/100+y/400)%7

run the above for last day of month (eg. 2022-1-31, ie d is 30 or 31 or 28/29 for Feb)
since you want day 0 (Sunday) , then remove {day} days from original d and you get the final d of last Sunday of month

an alternative without calculations is to use http get to a site that does calendar calculations

the macro setup is that you trigger on every Sunday then with the above code you check if it's the last Sunday of the month and only then you run your code

Update:
MD should have some time/date built-in functions
plus arrays and array operations
 

Dm114

Well-known member
I want to run a macro on every last Sunday of the month at a specific time. Is there any way to determine the last Sunday or for that matter any day which is the last day of the month?
Another way (attached)...
 

Attachments

  • IMG-20220104-WA0011.jpg
    IMG-20220104-WA0011.jpg
    51 KB · Views: 41

Dm114

Well-known member
Another way (attached)...
The logic is quite simple: if today's date + 7 days (according to the day of week selected in the trigger) is in the same month as current date, i e. today is not the last occurrence of the month. Otherwise, it's the last one because next one will occur on another (next) month/year.
 

Qarboz

Well-known member
The logic is quite simple: if today's date + 7 days (according to the day of week selected in the trigger) is in the same month as current date, i e. today is not the last occurrence of the month. Otherwise, it's the last one because next one will occur on another (next) month/year.
Very beautiful
But I don't understand where the string variable 's' is written.
Could you please explain?
 

Dm114

Well-known member
Very beautiful
But I don't understand where the string variable 's' is written.
Could you please explain?
Local string variable 's' is used to store the result of the Shell script date. As it returns a 2 digit string value (from 01 to 12) it is then converted to a numeric value to be compared with the Magic text [month_digit] which is numeric.
 

dsnz

Well-known member
it's assigned in the shell script command
it's not shown visually, you must enter into the configuration to see this
 

Qarboz

Well-known member
Local string variable 's' is used to store the result of the Shell script date. As it returns a 2 digit string value (from 01 to 12) it is then converted to a numeric value to be compared with the Magic text [month_digit] which is numeric.
Clear, thanks.
It's possible see the script, just to learn?
 

mmjoshi

Member
Great solution. Came to know about shell scripts for the first time. Will have to wait for the last Saturday of this month to test this out. I am sure it will work.
 

mmjoshi

Member
Sure. Please, find it attachedI
Sure. Please, find it attached.
next2

I have modified this script to run a Macro every last Wednesday of the month at 10 AM. It was working fine till last month. However, I was surprised thar it got executed today at 10 AM!

I am attaching the relevant screenshots. What could be wrong?
 

Attachments

  • Screenshot_20220817-123459.png
    Screenshot_20220817-123459.png
    162.6 KB · Views: 19
  • Screenshot_20220817-123519.png
    Screenshot_20220817-123519.png
    155.5 KB · Views: 22

dsnz

Well-known member
[month_digit] has value 08 (notice the leading 0)

however cal does not like the leading 0 and returns

cal: not integer: 08

the text extractions that follow return 08 (the last number in the above error message)

and the resulting calculation is 8 - 17 => -9

and then everything is wrong (well it was wrong from start 😂)

you must remove the leading 0 from the month
 

mmjoshi

Member
[month_digit] has value 08 (notice the leading 0)

however cal does not like the leading 0 and returns

cal: not integer: 08

the text extractions that follow return 08 (the last number in the above error message)

and the resulting calculation is 8 - 17 => -9

and then everything is wrong (well it was wrong from start 😂)

you must remove the leading 0 from the month
Thanks for the explanation. Being a newbie can you please let me know how to remove the leading 0?😬
 

Jacob L

Moderator (Lawsonator)
Thanks for the explanation. Being a newbie can you please let me know how to remove the leading 0?😬
Text manipulation action > replace>source text would be a variable, text to replace is 0 and text toceeplace it with leave blank. Set the output to the variable
 

mmjoshi

Member
Text manipulation action > replace>source text would be a variable, text to replace is 0 and text toceeplace it with leave blank. Set the output to the variable
Got it. Had to use regular expression so as to avoid replacing the zero for the month of October.

Thanks.
 

janokv

New member
Please, find just published "Run actions on last Wednesday of the month" macro on Templates.
 
Last edited:
Top