Rules / Variables based on Date or Calendar

I have the same request as the OP. I have many webCoRe pistons I want to convert that handle holiday lighting. My Christmas rules are the most complex and they require more than just date ranges but as a start being able to specify a range will get me some way there. I have simpler requirements for Valentines, July 4th and Halloween. Being able to set these dates in variables and then use those as ‘in between’ date IFs would be a winner.

Can you share more details about what the more complex automations would require?

Absolutely Josh.

Actually, what I’m looking for here for my Christmas rule I could also use elsewhere. It’s just this Christmas one is what is bothering me the most right now as it’s my most complex.

The date range as a condition is the biggest item. Then I want to be able to code that at certain times on certain days but only when the date range is true do something. Similarly, I want to trigger x mins before sunset, again only within a date range, do something else. Being able to set multiple devices in a variable is another plus.

Some of my issues are having to create multiple rules for what is currently a single piston. Not a huge deal but very tedious and often requires extensive testing.

It’s quite possible that much of this, sans the ‘date range’ is possible now. I’m only a few days in to working with the Rules Engine and I’m still learning.

Take a look at my Christmas piston to get some idea of what I’m trying to manage. Please ignore the ‘Set Piston State’ statements as those were only for the webCoRe dashboard.

This piston handles colour cycling of some lights in a random manner. I don’t think SharpTools currently has that capability.

Thanks for sharing the details and the example!

:stop_sign: Update: Math / Date Expressions are now available as a native feature. You can use the syntax noted below directly in an expression without having to use the Lab environment.

:link: Feature Announcement: Math / Date Expressions

Not formally, but I believe you could accomplish this with the /math endpoint on our Lab server that I mentioned in another thread by using the pickRandom() function:

The formula would be:

pickRandom(["Green","Red","Gold","Blue","whiteTemperature"])

You can test it by refreshing the following link:
https://lab.sharptools.dev/math?formula=pickRandom(["Green","Red","Gold","Blue","whiteTemperature"])

From what I can see, that seems to be the biggest missing piece. Basically a True/False indicating if you are between a start and and end date.

Sunset/Sunrise offsets are supported in triggers.

I believe you already cast a vote on the following feature request, but posting it here in case other people see it and want to cast a vote. :slight_smile:

:link: Select multiple devices in rule triggers and actions

2 Likes

Thank you Josh. That gives me some things to look in to and work with.

As always, I appreciate your support.

1 Like

We pushed an update the /date endpoint to support checking if the current date is between two dates (within a range).

:arrow_up: Content moved into the main post

1 Like

Oh, very nice. I’ve been using a routine in Tasker called DateSpan (or something like that) that someone posted on Reddit I believe and it works great (and similarly). I used to have it check for school break and then not run morning routines because kids would sleep in, but we just shipped the last one off to college, so it’s not quite as useful any longer. Oh well. At least I know how to use it in case it’s ever pertinent again (and can mimic it in SharpTools). :wink:

1 Like

I’ve successfully implemented this, thank you.

What I’ve actually done is created one rule that contains multiple actions that set variables to true/false, each representing different spans of time (Thanksgiving, Valentines, etc). The trigger for this rule is an HTTP trigger.

In my holiday lighting rules I then trigger the date setting rule as the first action then I can use the variable true/false settings it sets as conditions in the rest of the lighting rule. If I make any changes to the dates in the date setting rule they will always be picked up by triggering the rule each time a check is needed.

1 Like

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?