A fairly common question is "how can I have an alarm which is only triggered at certain times". An example of this use case may be a noise monitor which has a threshold that only applies during night hours. By using a process parameter, some output values can be suppressed based on timestamp, and therefore any state alarm thresholds set on the process can only ever be triggered during certain allowed times.
The following process example will produce output temperature values that are a copy of the input values, but only for input values that that have PM timestamps. That is, timestamps which occur between 12:00pm (inclusive) and 12:00am (exclusive) in my local timezone which is Sydney/Australia (UTC +10:00):
Let's walk through this example. On line 1, a variable named input
is declared, which represents a parameter named Temp
. Every process needs to have at least one input, and this process has only one input.
On line 4, a variable named timestamp
is declared, and it's value comes from the global T function, which is converting the current time of the input parameter into a Moment.js timestamp. On this same line, the timestamp that was just created has a UTC offset of 10 hours applied. This is important, because timestamps in process code are always in the UTC timezone by default. If the timestamp was not offset correctly, then the results would show PM timestamps but in the UTC timezone, which when viewed in my local timezone would actually be from 10:00pm to 10:00am.
On line 5, a variable named value
is declared, which contains the current value of the input parameter (i.e. the current temperature at the time the process is executing).
On line 8, the if statement is used to restrict the timestamp hours to be greater than or equal to 12. Since we already converted from UTC timezone to Sydney/Australia timezone, this means the hour part of the timestamp must be 12 or greater in the Sydney timezone in order for the process to return a value.
Finally on line 10, the input value is returned, but this only happens when the timestamp is within the required window, i.e. 12:00pm or after. All other timestamps (i.e. with an hour of less than 12) result in no value being returned from the process.
For a slightly more complex example, let's look at a PM10 dust monitor that should only trigger alarms between 7:00am (inclusive) and 5:00pm (exclusive) on weekdays, in the Australia/Perth timezone:
This process starts off very similar to the previous example. Note that we need a different UTC offset of 8 hours, as the Australia/Perth timezone is UTC + 08:00.
Since we only want timestamps that occur between 7:00am (inclusive) and 5:00pm (exclusive), the if statement is now specifying that the hours of the timestamp should be greater than or equal to 7, and should be less than 17.
We also only want weekday timestamps, so to exclude the weekend, we use another if statement; this one specifies that the days portion of the timestamp must not equal 0, and must not equal 6. Days of the week are numbered like this:
0 | Sunday |
1 | Monday |
2 | Tuesday |
3 | Wednesday |
4 | Thursday |
5 | Friday |
6 | Saturday |
So by excluding 0 (Sunday) and 6 (Saturday), we ensure that timestamps must be on a weekday.
You can modify these examples for your own needs by using different hours for the time window, or defining a window based on a different element of the timestamp (including day of week, day of month, minute of hour, etc.). Here are some quick examples to illustrate the flexibility of this method:
timestamp.minutes() < 5 |
Timestamps within the first 5 minutes of every hour |
timestamp.dates() === 1 |
Timestamps within the first day of every month |
timestamp.days() === 0 || timestamp.days() === 6 |
Timestamps on the weekend (Saturday or Sunday) |
For more details, check out the Moment.js documentation.
If you want to use the code from either of the examples as a starting point, you can copy and paste it below. Don't forget to change the input to be one of your own parameters, and adjust the UTC offset to be suitable for your local timezone.
var input = NUMBER( "Temp" );
// Sydney timezone is UTC +10:00, so apply offset to current time of input
var timestamp = T( input.currentTime ).utcOffset( 10 );
var value = input.currentValue;
// Only timestamps on or after 12:00
if( timestamp.hours() >= 12 )
{
return value;
}
var input = NUMBER( "PM10" );
// Perth timezone is UTC +08:00, so apply offset to current time of input
var timestamp = T( input.currentTime ).utcOffset( 8 );
var value = input.currentValue;
// Only timestamps between 7:00am (inclusive) and 5:00pm (exclusive)
if( timestamp.hours() >= 7 && timestamp.hours() < 17 )
{
// But NOT on Saturday or Sunday
if( timestamp.days() !== 0 && timestamp.days() !== 6 )
{
return value;
}
}
Comments
0 comments
Article is closed for comments.