Tutorial: Calculating the time spent in a specific state

In this tutorial, you define which state to aggregate for devices. Use the PrepareTimeInState built-in function to specify the state. Using the AggregateTimeInState, you calculate the amount of time that the device spends in each state. Build a dashboard to monitor the time spent in each state.

About this task

In this tutorial, you calculate the amount of time per day that robots traveled too fast. Select a robot and review the time that the robot spent traveling too fast over the past week.

Steps

Complete these tutorial steps:

Step 1: Create a robot device type

  1. On the Setup tab, click the Create new button to add a device type.
  2. In the Add device type dialog, on the Select a template page, select the Robot Sample Type template.
  3. Assign a name to the device type.
  4. Click Create.
  5. Select the sample device type, click Manage device type, and then click Data.

KPI data takes up to 5 minutes to generate.

Step 2: Define the state

  1. On the Data tab, click the Create new data item button.
  2. Select the PrepareTimeInState function from the catalog.
  3. On the scope page, select All devices of this type.
  4. Click Next.
  5. On the input page, select speed as the source data item.
  6. Define the state condition as >= 3.5.
  7. On the output page, specify too_fast as the new KPI name.
  8. Clear the 'Store in database' option.
  9. Click Create
  10. In the data item list, select too_fast.
  11. Wait up to 5 minutes for Analytics Service to evaluate the expression using the sample data. Verify that the function produces some output.

Step 3: Calculate the amount of time that was spent in the state

  1. On the Data tab, click the Create new data item button..
  2. Select the AggregateTimeInState function from the catalog.
  3. On the scope page, select All devices of this type.
  4. Click Next.
  5. On the input page, select too_fast as the source data item.
  6. On the output page, specify time_travelling_too_fast as the new KPI name.
  7. Set the time grain to hourly.
  8. Include the last 5 hours of historical data.
  9. Click Create
  10. In the data item list, select time_travelling_too_fast.
  11. Wait up to 5 minutes for Analytics Service to evaluate the expression against the sample data. Verify that the function produces some output.

Step 4: Add a dashboard to the robot devices

  1. Go to a robot device on the user interface.
  2. Click Dashboards.
  3. Click Add dashboard.
  4. Assign the title 'tutorial' to the dashboard and click Configure. You'll overwrite the dashboard title in the JSON configuration.
  5. Click Import and import the following dashboard configuration from a JSON file:
    • Expand to see JSON
      
               {
               "timeGrain": "day",
               "cards": [
                   {
                       "id": "344ed855-d211-4fcb-bec0-794f973de8e6",
                       "title": "Time spent travelling too fast",
                       "size": "MEDIUMWIDE",
                       "type": "BAR",
                       "content": {
                           "type": "SIMPLE",
                           "layout": "VERTICAL",
                           "series": [
                               {
                                   "dataSourceId": "time_travelling_too_fast_36588a25-19fd-48a2-a973-9eb6ae3c9a87",
                                   "label": "time_travelling_too_fast",
                                   "color": "#6929c4"
                               }
                           ],
                           "timeDataSourceId": "timestamp"
                       },
                       "dataSource": {
                           "attributes": [
                               {
                                   "id": "time_travelling_too_fast_36588a25-19fd-48a2-a973-9eb6ae3c9a87",
                                   "attribute": "time_travelling_too_fast",
                                   "aggregator": "mean"
                               }
                           ],
                           "timeGrain": "day",
                           "range": {
                               "interval": "day",
                               "count": -7,
                               "type": "rolling"
                           }
                       },
                       "timeRange": "last7Days"
                   }
               ],
               "title": "Robot speed",
               "layouts": {
                   "lg": [
                       {
                           "w": 16,
                           "h": 2,
                           "x": 0,
                           "y": 0,
                           "i": "344ed855-d211-4fcb-bec0-794f973de8e6",
                           "moved": false,
                           "static": false,
                           "isResizable": true
                       }
                   ],
                   "md": [
                       {
                           "i": "344ed855-d211-4fcb-bec0-794f973de8e6",
                           "x": 0,
                           "y": 0,
                           "w": 8,
                           "h": 2,
                           "isResizable": true
                       }
                   ],
                   "sm": [
                       {
                           "i": "344ed855-d211-4fcb-bec0-794f973de8e6",
                           "x": 0,
                           "y": 0,
                           "w": 4,
                           "h": 2,
                           "isResizable": true
                       }
                   ]
               }
           }
                       
  6. Click Save.

Step 5: Monitor the time spent in the state

  1. On the Monitor tab, click Devices.
  2. Find your device type.
  3. Open the Robot speed tab.
  4. Review the time spent traveling too fast for one of the devices over the last 7 days.
    Example: Robot traveling too fast bar chart