peter-murray / node-hue-api

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

Does creating / activating scenes work? #46

Closed hansmbakker closed 9 years ago

hansmbakker commented 9 years ago

I'm trying to store and recall scenes. The scenes are created with the correct lamp selection on the bridge, but when I call api.activateScene using the scene id, the lamps do not respond.

Unfortunately I cannot verify the light states that are stored for each scene, only the lamp set that makes up the scene.

This is my code. It shows each scene once (because the first time the light states are set separately using setLightState instead of using the scene mechanism). After that the scenes mechanism (api.createScene, api.activateScene) should take over, but the lamps do not change according to the called scenes.

index.js:

var hue = require("node-hue-api"),
    HueApi = hue.HueApi,
    lightState = hue.lightState;

var displayResult = function (result) {
    console.log(JSON.stringify(result, null, 2));
};
var displayError = function (err) {
    console.log(JSON.stringify(err, null, 2));
};

var hostname = "xxx.xxx.xxx.xxx",
    username = "someusername",
    api;
api = new HueApi(hostname, username);

//load initial config from json
var sceneConfig = require('./scenes.json');

//set the lightstate for each light separately when they're not stored as a scene yet
var setStates = function (lightStates, callback) {
    var todo = Object.keys(lightStates).length;
    console.log(todo);

    //safety function to make sure all states are set before saving to scene
    var lightDone = function () {
        todo--;
        console.log(todo);
        if (todo === 0) {
            callback();
        }
    };

    for (var lightId in lightStates) {
        api.setLightState(lightId, lightStates[lightId])
            .then(lightDone)
            .fail(displayError)
            .done();
    }
};

//activate a scene from the scene config. if it has no Hue scene id yet,
//set the states separately and store them as a Hue scene
var activateScene = function (sceneName) {
    var scene = sceneConfig[sceneName];
    if (scene !== null) {
        if (scene.id !== undefined) {
            console.log('using predefined scene id ' + scene.id);

            api.activateScene(scene.id)
                .then('predefined scene result: ' + console.log)
                .done();
        } else if (scene.states !== undefined) {
            setStates(scene.states, function () {
                var lightIds = Object.keys(scene.states);
                api.createScene(lightIds, '', function (err, result) {
                    if (err) throw err;

                    //store the created scene id in the scenes config object
                    scene.id = result.id;
                    sceneConfig[sceneName] = scene;
                    console.log('scene created.');
                    console.log(result);
                });
            });
        }
    }
};

//switch between scenes every 4 seconds
var switchBool = true;
setInterval(function () {
    var sceneToActivate = switchBool ? 'gold' : 'sleep';
    activateScene(sceneToActivate);
    switchBool = !switchBool;
}, 4000);

scenes.json:

{
    "gold": {
        "states": {
            "4": {
                "on": true,
                "bri": 123,
                "hue": 14582,
                "sat": 251
            },
            "5": {
                "on": true,
                "bri": 123,
                "hue": 14582,
                "sat": 251
            },
            "6": {
                "on": true,
                "bri": 123,
                "hue": 14582,
                "sat": 251
            }
        }
    },
    "sleep": {
        "states": {
            "1": {
                "on": false
            },
            "2": {
                "on": true,
                "bri": 254
            },
            "3": {
                "on": true,
                "bri": 123,
                "hue": 14582,
                "sat": 251
            },
            "4": {
                "on": false
            },
            "5": {
                "on": false
            },
            "6": {
                "on": false
            }
        }
    }
}
peter-murray commented 9 years ago

It was a mistake in the refactoring on my part, the scene id was not being sent in the body of the request. Release 1.0.5 has just been published to correct this.

peter-murray commented 9 years ago

If you set the NODE_DEBUG environment variable to include the string hue-api then it will show you the commands being sent to the bridge on the console, which can assist you if you run into any further issues.

hansmbakker commented 9 years ago

Aah, I understand. Thank you for repairing it this quick :+1: