Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does creating / activating scenes work? #46

Closed
hansmbakker opened this issue Feb 8, 2015 · 3 comments
Closed

Does creating / activating scenes work? #46

hansmbakker opened this issue Feb 8, 2015 · 3 comments

Comments

@hansmbakker
Copy link

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Author

Aah, I understand. Thank you for repairing it this quick 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants