Skip to content

Instantly share code, notes, and snippets.

@asherkin
Created November 3, 2017 20:53
Show Gist options
  • Save asherkin/feae8cbbeb4bc898a7051790c8cbd4fd to your computer and use it in GitHub Desktop.
Save asherkin/feae8cbbeb4bc898a7051790c8cbd4fd to your computer and use it in GitHub Desktop.
'use strict';
const redis = require('redis').createClient();
const Firebase = require('firebase');
const nest = new Firebase('wss://developer-api.nest.com');
nest.auth('REDACTED');
const HueApi = require("node-hue-api").HueApi;
const hue = new HueApi('REDACTED', 'REDACTED');
const homeState = nest.child('structures/REDACTED/away');
let stateChangePending = true;
let currentState = 'unknown';
homeState.on('value', (snapshot) => {
const state = snapshot.val();
console.log('Home state changed from', currentState, 'to', state);
currentState = state;
stateChangePending = false;
if (state !== 'home') {
console.log('Nobody home, turning lights off');
hue.setGroupLightState(0, { on: false }, (err, res) => {
console.log('The lights have been turned off');
});
return;
}
console.log('Welcome home! turning lights on');
hue.setGroupLightState(0, { on: true, bri: 255, ct: 415 }, (err, res) => {
console.log('The lights have been turned on');
hue.setLightState(2, { bri: 100 }, (err, res) => {
console.log('and the desk has been dimmed');
});
});
});
redis.on('message', (channel, message) => {
if (stateChangePending) {
console.log('Motion detected while state change pending, ignoring');
return;
}
if (currentState === 'home') {
//console.log('Motion detected while currently home, ignoring');
return;
}
stateChangePending = true;
homeState.set('home');
});
redis.subscribe('scc.sensors.motion.door');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment