Rules / Variables based on Date or Calendar

So it looks like the temperature is actually under the top-level main property. So you would want to access it as main.temp

Also, don’t forget that you can include the &units=metric parameter at the end of your URL so it puts the temperature in the proper format. :smiley:

(And for those that prefer Fahrenheit, you can use &units=imperial)

I remembered, I remembered and added & lang=PL :slight_smile:

All in all, it’s good to describe it and everyone who has a problem with virtual weather stations, which will not work easily on EDGE, can do something like this by themselves, all you need is CRON, Weather API, @josh friend and done :slight_smile:

1 Like

In object property property notation, each ‘child’ property is accessed with a . and arrays are normally accessed with brackets [n] where n is the index (0 based) of the item.

Let’s take a simpler example:

{
   "animal": {
      "type": "cat",
      "colors": ["red", "orange"] 
   }
}

We would access the type as animal.type.

We could get the list (array) of colors as animal.colors but if we only wanted to get the first color, we would normally access it as animal.colors[0] and if we wanted the second color we would use animal.colors[1] (remember that indexes are 0 based with JSON objects)

Hopefully that arbitrary example helps explain and doesn’t just make things more confusing. :laughing:

2 Likes

Also think we need variable and not a trigger
In this piston and other piston, I’m using a variable to be sure I execute the command once a day, So I compare a variable with a number with sysdate

For that particular use case, you can use a True/False variable. Create one rule that resets the variable to False at 00:00 every day. Then in your other rule that you only want to run once per day, you can add an IF Condition to only run if the variable is False and then set the variable to True within THEN part of the condition. :slight_smile:

Will try, thanks a lot again!

1 Like

I wanted to a share a workaround that might be sufficient until we get a chance to build this as a first-class feature in the Rule Engine.

: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

We have a Lab environment which has some endpoints that can be called using the HTTP Action in the Rule Engine and you can get the results using context variables. The /date endpoint can be used with an optional timezone (IANA format) and optional locale parameter to customize the output.

https://lab.sharptools.dev/date?timezone=Poland&locale=pl

As you’ll see from the link above, this provides a wide variety of date related details available as properties that you can then use in your rules.

Formatted Output

Furthermore, you can pass a format property using any combination of those values to receive a fully formatted string in return. Examples:

Note that if you are using more complex formats with spaces and such, you will either want to URL encode them or you can even use a POST request to send the parameters across.

Localized Formats

Also supported for the format field are some default localized formats:

  • L - localized date (short)
  • LL - localized date (long)
  • LT - localized time
  • LTS - localized time with seconds

Formatted Input

You can also provide an input to the /date endpoint as well.

  • input - expects an ISO 8601 timestamp by default, but can be modified with inputFormat
  • inputFormat - the format of the input value (eg. a format string like YYYY-MM-DD or MMMM DD, YYYY)
  • inputTimezone - the IANA timezone (if your input value is not UTC)
  • inputLocale - can only be used if you’re using the default UTC timezone (eg. no inputTimezone)

Date Between

The /date/between endpoint accepts a start, end, and timezone (IANA) property for checking if the current date is between two dates.

For example, you could check if the date is between August 1st and September 1st
https://lab.sharptools.dev/date/between?start=2022-08-01&end=2022-09-01&timezone=America/Chicago

The formats are YYYY-MM-DD or MM-DD. By default, the endpoint will ignore the year – this enables the same condition to continue to work year after year without needing to update the rule. You can include the flag &includeYear=true if you want to restrict to a specific year.

The main property you would look for in the response is result which is a true/false indicating if it’s currently between the specified date range.

You could use this with a rule that has a Time trigger to run every day and checks this endpoint to determine if it matches your range to decide if you want to run the rest of the rule or not.

1 Like

wow nice, will take a look!

We added the ability to provide an input to the /date endpoint as well.

:arrow_up: Content moved into the main post

Also supported for the format field are some default localized formats:

:arrow_up: Content moved into the main post

2 Likes

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.