Rules / Variables based on Date or Calendar

If you’re just looking to trigger a rule based on different days of the week, you can do this with the Time Trigger. See the screenshots two posts up – you can tap the ‘Everyday’ in my screenshot which brings up the Repeat Options. From here, you can select ‘weekly’ and choose which days of the week you want to trigger things on.

Do you have any weather devices attached to your SmartThings account? If so, do they not include the data you’re looking for?

@josh - variables are so much better that you can use them in notifications, eg “Hi, It’s Monday, September 20, moderate cloudiness”

Do you have any weather devices attached to your SmartThings account? If so, do they not include the data you’re looking for?

I have an outside temperature sensor, but it doesn’t show the weather. The easiest way would be to extract data from the integration with https://openweathermap.org/ and on their basis set constant variables, such as the current weather forecast, humidity, temperature, etc.

Thanks. I suspected there was more than just the ‘variable’ part of things. It sounds like some controls around formatting the dates would be important in that case too. :slight_smile:

Can you share some examples of how you format these date/timestamp variables today? It seems like it could be something that would feel overwhelming to a new user… but perhaps it’s just more of an ‘advanced’ feature and naturally requires a bit more configuration if you want to customize the format.

Currently in Homey Bridge I have one date format and I accept it as it is DD.MM.YYYY you can define 2 or 3 formats permanently in the settings or for very demanding users make 3 variables
current day (e.g. 20), current month digitally or in words (08) and current year 2022 and you can write in the notification

E.g. “Hey, it’s {day}. {Month}. {Year}, current weather: it’s raining”

Similarly, with the time we are from different parts of the world, some use 24 hours, others AM / PM

You can do this with the HTTP Action in SharpTools today. I agree that it makes for a good feature request as a native integration though to simplify this process for people who don’t have weather devices attached to their SmartThings. :smiley:

Let’s take the Open Weather /weather endpoint which provides the current weather. The URL is in the format:

https://api.openweathermap.org/data/2.5/weather?lat=-33&lon=96&appid={{ApiKey}}

And the response is in the format:

{
   "coord":{
      "lon":96,
      "lat":-33
   },
   "weather":[
      {
         "id":500,
         "main":"Rain",
         "description":"light rain",
         "icon":"10n"
      }
   ],
   "base":"stations",
   "main":{
      "temp":286.4,
      "feels_like":286.08,
      "temp_min":286.4,
      "temp_max":286.4,
      "pressure":1032,
      "humidity":88,
      "sea_level":1032,
      "grnd_level":1032
   },
   "visibility":10000,
   "wind":{
      "speed":6.89,
      "deg":143,
      "gust":7.83
   },
   "rain":{
      "1h":0.19
   },
   "clouds":{
      "all":97
   },
   "dt":1661023588,
   "sys":{
      "sunrise":1661040462,
      "sunset":1661080271
   },
   "timezone":21600,
   "id":0,
   "name":"",
   "cod":200
}

So if we wanted to get the main weather report, we would be looking for the weather.0.main element in the response. So we can make the HTTP call and then get the response data using Context Variables (Response > HTTP > Data) and use the object property notation to grab that nested property for use in our notification:

1 Like

Thanks for the feedback. In our Super Tiles, since they are running in the browser, we can infer your ‘locale’ and use that to display things in the timezone and format your computer / tablet prefers.

For Rules, we typically associate things with ‘locations’ since some people have multiple locations attached to their account – for example, their main home and a vacation home. We use that for determining the appropriate localized time for triggering rules. I suspect we could either add user-level or location-level formatting options. Originally I was thinking you might want a rule action like Format Date where you could customize the date for that particular notification just to pull out the parts you are interested in (similar to our Super Tile date/time format options).

I love your advice right away in a few hours :slight_smile: Divine!

Yes, but how do I pair it with variables? I would like to use them in notifications?

My apologies for not being clear – I was just trying to better understand the use-cases. We don’t have it available as Rule Variables today, but if we eventually build it I want to make sure I understand how people use it so we can make the right design. :smiley:

Thanks! :slight_smile:

Sorry, my English is terrible.

Generally, it is about notifications in which you can use the date, sometimes in simple such as hello is a day, and sometimes in emergency such as “At 12:00 on August 20, 2022, a movement in the garage has been detected”

The weather works perfectly, I take the data I need from the API, you are great!

1 Like

I was able to download weather.0.description from the data, but I would like to download the temperature data in the same query and how it adds main.0. temp is unfortunately nothing changes in variables, can you tell me what am i doing wrong?

Ok, I did it, I don’t know why you put “0” there, what does it mean, what does it mean?


Sorry, I call myself variables in Polish, I think you might guess what’s going on: D

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!