Skip to content

Latest commit

 

History

History
167 lines (115 loc) · 5.14 KB

schedules.md

File metadata and controls

167 lines (115 loc) · 5.14 KB

Schedules API

The schedules API provides a means of interacting with the Schedules in the Hue Bridge.

The Schedules API interacts with Schedule objects along with their associated Time Patterns.

getAll()

The getAll() function allows you to get all the Schedules that the Hue Bridge has registered with it.

api.schedules.getAll()
  .then(allSchedules => {
    // Display the Schedules from the bridge
    allSchedules.forEach(schedule => {
      console.log(schedule.toStringDetailed());
    });
  });

This function call will resolve to an Array of Schedule objects.

A complete code sample for this function is available here.

getSchedule()

The getSchedule(id) function allows a specific Schedule to be retrieved from the Hue Bridge.

  • id: The id of the Schedule or a Schedule instance that was previously obtained from the bridge.
api.schedules.getSchedule(1)
  .then(schedule => {
    console.log(schedule.toStringDetailed());
  })
;

This function call will resolve to a Schedule object for the specified schedule id.

If the Scene cannot be found an ApiError will be returned with a getHueErrorType() value of 3.

A complete code sample for this function is available here.

getScheduleByName()

The getScheduleByName(name) function will find all the Schedules that are stored in the bridge with the specified name.

  • name: The String that represents the name of the Scheduless that you wish to find.
api.schedules.getScheduleByName('Wake Up')
  .then(results => {
    // Do something with the schedules we matched
    results.forEach(scene => {
      console.log(schedule.toStringDetailed());
    });
  })
;

The function will resolve to an Array of Schedule Objects that were matched to the specified name. It none are matched the Array will be empty.

A complete code sample for this function is available here.

createSchedule()

The createSchedule(schedule) function allows for the creation of new Schedules in the Hue Bridge.

  • schedule: A Schedule object that has been configured with the desired settings for the Schedule being created.
const model = require('node-hue-api').v3.model;

const schedule = model.createSchedule();
schedule.name = 'My Schedule';
schedule.description = 'A test schedule from the node-hue-api examples';
// trigger the schedule in 1 hour from now
schedule.localtime = model.timePatterns.createTimer().hours(1);
// Turn all the lights off (using light group 0 for all lights)
schedule.command = model.actions.group(0).withState(new model.lightStates.GroupLightState().off());

api.schedules.createSchedule(schedule)
  .then(createdSchedule => {
    console.log(`Successfully created Schedule\n${createdSchedule.toStringDetailed()}`);
  })
;

The function will return a Promise that will resolve with a corresponding Schedule with a populated id attribute.

A complete code sample for this function is available here.

updateSchedule()

The updateSchedule(schedule) function will update the schedule in the bridge to match the attributes of the specified schedule.

  • schedule: The schedule with updated attributes to set on the bridge.
// Obtain a schedule from the Bridge, e.g. get schedule with id = 1
const mySchedule = await hue.schedules.get(1);

// Update some attributes
mySchedule.name = 'Updated Name';

// Update the schedule in the bridge
hue.schedules.updateSchedule(mySchedule)
  .then(updateResult => {
    console.log(`Updated Name? ${updateResult.name}`); // Will print "Updated Name? true" 
  });

Note: Currently there is no checking as to whether or not a value has been modified, so all the updatable attributes are passed to the bridge. (this is quicker and more efficient than doing a get/put chain of requests).

The function call will return a Promise that will resolve to an Object with the update information.

The result Object that will have the form of the keys that were updated along with a boolean flag indicating if the value was modified.

{
  "name": true,
  "description": true,
  "command": true,
  "localtime": true,
  "status": true,
  "autodelete": true
}

A complete code sample for this function is available here.

deleteSchedule()

The deleteSchedule(id) function will delete the specified scene identified by the id from the Hue Bridge.

  • id: The id of the scene to delete from the Hue Bridge.
api.scenes.delete('abc170f')
  .then(result => {
    console.log(`Deleted scene? ${result}`);
  })
;

The call will resolve to a Boolean indicating the success status of the deletion.

A complete code sample for this function is available here.