Is there any way to trigger an action every 4th Monday, e.g. 2 Nov, 30 Nov, 28 Dec, 25 Jan, 22 Feb, and so on. I tried using a timer, but that does not work and it triggers every Monday.
See picture 1 below.
Pic 2 shows the piston. Monday is chosen in the timer, but does not show in the code. However, it is included since it only triggers on Mondays.
How to execute action every 4th Monday
Triggers on time can be tricky. Is it actually executing the logic inside the âeveryâ or just the following logic? I would suggest putting the âifâ inside the âeveryâ so it all gets executed only when the timer is true. Some logs would be helpful.
You could program that way, but it will be a lot of work with the dates constantly shiftingâŚ
I would probably start coding using âweeks of the yearâ⌠which always returns a number between 1 and 54. We can do math on this number, to determine which of the 4 weeks is current.
In the following example, it runs once a week to grab the current info. The variable
{week} will always return one of four numbers (1-4). The trick will be the initial sync, but once itâs calibrated, it will stay on track for the entire year.
For reference, the current week of the year is 42.
(insert Douglas Adams quip here)
Donât Panic
Line 22 is the calibration line⌠It should be an integer from 1-4, and remains static for the entire year.
Note:
Based on your previous statement:
every 4th Monday, e.g. 2 Nov, 30 Nov, 28 Dec
line 22 should use â2â this year and nextâŚ
(although it will likely need tweaking in 2022)
To clarify:
A 5 second edit may be required on January 1st to calibrate for the new yearâŚ
(or you can complexify
the code to do it automatically)
Edit: For those curious, hereâs the math breakdown for the next few weeks:
Oct 12 = Week 42 / 4 is 10.50... and 0.50 * 4 + 1 = Week 3
Oct 19 = Week 43 / 4 is 10.75... and 0.75 * 4 + 1 = Week 4
Oct 26 = Week 44 / 4 is 11.00... and 0.00 * 4 + 1 = Week 1
Nov 02 = Week 45 / 4 is 11.25... and 0.25 * 4 + 1 = Week 2 = ACTION
Nov 09 = Week 46 / 4 is 11.50... and 0.50 * 4 + 1 = Week 3
Nov 16 = Week 47 / 4 is 11.75... and 0.75 * 4 + 1 = Week 4
Nov 23 = Week 48 / 4 is 12.00... and 0.00 * 4 + 1 = Week 1
Nov 30 = Week 49 / 4 is 12.25... and 0.25 * 4 + 1 = Week 2 = ACTION
etcâŚ
The earliest a fourth Monday can be is the 22nd and the latest is the 28th.
You could use this method for any day of the week on any week of the month. Just change the day of the week and the date range to match.
First week: 1-7
Second week: 8-14
Third week: 15-21
Fourth week: 22-28
Fifth week: 29-31
They do not want the 4th Monday of the monthâŚ
They want to know about every 28 days.
(IE: Twice this November⌠the 2nd and the 30th)
Yes, give yourself some creditâŚ
Your idea is good for events that happen only once a monthâŚ
I believe this formula should always give you the 4th Monday from today:
addHours(date(addDays($now,(29-$dayOfWeek))),4)
You could use that to set a timer if desired. Could have been simpler but date() and datetime() donât appear to work properly on strings.
While I think that formula is quite eloquent, unfortunately, webCoRE / SmartThings has issues with any trigger
too far in the future. If memory serves me correctly, any trigger
more than 3 weeks in the future is unreliable.
This is why my example forced a weekly run, with every fourth actually taking action.
OK, I remember I tried to do daylight savings time triggers which are 6 months apart and that didnât work. I will keep an eye on this and let you know my experience. Right now I notice it already has an extra hour in the countdown to account for the time change.
My formula also has a start up issue in that, whenever you start it, it wonât run until the 4th Monday. Really need some sort of initialization.