Rules / Variables based on Date or Calendar

One thing to keep in mind is that triggering a rule via HTTP enqueues it to run and returns a response if it was successfully queued up to run. It doesn’t wait for the rule to finish before it returns.

You can still use this concept, but would either want to have the primary rule that does all the variable calculations just run first thing in the morning every day (eg. 12:05 AM) or have the other rules introduce some delay after they execute the HTTP Action to the other rule.

Ah, thanks for that info. I’ll make some adjustments.

1 Like

Josh, can you help me with the syntax for the payload on this please (JSON is relatively new to me).

I currently have:

{
"formula": "pickRandom(["Green","Red","Gold","Blue","whiteTemperature"])"
}

But it’s obviously not right as the editor is not letting me save.

The first quote before Green is causing the formula to become unquoted. You would either need to escape the quotes with a backslash or just switch to single quotes.

{
"formula": "pickRandom(['Green','Red','Gold','Blue','whiteTemperature'])"
}
1 Like

Doh! Thanks Josh, that did it.

1 Like

I’m trying to check if a single date is true or false but can’t get it right. The variable remains blank.

https://lab.sharptools.dev/date=09-02&timezone=America/Phoenix

I also tried using the date range with one date. This results in true when set to today but the variable is empty when set like this.

https://lab.sharptools.dev/date/between?start=09-02&end=09-02&timezone=America/Phoenix

What am I doing wrong?

Which variable is empty? The second approach would be the right approach for checking if it’s currently a specific day.

You would access the variable as $context.response.data.result

In other words: Variable > Context Tab: Response → HTTP → Data, then enter result as the property you want.

Apparently it only works when you spell result correctly :woman_facepalming:t4:

1 Like

Could this be used to pick a random light and/or random time by replacing color with device names or list of numbers?

The pickRandom() formula can be used to pick a random item from a list. So you could have a list of random Device Names or IDs, but the Device Command action in Rules doesn’t allow using a variable for the Device ID.

This thread has been immensely helpful!

Is there a way to pull Holiday dates out of calendar and assign them to variables? Preferably a publicly accessible calendar with major US holidays? …as opposed to manually defining them each year?

Is there an easy way to set If day (date) is odd or even?

Would you be able to share the rule you created for this? I’m attempting to do the same thing, having lights with colors based on holidays, but having a bunch of trouble getting the date checking part nailed down.

Here’s the rule I created to set the variables:

Hoping someone can help me with formatting. I know it’s a nesting issue but not sure what I need to add or where to get the hour and minutes.

I am trying to get the result of 4:30 but am doing something wrong as I am only getting 4

formatDate(setDatePart(startOfDate(now()), 'hour', 4, 'minute', 30), 'MMM D h:mm a') // 4:00

From the documentation, we see that the setDatePart() function accepts three parameters:

Normal Approach (recursive)

So if you want to set multiple date parts, you have to call the method twice:

            setDatePart(now(), 'hour', 4)
setDatePart(                             , 'minute', 30)
↓          ↓          ↓          ↓          ↓          ↓
setDatePart(setDatePart(now(), 'hour', 4), 'minute', 30)

Hardcoding an Output Time

Alternatively, you can use format strings with hard coded values as a trick. So if you’re only trying to get the output format. Then using your example where you want today’s date but the timestamp at 4:30, you could do:

formatDate(now(), 'MMM D 4:30 [am]')

Hardcoding Time for Parsing

That same trick can be used to parse a particular date/time. Here’s the final version and following that is the breakdown with some comments:

date(formatDate(now(), 'YYYY-MM-DD 16:30'))

And the breakdown:

                now()                       //the current time in millis
     formatDate(     , 'YYYY-MM-DD 16:30')  //Format into ISO format, but hardcode the time
date(                                     ) //parse the ISO formatted string as a date (millis)

We’re using a bit of a shortcut in that last step. Since the date() function tries to parse in ISO format if a format string is not explicitly provided, we use ISO format in the output of the formatDate() call. We use 24 hour time which is the default with ISO (I used 4:30 pm in this example to demonstrate).

You could use a different output format and then match that when calling the date(input, format) function with a specific format to parse, but using ISO works well and simplifies some of the verbosity.

@josh has been helping me A LOT in creating some rules to replace my morning alarm webcore pistons. I have these set up to display what time the alarm will go off as well as make an Alexa announcement that the alarm will go off in X hours and X minutes. I use these for someone who is disabled so they can hear the times. Thought I’d share it in case it helps anyone else.

I have ST virtual switches for specific wake up times. 4:30am, 5am, 6am, 7 hrs. These are linked to a remote so they can easily be triggered to turn on. I also have a ST virtual switch which is triggered when everyone goes to bed.
To make this a fully functional alarm clock I am using a total of 2 rules per time (this one to set the alarm and another to do all the things when the alarm goes off in the morning) and 2 rules to set additional variables to compare before or after midnight described in [red] in the screenshot.

1 Like

The expression feature is now available in beta as native feature (no more HTTP requests required!). If you’re part of our beta program, you should be able to see the post in the beta category.

:tada: Update: Math / Date Expressions are now available to everyone!

:link: Feature Announcement: Math / Date Expressions

1 Like

This is awesome and slightly depressing! I wish I would’ve waited to build that long complicated rule you just helped me with! :rofl:

Will I still need the “timezone” or will that be based on my location?

1 Like

It pulls the timezone from your location. Details are in the linked thread. :grin:

The hard part for most people is getting the syntax figured out. The beauty is you have that done and it should all transfer over to this and streamline your rules!

Edit: Or you can continue to use your existing version!

1 Like