node-hue-api

repository·typescript·Indexed 22 days ago

https://github.com/peter-murray/node-hue-api

A Node.js library that abstracts the Philips Hue Bridge REST API, allowing developers to control lights, schedules, sensors, and other bridge features via JavaScript. Version 5.0.0-beta.14 provides complete coverage for the Philips Hue REST API with native Promise and async/await support. It includes built-in rate limiting, support for both TLS and HTTP connections, and tools for bridge discovery via N-UPnP and UPnP.

Tokens
42.8K
Snippets
156
Records
214
Agent score
76%

What's inside node-hue-api

  1. What is a RuleCondition and how to build them

    typescript

    A RuleCondition is an object that defines the criteria used to trigger a Rule on the Hue Bridge. The API provides a fluent interface via v3.model.ruleConditions to build these conditions. Currently, you can build conditions for two types of entities:

    1. Sensors: Using v3.model.ruleConditions.sensor(sensor)
    2. Groups: Using v3.model.ruleConditions.group(id)

    To finalize the builder and obtain a valid RuleCondition object, you must call .getRuleCondition() at the end of your fluent chain. If the configuration is invalid, this method will throw an ApiError.

    const v3 = require('node-hue-api').v3;
    const conditions = v3.model.ruleConditions;
    
    // Example pattern for building a condition
    const ruleCondition = conditions.sensor(mySensor)
      .when('attribute')
      .equals(value)
      .getRuleCondition();
  2. Overview of Hue Bridge Time Patterns

    typescript
    The Hue Bridge supports at least 10 different time patterns for scheduling automation. To build these patterns programmatically, use the v3.model.timePatterns module provided by node-hue-api. This module offers a user-friendly way to construct various scheduling logic, including absolute, randomized, recurring, and timer-based patterns.
  3. What is a RuleAction and how to use it

    typescript

    A RuleAction is an object that defines an action to be performed when a Hue Bridge Rule is triggered. The API provides a fluent interface via v3.rules.actions to build these actions.

    All RuleAction types inherit from a base class that provides the following properties and methods:

    • address: Gets the target address for the action.
    • id: Gets the target ID (e.g., Light ID, Group ID, or Sensor ID).
    • method: Gets the HTTP method (usually PUT).
    • withMethod(method): Sets the HTTP method.
    • payload(): Returns the JSON payload that will be sent to the Hue Bridge.
    • body: Gets the body payload.
    • toString(): Returns a string representation.
  4. What is a ResourceLink?

    typescript
    A ResourceLink is a grouping construct used to link various Hue Bridge resources together to provide interconnected functionality. While primarily used for Hue Formulas, developers can use it as an advanced mechanism to build complex logic by grouping resources like lights, groups, and scenes.
  5. Understand Hue Bridge Scene limitations

    typescript

    The Scenes API interacts with Scene objects stored on the Hue Bridge. These are distinct from the preset scenes found in mobile Hue applications.

    Be aware of the following hardware limitations on the Hue Bridge:

    • Maximum Scenes: Up to 200 scenes.
    • Maximum Scene LightStates: 2048 total.

    Note that the actual number of scenes you can store depends on how many light states each scene contains. For example, if every scene contains 20 light states, the bridge can only support 102 scenes ($2048 / 20 = 102.4$).

  6. Understand the two types of Hue Scenes

    typescript

    The Hue Bridge supports two distinct types of Scenes. The type attribute is implicitly set by the API based on the properties you provide, so you should generally avoid setting it manually.

    1. LightScene: The default scene type. It maintains a specific list of lights that can be updated independently of any group membership.
    2. GroupScene: A scene linked to a specific group. The lights involved in a GroupScene are determined by the membership of that group. If the group is removed or becomes empty, the associated GroupScene is also removed. You cannot modify the lights of a GroupScene directly; they are controlled via the group object.
  7. What are CLIP Sensors and how to use them

    typescript

    CLIP Sensors are software constructs that allow you to integrate external objects and statuses into the Hue Bridge. Unlike hardware sensors, CLIP sensors are updated via API calls, allowing you to represent external data (like humidity from a custom IP sensor) as a sensor resource within the Hue ecosystem.

    To use them, you can either access existing sensors via the v3.api.sensors API or create new ones using the v3.model.createCLIP[xxx]Sensor() functions.

    Mandatory Properties

    When creating a new CLIP sensor, you must set the following properties:

    • type: The sensor type (read-only, set at instantiation).
    • modelid: A unique identifier for the hardware model.
    • manufacturername: The name of the manufacturer.
    • uniqueid: A unique ID (e.g., the device's MAC address).
    • swversion: The software version running on the sensor.
    • name: A human-readable name (can be modified after creation).

    Common Properties

    All CLIP sensors also support:

    • id: Read-only ID assigned by the Hue Bridge.
    • lastupdated: Read-only timestamp of the last update.
    • on: get/set boolean to turn the sensor on/off. If off, state changes are not reflected.
    • reachable: get/set boolean indicating communication status.
    • battery: get/set battery percentage.
    • url: get/set optional URL for the CLIP sensor.
  8. Understand the v3 API and Promise support

    typescript
    The v3 API is designed to work with native JavaScript Promises. You can use standard Promise chaining with .then() and .catch(), or utilize async/await syntax in your code. As of version 4.0.0, the library provides complete coverage for the Philips Hue REST API.
  9. Use the Hue Remote API

    typescript

    The library supports interacting with the Hue Remote API in addition to local network connections. While most endpoints function similarly to the local API, there are some limitations on remote endpoints.

    Setup involves either:

    1. Connecting from scratch (remote access setup).
    2. Using existing OAuth tokens.

    Refer to the docs/remoteApi.md for full documentation and the examples/v3/remote/ directory for implementation details.

  10. Understand the Light abstraction

    typescript

    In node-hue-api, all lights on the Hue Bridge are represented by a Light instance. Because different hardware supports different features, the properties available on a Light instance depend on its underlying type.

    Known light types include:

    • On Off Light
    • Dimmable Light
    • Color Light
    • Color Temperature Light
    • Extended Color Light