Simple Automation - Feature Request - Turn On/Off based on Odd/Even # Day of the year

Would like to change the state of a switch based on the odd/even numeric sequence of the days of the year.

For example, on Jan 1 (Day #1 ) an odd day, I would like that switch ON for that entire day until crossing past midnight when I would like it to go OFF.

In this particular case I would be changing a Virtual Switch which I would use in one or more other applications or rules.

OK with Leap Year impact.

Thanks for considering.

Out of interest, can you give a real life example of how or why you might use this?

Anything you want to cycle every other day. Irrigation being one of them.

Actually an alternative to burying this within SA might be a Global Variable that offered the day number.

I have some medication I am meant to take on alternate days, but because the dosage regime wasn't critical I actually take it on 4 set days of the week. I guess the criticality of irrigation interval might depend on how sensitive your plants were.

Simple enough to make a driver for this if you like:

...
attribute "evenOdd", "string"
....

dateNow = new Date()
dayOfYear = dateNow[Calendar.DAY_OF_YEAR]
if(dayOfYear % 2 == 0) {
   sendEvent(name:"evenOdd", value:"even")
else
   sendEvent(name:"evenOdd", value:"odd")
1 Like

It sounds like you need a daily toggle, not the actual calculation based on date. How about making a virtual switch with a rule that toggles it every day at sunrise, then using the state of that switch in any other logic as needed?

4 Likes

This would also handle leap-years without a hiccup!

2 Likes

Event Engine already does this.

1 Like

It does indeed. Thanks for that. You consistently do great work...

But refer to my now deleted discussion about where the flip a new user should choose to learn & do the myriad of things one wants to do; paying particular attention to the concern about the support and longevity of any one of the more helpful yet functionally complex community add-ons to HE.

With ALL due respect, as I continue to use some of what you've written :crazy_face:

Could this solution "get lost" in its' toggle sequence by some unforseen system lock up or power cycle ?

I suppose it could. The system would have to be down when the trigger point was crossed. But I guess the same could come about if a driver or app trigger happened at the same time. Either way, the transition would happen once per trigger (midnight, or sunrise, or whatever). The advantage of actually calculating it rather than just toggling would be that it would eventually 'correct' itself in phase, if that really mattered.

Just to make sure... using odd and even days of the month will also cause issues at the end of every month with 31 days in it... You'll have two ODD days in a row... 7 to 8 months of the year (depending on leap years)

Thus, I like @tomw's idea of simply toggling a virtual switch once per day. It should provide a much better 'every other day' result, if that is important.

2 Likes

Ah, good catch. Depending on the power supply, etc, reliability for your hub, this one seems a lot more likely to bite you regularly than an occasional hiccup on toggling.

2 Likes

Full source of a simple driver to toggle a switch even and odd based on day of the year:

https://raw.githubusercontent.com/thebearmay/hubitat/main/evenDaySwitch.groovy

Resets the switch every night at midnight and at reboot of the hub.

2 Likes

Hold the bus guys. I'm not sure we're talking bout the same "Day #".

The year has 365 days (366 in a leap year). Refer to his statement evaluation for the even/odd determination based on the "day number"

See this if that % happened to look greek to anyone :crazy_face:

1 Like

Can also use dayOfMonth if that's what you want.

or weekOfYear :grinning:

Bingo.

Fit for purpose, multiple actually.

Thank you!

No problem, I’ll add a switch to handle the day 365 to day 1 issue either tonight or tomorrow - it’s a simple matter of reversing the even-odd switch behaviour for the year.

Edit: Day 365 to day 1 code is in place, v0.0.2

2 Likes

Ahhh, very good point! Using day of the year makes perfect sense.

Sorry for the distraction…

Had to rebuild a broken/corrupted rule today that uses your Even/Odd Day switch (got myself into a bad edit pickle and couldn't get back in to the rule past a reoccurring error I somehow introduced, nothing to do with your stuff).

It's been awhile, I had forgotten how this was a simple yet elegant solution.

Want to reiterate that this was a REALLY helpful add to HE and your ability to produce this kinda stuff, seemingly at the drop of a hat, is appreciated.

Thanks.

1 Like