Populating and sending commands

Completed

Lab overview

Dynamics 365's Connected Field Service solution provides the ability to remotely monitor IoT enabled devices to keep track of up time, device statistics, anomalies, and more. After an IoT device communicates an anomaly or exception, that information is sent to Connected Field Service as an alert. Once the alert is captured, IoT devices can be remotely managed and interacted with using IoT device commands from within Dynamics 365. To assist some flexibility, the data populated in these commands can be easily populated using command and property definitions.

The purpose of this hands-on-lab is to introduce you to creating and defining command and property definitions in Connected Field Service. Additionally, we will demonstrate how you can automate the process of sending commands using Dynamics 365 functionality.

Scenario

You work for a plumbing and heating company that sells and services smart home devices. Since many of the devices that you service are smart devices, information from those devices can be captured remotely to aid in the servicing of that equipment. Your organization has several defined scenarios that include the processes that will be used while servicing equipment. These processes can vary depending on several factors including:

  • Type of device in need of service

  • Severity of the issue detected

  • Age of the device

  • Location of the device

  • Availability of resources to work on a specific device

The above items represent only a small portion of factors that your organization uses to determine the process for servicing equipment.

Lately, you have been installing many smart thermostat devices. These devices have been handy in being able to track temperature and humidity readings in areas when it is important to maintain a consistent temperature and humidity. Because you have much more information available than previously, it has forced you to create new processes for handling and resolving issues reported by these devices. The first area you would like to focus on is for automating the process used for temperature-related issues.

Your organization is looking to implement the following process:

  • If a device detects a temperature reading of more than 70 degrees, an alert should be surfaced in your Connect Field Service environment.

  • If the temperature reading is between 70 degrees and 85 degrees, a command should be sent to remotely reset the device to see if the issue resolves itself.

Your organization wants to make it as easy as possible for technicians to create commands and modify parameters. They would like to pre-define a few of the commands and properties that are common to these smart thermostat devices.

The two commands they want to pre-define are:

  • Notification:
{"CommandName":"Notification","Parameters":{"Message":"Technician has been dispatched"}}
  • Set Values:
{"CommandName":"Set Values","Parameters":{"Reading":{"Temperature":"30","Humidity":"30"}}}

Exercise 1: Define command and property definitions

Task 1: Create device categories

  1. Navigate to Power Apps and make sure you are in the correct environment that has Connected Field Service configured.

  2. Select Apps and click to open the Connected Field Service application.

  3. Switch to the Settings area of the sitemap.

  4. Select Device Categories and click + New.

  5. Enter Temperature for Name and click Save.

  6. Click + New again.

  7. Enter Humidity for Name and click Save.

Task 2: Create property definitions

  1. Select Property Definitions from the sitemap and click + New.

  2. Configure your property definition as follows:

    • Name: Message

    • Type: String

    • Editable: Yes

    • Visible: Yes

    • Additional Properties:

      • Default: A technician has been dispatched.
  3. Save and Close the Message property definition.

  4. Click the + New button.

  5. Configure your property definition as follows:

    • Name: Reading

    • Type: Object

  6. Save and Close the reading property definition.

  7. Click the + New button.

  8. Configure your property definition as follows:

    • Name: Temperature

    • Type: Whole Number

    • Parent Property: Reading

    • Editable: Yes

    • Visible: Yes

    • Additional Properties:

      • MinValue: 1

      • MaxValue: 120

      • Default: 65

  9. Save and Close the Temperature property definition.

  10. Click + New again.

  11. Configure your property definition as follows:

    • Name: Humidity

    • Type: Whole Number

    • Parent Property: Reading

    • Editable: Yes

    • Visible: Yes

    • Additional Properties:

      • MinValue: 1

      • MaxValue: 100

      • Default: 40

  12. Save and Close the Humidity property definition.

  13. Your completed property definitions should resemble the image below:

    Screenshot of completed property definitions.

Task 3: Create command definitions

  1. Select Command Definitions and click + New.

  2. Configure your command definition as follows:

    1. Name: Notification
  3. Save the Notification command definition and leave it open. This will also enable the Property Definition sub-grid.

  4. Click Add Existing IoT Device Category.

  5. In the lookup records, select both the temperature and humidity categories.

  6. Click Add.

  7. Click Add Existing IoT Property Definition.

  8. Select Message, click Add.

  9. The completed Notification command definition should resemble the image below:

    Screenshot of completed Notification command definition.

  10. Click the + New button.

  11. Configure your command definition as follows:

    • Name: Set Values
  12. Save the Set Values command definition and leave it open.

  13. Click Add Existing IoT Device Category.

  14. In the lookup records, select both the Temperature and Humidity categories.

  15. Click Add.

  16. Click Add Existing IoT Property Definition.

  17. Select Reading, click Add.

  18. The completed Set Values command definition should resemble the image below:

    Screenshot of completed Set Values command definition.

Exercise 2: Using definitions to populate commands

Task 1: Attach to simulator (Optional)

Note

