Working with Time in Triggers
  • 30 Sep 2022
  • 12 Minutes to read
  • Contributors

Working with Time in Triggers


Article Summary

Overview

Many aspects of operations rely on checking the amount of time that has passed between two events. Here's how to find that time.

Checking the time passed between two timestamps is often a crucial aspect of any process.

Two timestamps could determine:

  • A specified production time
  • The time passed since a batch has run
  • The time passed since a reactor has been cleaned.

Adding logic to the time between two Datetime can be very powerful for both data collection, as well as app Actions.

A few use cases for this functionality:

  • Checking the time between inspections
  • Finding out how long it has been since a given product was run
  • Ensuring a maintenance schedule.
  • Find time between two points in an app

This guide will show you how to build out a few of these scenarios in your app using a combination of triggers, tables and the Expression Editor.

The three examples covered are:

  1. Calculating time between steps in two different parts of an app
  2. Checking whether an inspection of a machine has happened in the past week
  3. Setting a target date for a work order when it is initially scanned in an app

Useful Intervals to Know:

1 minute: 60 seconds
1 hour: 3600 seconds
1 day: 86,400 seconds
1 week: 604,800 seconds
1 month (30 days): 2,592,000 seconds
1 year: 31,557,600 seconds

Example 1: Find time between two parts of an app (production time)

In this example, we will show how to calculate the difference between a two timestamps within a single usage of an app.

Level: Intermediate

For this example, you should have an understanding of:

On the desired "Starting" point for this process, add a "Then" statement in the Trigger Editor to capture the current date and time.

Data Manipulation - Store - App Info - Current Date and Time - location: <Initial Time Variable>.

On the desired "End" point of this time measurement within the app, add another Trigger to capture of the current date and time**.**

In addition to capturing the time, you can also now add the trigger to find the difference between the two timestamps, and store it in a Variable.

In this example, both times are taken when a button is pressed, but it would also be possible to capture times for when Steps close or open, or when devices fire.

The time difference is stored in an "Interval" variable in terms of total number of seconds between the two timestamps.

Example 2 : Checking time between inspections

Level: Advanced

For this example, you should have an understanding of:

In the following example, the app's goal is to check a "Machine Inspection" Table, and determine when the last inspection occurred. Further, if the inspection is greater than the required frequency, the app will direct the user to complete an inspection.

Linking the Table:

The first step in this app will require a Table Record to be loaded into the app. Therefore, the table must already exist. The table might look something like this:

  • ID (text)
  • Last Inspected (timestamp)
  • Inspection Frequency (interval)

Above, the table contains both the last time the machine was inspected, as well as the required frequency of inspection for each machine.

Now, the table must be given a Record Placeholder in the app to check the inspection. Do this by adding a record placeholder of the table on the app. You might name it something like this:

Create the Variable:

Now that you've linked the proper Table to the app, you will need to create a variable to store the difference of the two timestamps. Creating an "Interval" datatype will allow for this manipulation. You can do this by navigating to the Context Pane of the App Editor, and select app.

In the Context Pane, select the sigma symbol to create a new variable.

In the variables pane, create a new Variable by any name you desire, and select the type “interval” from the dropdown menu. Note that this variable will hold the result of the subtraction of the two timestamps that will be manipulated.

Make sure to select “Add”, and make sure the variable adds to the list before closing.

Above, the variable is named "ResultantTime", and will mark the difference between times. In this example, this will note the time between the last inspection, and the current time of the app.

Load the Table Record

In order to check the appropriate table record, the table must be loaded by the correct Table ID. In this example, we will load a record based on the valueof a dropdown widget.

Note that the text will have to exactly match the table ID in order to appropriately load the correct table record.

The form step above allows the user to select which machine will be checked. It is very important to make sure to add a variable to this field, in this case MachineType, to add logic to the result.

Checking the Logic

Now it's time for the check!

This form, when submitted, should check the time between the app's current time, and the previous timestamp from the table.

To do this, you will need to add a few 'custom action' triggers to the submit button.

First, the button needs to load the correct table record:

Table Records - Load Record - by ID: Variable - MachineType - into: Inspection

This Table is loaded dynamically based upon the selected machine in the Form Step, rather than creating several actions to load certain tables.

Next, the submit button will find the time between now, and the timestamp in the table record loaded. In the same trigger, you might add something like this:

This statement will subtract the current time from the table's previous timestamp. It will then save this time difference as an Interval datatype, into the Variable created at the beginning.

In a new Trigger, the difference between the two timestamps will determine an action to the app.

For example, if the time between now and the last inspection is greater than the necessary frequency, the operator must inspect the machine. If not, the machine is OK, and no action is needed. This might look something like this:

IF
Variable - ResultantTime - > - Table Record - Inspection - Inspection Frequency

THEN
Go to Step - Inspection

Here, the check ensures that if the time has been too great, then the inspection must be performed again.

If it is not, the app can complete, since this app is used for checking and performing inspections.

This Else statement, within the same Trigger, might look something like this:

ELSE IF
App - Cancel App
Show Message - Static Value - text - your message

Updating the Table

Once the inspection has been completed, the Table Record should be updated to reflect that the inspection has been performed. To do this, it is best to add a Trigger to the close of the inspection Steps. This way, the record will only update once the inspection is completely done.

Add a Trigger to save the new time, like this:

Data Manipulation - Store - App Info - Current Date and Time - Table Record - Inspection - Last Inspected

Example 3- Setting the target completion date for a work order

Let's say that a work order must pass through 10 separate operations before it is completed.

When the work order starts at the first of the 10 operations, you need to create a "Due Date" to match the customer's expectations. Let's say that the due date is 1 week after the first operation is started.

To accomplish this in Tulip, you will need to add an Interval to the current time in order to determine the time for a week in the future.

Then, you should save that "Due Date" in your "Work Orders" table. Here's how to do that.

First, you need to create a Variable that stores the value for 1 week in the future in an interval.

Go to the Variables dialog in the Context Pane, and create a new variable called "next_7_days" with type interval.

Then, set its default value to 604800 seconds, like this. That is the number of seconds in a week:

Next, you will create a record placeholder for the current work order, and load that work order into the app.

After the record has been loaded, you can set the "Due Date" field using an Expression:

  • "Data Manipulation" "Store" data: "Expression" "App Info-Current Date and Time + Variable.next_7_days" location: "Table Record" "Current Work Order" "Due Date"

In the trigger statement above, we add 7 days to the current date and time to create the due date.


Did you find what you were looking for?

You can also head to community.tulip.co to post your question or see if others have faced a similar question!


Was this article helpful?