Last active
August 27, 2022 00:56
-
-
Save abfo/8137eb25fc70fbcc05048e7e3ac76990 to your computer and use it in GitHub Desktop.
LIFX light bulb control from Google Apps Script, sunrise, sunset and holiday colors. Full instructions at https://ithoughthecamewithyou.com/post/control-lifx-wifi-light-bulbs-from-google-apps-script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var LifxToken = ''; | |
var LifxSelector = ''; | |
var Latitude = 37.7749; | |
var Longitude = -122.4194; | |
var OnHour = 6; | |
var OffHour = 23; | |
var DaytimeOffsetMins = 25; | |
var DefaultOnColor = 'kelvin:2700'; | |
var DefaultBrightness = 0.75; | |
var DefaultFadeSeconds = 10.0; | |
function nightSchedule() { | |
// credit to https://sunrise-sunset.org/api for this simple API | |
var result = UrlFetchApp.fetch('https://api.sunrise-sunset.org/json?lat=' + Latitude + '&lng=' + Longitude + '&formatted=0'); | |
var resultJson = JSON.parse(result); | |
var sunrise = addMinutes(new Date(resultJson.results.sunrise), DaytimeOffsetMins); | |
var sunset = addMinutes(new Date(resultJson.results.sunset), -DaytimeOffsetMins); | |
var earlyOn = todayWithTime(OnHour, 0); | |
var sunriseOff = todayWithTime(sunrise.getHours(), sunrise.getMinutes()); | |
var sunsetOn = todayWithTime(sunset.getHours(), sunset.getMinutes()); | |
var lateOff = todayWithTime(OffHour, 0); | |
var now = new Date(); | |
console.log('On at (morning): ' + earlyOn); | |
console.log('Off at (morning): ' + sunriseOff); | |
console.log('On at (evening): ' + sunsetOn); | |
console.log('Off at (evening): ' + lateOff); | |
console.log('Time now: ' + now); | |
var color = DefaultOnColor; | |
var nowDay = now.getDate(); | |
var nowMonth = now.getMonth() + 1; | |
var lightsOnCheck = false; | |
// if sunrise is later than the early on time (otherwise no morning light needed) then on between earlyOn and sunriseOff | |
if (sunriseOff > earlyOn) { | |
if ((now >= earlyOn) && (now <= sunriseOff)) { | |
lightsOnCheck = true; | |
} | |
} | |
// in the evening on between sunsetOn and lateOff | |
if ((now >= sunsetOn) && (now <= lateOff)) { | |
lightsOnCheck = true; | |
// check for holidays | |
if ((nowMonth == 12) && ((nowDay == 24) || (nowDay == 25))) { | |
color = christmasColor(); | |
} | |
if ((nowMonth == 10) && (nowDay == 31)) { | |
color = halloweenColor(); | |
} | |
} | |
if (lightsOnCheck) { | |
lightsOn(color); | |
} else { | |
lightsOff(); | |
} | |
} | |
function lightsOn(color) { | |
// color formats https://api.developer.lifx.com/reference/colors | |
console.log('Switching ' + LifxSelector + ' ON: ' + color); | |
var request = { | |
"power": "on", | |
"color": color, | |
"brightness": DefaultBrightness, | |
"duration": DefaultFadeSeconds, | |
"fast": true | |
}; | |
sendControl(request); | |
} | |
function lightsOff() { | |
console.log('Switching ' + LifxSelector + ' OFF'); | |
var request = { | |
"power": "off", | |
"duration": DefaultFadeSeconds, | |
"fast": true | |
}; | |
sendControl(request); | |
} | |
function sendControl(jsonRequest) { | |
UrlFetchApp.fetch('https://api.lifx.com/v1/lights/' + LifxSelector + '/state', { | |
headers: { | |
Authorization: 'Bearer ' + LifxToken | |
}, | |
'method' : 'put', | |
'contentType' : 'application/json', | |
'payload' : JSON.stringify(jsonRequest, null, 2) | |
}); | |
} | |
function christmasColor() { | |
switch(Math.floor(Math.random() * 4)) { | |
case 0: | |
return '#225140'; | |
case 1: | |
return '#2d6d5c'; | |
case 2: | |
return '#85010b'; | |
default: | |
return '#a11616'; | |
} | |
} | |
function halloweenColor() { | |
switch(Math.floor(Math.random() * 4)) { | |
case 0: | |
return '#cc280e'; | |
case 1: | |
return '#ec5b07'; | |
case 2: | |
return '#f08b18'; | |
default: | |
return '#54248c'; | |
} | |
} | |
function addMinutes(date, minutes) { | |
return new Date(date.getTime() + minutes*60000); | |
} | |
function todayWithTime(hour, minute) { | |
var mydate = new Date(); | |
mydate.setHours(hour); | |
mydate.setMinutes(minute); | |
mydate.setSeconds(0); | |
return mydate; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment