How to create Day of Week string

Searching for Day of Week string, I was surprised that it is not built into Rules 5.1. So far I have avoided having to do anything outside of 5.1. I may have taken some extra steps, but I wanted to make things clear to anyone using this code.

See screen shots below.


1 Like

Or you can create a virtual device that uses this driver:

I knew this reminded me of earlier work . . . TTS announcement of day of week

I prefer to use things that are either supported by the manufacturer, or that are under my control. This is not always possible, but in this case it is.

I don't know how to write or fix a driver. I still hold out hope that I don't have to learn these things.

In the spirit of consolidation without obfuscation, I offer this somewhat condensed version of the OP rule, which seems to work:


The variable Now is of DateTime, while DOW is a number and Days is a String as follows:

3 Likes

The above info and solutions are great/clever--well done!

As an FYI/alternative, here's another approach including a short day string that I use:

Thanks for your cleanup. I did not see the "%" operator in the list. In that I plan to use DOW and Day_Of_Week in other places, I choose to keep things in what seems logical to me. Here is what I came up with:

Set dtToday to current date(2025-12-21)
Set Epoch to epoch time (dtToday)
Set DOW to (Epoch(1766293200000) / 86400000)
Add 5 to DOW
Set DOW to (DOW(1) % 7)
If DOW = 0
    Set DOW to 7
Endif
Set Day_Of_Week with token(DOW(1)) delimited by '/' from variable: StringOfDays(XXX/Sun/Mon/Tue/Wed/Thu/Fri/Sat)

This way, I can have global variables DOW and StringOfDays, use them in other routines, and read them as my mind expects them to be.

1 Like

You don't show where the variable "Day" comes from.

I defined it as a global. The condition is built in. I define many other globals like this that I treat as essentially system global vars (that as others have implied would ideally already be there): day, shortDay, month, year, etc.

I get your point but in this case I think it is misguided, IMO. Using this driver is by far the easiest method and once downloaded it is in your control. The driver also has many other great features that can be very useful in handling date and time type rules. Once you download, it will not change unless you decide to change it.

That is one of Hubitat best features. Nothing on your system ever has to change just because there is an update. If you decide you want try an update and it doesn't work it's a simple task to roll back to where you were.

You could always ask for a new Format DateTime output of Day of Week (something Webcore already accommodates, btw) in Rule Machine. It's built into Java/Groovy after all; I think the first DEC mainframe in our office had the system function WEEKDAY(), so it's not an unusual request IMHO.

2 Likes