Scenario:
- We have a "Thermometer" thing with a float property "Temperature"
- The user wishes to receive updates of the current temperature by email according to a schedule
Implementation:
- A "sendUpdate" method will be created here that sends an email message with the current temperature
- A schedule called "temperatureUpdateEmail" will be created to call the "sendUpdate" method once every hour
Prerequisites:
- A "Thermometer" thing with a float property "Temperature" was created in the Create, Read And Write Property scenario
- Schedules are used to push events or call methods at set times according to a cron-based schedule
- The cron expression used as the cron value (once per hour) can be found be clicking "cron expression builder" just under the cron input.
- 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
- 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
- The schedule created here is used to call a method that sends the current temperature value in an email once per hour
- 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
- In the "Thing: Thermometer" tab, click "Methods"
- Click "New" to open "Create Method" dialog
- Set the name of the method
- Select "script" as the type of method
- Set "Request Data Type" to "string"
- 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
- In the "Methods" section of "Thing: Thermometer", select "sendUpdate" method
- Click to open "Edit Script" dialog
-
Insert the following JavaScript code for the "sendUpdate" method:
-
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
- In the "Thing: Thermometer" tab, click "Schedules"
- Click "New" to open "Create Schedule" dialog
- Set name of the schedule
- 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)
- Select "method" in the drop-down menu as "Type" used for the schedule (the alternative is "event")
- Select the "sendUpdate" method in the drop-down menu
- Specify the email address where updates can be sent in the "Arguments, JSON" text area
- click "Save" button
After waiting one hour(or one minute), an update message with a temperature reading should appear in your Inbox