peter-murray / node-hue-api

Node.js Library for interacting with the Philips Hue Bridge and Lights
Apache License 2.0
1.18k stars 145 forks source link

Enhancement: HueLabs Scenes #143

Closed foxriver76 closed 4 years ago

foxriver76 commented 4 years ago

It seems like when activating a scene id which represents a HueLabs Scene the related lights turn on but the scene is not really triggered (dynamic is missing like when triggering the scene via Hue App)

peter-murray commented 4 years ago

A lot of the HueLabs Scenes are more than just standard scenes, they introduce flags in the CLIP Sensors...

Is there a particular HueLabs scene that I can look into here so that I can attempt to reproduce the problem?

foxriver76 commented 4 years ago

Lets take https://labs.meethue.com/formulas/huelabs/meditation-lights as an example. So what I tried was activating it like a common scene by

const groupState = new v3.lightStates.GroupLightState();
groupState.scene(`e4SezFcXkCeBaXv`); // id of my meditation lights scene
api.groups.setGroupState(0, groupState);
peter-murray commented 4 years ago

Thanks for the details.

These are not standard Scene objects, but formulas.

If you take a look at the resourcelinks for the bridge (there is no API support around this in the library yet as it is on a longer list of things to add), the formula will create a resource link like so:

"62738": {
        "name": "Meditation lights",
        "description": "HaM_ofYZuQt91EaJlegnyg:0:O52Qr1gOPIGpgo+BiSiO1cGPryY",
        "type": "Link",
        "classid": 2,
        "owner": "985692e7-6abf-4043-b20e-30d75c0ab864",
        "recycle": true,
        "links": [
            "/rules/28",
            "/rules/26",
            "/rules/13",
            "/rules/12",
            "/rules/11",
            "/rules/10",
            "/scenes/36RwPeb36LqYbJl",
            "/sensors/189",
            "/sensors/188",
            "/groups/6"
        ]
    }

That lists all the things that make up the Meditation Lights Formula. You can use the http://[bridge ip address]/debug/clip.html URL to run GET /api/[valid-username]/resourcelinks on your bridge to see the equivalent.

In the two sensors that were created, both of them are CLIPGenericStatus with names of Start and stop and mystate.

Toggling the Start and stop sensor state, which is an integer value.

Turning this formula on is done using (the Start and stop sensor on my bridfge is id 189):

api.sensors.get(189)
      .then(sensor => {
        sensor.status = 1;
        return api.sensors.updateSensorState(sensor);
      });
api.sensors.get(189)
      .then(sensor => {
        sensor.status = 0;
        return api.sensors.updateSensorState(sensor);
      });
foxriver76 commented 4 years ago

How do you know that sensor 189 and not 188 is the one to control in this example? In the future it would be handsome to have a wrapper for this. What are your thoughts on this?

peter-murray commented 4 years ago

There are two sensors, the names are myState and Start and stop in the resourcelink. For my bridge 189 was Start and stop, based on the name I took a punt on how it works.

Most formulas are built like this, creating software sensors to control on/off and other intermediate behaviour.

I am starting work on resource links, but this is only a collection mechanism, and is fairly new to the APIs. The problem is it is merely a grouping construct and there is no naming conventions about what developers, official or otherwise will use these for.

There is no simple thing like all resourcelinks will have a sensor called x for turning on say a formula. They are completely free form for connecting things that may be related.