If you already have the simulator open from the Unit 3 exercise, you can skip straight to Task 2. Otherwise follow the steps below to attach to the simulator.

  1. In your web browser, select a new tab and navigate to Azure portal. Sign in to Azure if prompted. You need an Azure subscription associated with the account you are using.

  2. Select Resource Groups.

  3. Open the resource group you are using with Connected Field Service, and select the Simulator.

  4. Click the Simulator URL to open the thermostat simulator. The simulator should open in a new tab.

  5. On the simulator click Connection.

  6. Switch back to your Azure subscription, close the simulator app service screen.

  7. Open the IoT Hub.

  8. Highlight the IoT Hub name and Copy it.

  9. Switch back to your simulator screen and paste the IoT hub name into the Host field.

  10. Switch back to your Azure subscription, select Shared Access Policies.

  11. Open the iothubowner policy and copy the Primary key.

  12. Switch back to your simulator screen and paste the Primary Key into the Key field.

  13. Click the Connect button. Your Simulator is connected and ready to use.

Task 2: Use simulator to generate and interact with alerts

  1. With the simulator open, click the Refresh button to ensure all registered devices are available.

  2. From the Select a device drop-down, select the smt-9876 device. A green dot should appear next to the refresh button, and after a few seconds it should start transmitting information.

  3. On the Temperature Slider, set it to 75 Degrees and wait for the temperature status to register on in the Messages Sent window.

  4. Switch to the browser tab that has Connected Field Service loaded.

  5. Switch to the Connected Field Service sitemap area.

  6. Select IoT Alerts.

  7. Open the Temperature of 75 exceeded a threshold of 70 Alert.

  8. On the Command Bar, click Send Command.

  9. Click Advanced.

  10. Enter Set Device Values for Name.

  11. Under Message to Send, click in the lookup field next to Command, select the Set Values command.

  12. The message should display with the Temperature and Humidity properties pre-defined.

    1. Change Temperature to 60

    2. Change Humidity to 35

  13. On the Command bar, click the Send & Close button.

  14. Switch back to your simulator, after a short while the device should have its values set to the values defined in the message. Depending on internet speeds, and other factors it can take several minutes for this to occur.

  15. Set the temperature on the device to 86 degrees and wait for it to register in the Messages Sent.

  16. Switch to the browser tab that has Connected Field Service loaded.

  17. Select IoT Alerts.

  18. Open the Temperature of 86 exceeded a threshold of 70 alert.

  19. On the Command Bar, click Send Command.

  20. Click Advanced.

  21. Enter Dispatch Tech for Name.

  22. Under Message to Send, click in the lookup field next to Command, select the Set Values command.

  23. The message should display with the Message property pre-defined.

  24. On the Command bar, click the Send & Close button.

  25. Switch back to your simulator, after a short while a message should appear in the Messages Received that a Technician has been dispatched. Depending on internet speeds, and other factors it can take several minutes for this to occur.

  26. If you check the status of "IOT Alert" in the Connected Field Service environment, it should now change to "In Progress -- Command Sent".

Exercise 3: Automate sending of commands

Task 1: Create a new IoT alert process solution

  1. Navigate to Power Apps and make sure you are in the correct environment.

  2. Select Solutions and click New Solution.

  3. Name the solution IoT Alert Processes.

  4. Click on the Publisher drop down and select + Publisher.

  5. Configure the Publisher as follows.

    1. Display Name: Connected Field Service Course

    2. Prefix: cfs

  6. Click Save and Close.

  7. Click on the Publisher dropdown and select the publisher you created.

  8. In the Version field, enter 1.0.0.0. and click Create.

  9. Click to open the IoT Alert Process solution you created.

  10. Click Add Exiting and select Entity.

  11. Select IoT Alert and click Next.

  12. Do not include required components. Click Add again.

Task 2: Customize the IoT alert entity

  1. Click to open the IoT Alert Process solution.

  2. Click to open the IoT Alert entity.

  3. Select the Fields tab and click + Add Field.

  4. Provide the information mentioned below:

    1. Display Name: Reading

    2. Data Type: Whole Number

    3. Minimum Value: 0

    4. Maximum Value: 250

  5. Click Done.

  6. Click + Add Field again.

  7. Configure the new field as follows:

    1. Display Name: Customer

    2. Data Type: Lookup

    3. Target Record Type: Account

  8. Click Done.

  9. Click Save Entity.

  10. Select Solutions and click Publish All Customizations.

Task 3: Copy set values command message

  1. Navigate to Power Apps and make sure you are in the correct environment.

  2. Select Apps and open the Connected Field Service application.

  3. Select Commands and click + New.

  4. Scroll down to the Message to Send section and select Set Values for Command.

  5. Click Switch to JSON View.

  6. Copy the JSON and save it to a Notepad.

  7. Close the Connected Field Service application without saving the Command record.

