LS' Utili-Teasers #001 ► Finding "Day of the Week"

What Day of the Week Is It?

Inspired by @len's question Get day of week in app?, I thought it would be fun to compile a few ways to conjure up the current Weekday name in HE. The result could be used to set a Variable value, populate a Dashboard tile, Trigger a rule, or decide logic flow in a Conditional (this function also available in RM), etc. based on the given day.

Why Do I Need This?

While Rule Machine possesses many native DateTime-based Triggers and Conditionals, and can produce a limited number of time/date/epoch/timestamp/%event% values, there is currently no Variable function that explicitly returns a given calendar day as, say, "Wed" or "Wednesday".

Show Me the Secret

Here you go! I'll update this post with alternative methods as they come to my attention...

Using Rule Machine 5.1 - Basic


Very obvious how this one works; essentially a CASE-SWITCH approach using a series of seven simple IF-THEN clauses.
Author: @LibraSun

Using Rule Machine 5.1 - Intermediate



Here, RM simply counts the number of days between Time Zero (a Thursday) and the specified date, then picks the corresponding day name from a tokenized list, using a calculated index (modulo 7).

Using Rule Machine 5.1 - Advanced (*broken?*)

In principle, one should be able to perform an HTTP GET Request to a special URL —
for example, World Clock API, or AIMyLogic — with the appropriate Time Zone and/or Format Code, to populate a local RM variable with the plain-text JSON results.
However, despite rigorous testing, I have been unable to capture any result (JSON, text or otherwise) in this fashion. Can anyone show me a working example?
Author: TBA

Using a Community Driver

[RELEASE] Date Time Parser driver (aka schedule_ur_garbage_cans)
This one's my favorite, with countless date/time attributes made available on the child device.
Author: @jshimota

Using WebCoRE


Here, a daily piston runs just after midnight to set a Hub Variable to the current Day of the Week, using a DateTime() expression called on system clock.
Author: @LibraSun

Using Groovy code

A working example in Groovy
This approach leverages JVM's native DateTimeFormat library via Groovy script.
Author: @bptworld

Tune in Tomorrow...

Got any more novel approaches to share?

2 Likes

Download the Hubitat app