Skip to end of metadata
Go to start of metadata

Scenario

  1. We have a "Thermometer" thing with a float property "Temperature"
  2. The user wishes to receive updates of the current temperature by email according to a schedule

Implementation

  1. A "sendUpdate" method will be created here that sends an email message with the current temperature
  2. A schedule called "temperatureUpdateEmail" will be created to call the "sendUpdate" method once every hour

Prerequisites:

  1. A "Thermometer" thing with a float property "Temperature" was created in the Create, Read And Write Property scenario
  2. Schedules are used to push events or call methods at set times according to a cron-based schedule
  3. The cron expression used as the cron value (once per hour) can be found be clicking "cron expression builder" just under the cron input.
  4. If a schedule is used to call a method, JSON arguments must be entered only when script logic of the method requires an argument for a function
  5. If a schedule is used to push an event, JSON arguments must be entered showing value(s) to be passed to the event, since events must always refer to value(s) with the given data type
  6. The schedule created here is used to call a method that sends the current temperature value in an email once per hour
  7. The email address is entered in as the JSON argument when creating the schedule, and the timing for the schedule is entered in as a cron-based code expression

Steps

2. Create "sendUpdate" method


  1. In the "Thing: Thermometer" tab, click "Methods"
  2. Click "New" to open "Create Method" dialog 
  3. Set the name of the method
  4. Select "script" as the type of method
  5. Set "Request Data Type" to "string"
  6. Click to save new method

The string used for "Request Data Type" is the email used for sending updates, and will be entered when creating the schedule

The email string is then given as the argument for the sendUpdate function in the script logic:

3. Code script logic for "sendUpdate" method


  1. In the "Methods" section of "Thing: Thermometer", select "sendUpdate" method 
  2. Click to open "Edit Script" dialog
  3. Insert the following JavaScript code for the "sendUpdate" method:

  4. Save script by clicking "Save"

The only thing missing now is the schedule for calling this method, and the email address used for sending updates

4. Create "temperatureUpdateEmail" schedule


  1. In the "Thing: Thermometer" tab, click "Schedules"
  2. Click "New" to open "Create Schedule" dialog
  3. Set name of the schedule
  4. Set "cron" to "0 0 0/1 1/1 * ? *" for 1 hour email send intervals (or if you want it faster, set it to "0 0/1 * 1/1 * ? *" for 1 minute intervals)
  5. Select "method" in the drop-down menu as "Type" used for the schedule (the alternative is "event")
  6. Select the "sendUpdate" method in the drop-down menu
  7. Specify the email address where updates can be sent in the "Arguments, JSON" text area
  8. click "Save" button

After waiting one hour(or one minute), an update message with a temperature reading should appear in your Inbox

 

 

  • No labels