Task 4: Create a flow to populate values

  1. Navigate to Power Apps and make sure you are in the correct environment.

  2. Select Solutions and click to open the IoT Alert Process solution.

  3. Click + New and select Flow. This will open a New window.

  4. Rename the Flow Populate Alert Fields and Set Values.

  5. Search for Microsoft Dataverse and select Common Data Service (Current Environment). Make sure that you have selected the one with "Current Environment".

  6. Select When a Record is Created, Updated, or Deleted.

  7. Select Create for Trigger Condition, IoT Alerts for Entity, and select Organization for Scope.

  8. Click on the Menu button and select Rename.

  9. Rename the When Alert is Created and click + New Step.

  10. Search for Parse and select Parse Json.

  11. Click on the Content field and select Alert Data from the Dynamic Content pane.

  12. Paste the JSON schema below in the Schema field.

     {
         "type": "object",
         "properties": {
             "deviceid": {
                 "type": "string"
             },
             "readingtype": {
                 "type": "string"
             },
             "reading": {
                 "type": "integer"
             },
             "eventtoken": {
                 "type": "string"
             },
             "threshold": {
                 "type": "integer"
             },
             "ruleoutput": {
                 "type": "string"
             },
             "time": {
                 "type": "string"
             }
         }
     }
    
  13. Rename the step ParseAlertDate and click + New Step.

  14. Search for Get a Record and select Get a Record (Current Environment). Make sure that you have selected the one with "Current Environment".

  15. Rename the step Get Customer Asset.

  16. Select Customer Assets for Entity Name.

  17. Click on the Item Identifier field and select Customer Asset (Value) from the Dynamic Content pane.

  18. Click + New Step.

  19. Search for Update a Record and select Update a Record (Current Environment). Make sure that you have selected the one with "Current Environment".

  20. Rename the step Update Alert.

  21. Select IoT Alerts for Entity Name, click on the Records Identifier field and select IoT Alert from the Dynamic Content pane.

  22. Click Show Advanced Options.

  23. Click on the Customer (Account) field and type accounts().

  24. Place your cursor between ( and ) and select Account (Value) from the Dynamic content.

  25. Click on the Reading field and select reading of the Parse Json step from the Dynamic content.

  26. Hide Advanced Options.

  27. Click + New Step.

  28. Select Condition.

  29. Click on the Choose Value field and select Reading from the Update Alert step.

  30. Select Is Greater than or Equal to.

  31. Enter 71 for Value, click + Add, and select Add Row.

  32. Click on the Choose Value field and select Reading from the Update Alert step in the Dynamic Content.

  33. Select Is Less than or Equal to.

  34. Enter 85 for Value.

  35. Your condition should now look like the image below.

    Screenshot of condition with Reading greater than 71 and less than 85.

  36. Go to the Yes branch and click Add an Action.

  37. Search for Create a New Record and select Create a New Record (Current Environment). Make sure that you have selected the one with "Current Environment".

  38. Rename the step Send Set Values Command.

  39. Select IoT Device Commands for Entity Name, click on the Name field and select Description from the When Alert is created action in the Dynamic Content.

  40. Add Set Device Values to the Name and click Show Advanced Options.

  41. Click on the Customer Asset field and type msdyn_customerassets().

  42. Place your cursor between ( and ) and select Customer Assets (Value) from the When Alert is created action in the Dynamic Content.

  43. Click on the Device field and type msdyn_iotdevices().

  44. Place your cursor between ( and ) and select Device (Value) from the When Alert is created action in the Dynamic Content.

  45. Click on the Parent Alert field and type msdyn_iotalerts().

  46. Place your cursor between ( and ) and select IoT Alert from the Update Alert action in the Dynamic content.

  47. Select the Device ID field and select Device ID from the When Alert is Created action in the Dynamic Content pane.

  48. Paste the JSON you copied from the Set Values command in the Message field.

     {"CommandName":"Set Values","Parameters":{"Reading":{"Temperature":65,"Humidity":40}}}
    
  49. Click Hide Advanced Options.

  50. Click Add an Action.

  51. Search for Update a Record and select Update a Record (Current Environment) Make sure that you have selected the one with "Current Environment".

  52. Rename the step Deactivate Alert.

  53. Select IoT Alerts for Entity Name.

  54. Click on the Record Identifier field and select IoT Alert from the Update Alert action in the Dynamic Content pane.

  55. Click Show Advanced Options.

  56. Set the Status to Inactive and Hide Advanced Options.

  57. Your flow should now look like the image below.

    Screenshot of flow that was created in previous steps.

  58. Click Save.

Task 5: Test the process

  1. Switch back to the web browser tab that contains your simulator. If this is not connected, please follow the same process to open and simulator and create a connection to the IoT Hub.

  2. Set the temperature to 80 Degrees and wait for it to register in the Messages Sent window.

    Because the temperature was between 71 degrees and 85 degrees, a command will be sent to the device to set the values on the device. It can take several minutes for the reboot command to be sent to the device.

  3. Switch to the browser tab that has Connected Field Service loaded.

  4. Select IoT Alerts.

  5. Switch the view to Inactive IoT Alerts.

  6. Open the Temperature of 80 exceeded a threshold of 70 Alert.

  7. Select the Commands tab. You should see a Command that was sent to the device.