Control LIFX WiFi light bulbs from Google Apps Script
Here is a simple apps script to control LIFX WiFi light bulbs. It turns on the bulbs in the morning until a little after sunrise and then again in the evening before sunset until a configurable time. As a bonus it will also rotate between some seasonal colors in the evening on several holidays.
To get this up and running you need a LIFX access token (from here) and a selector. If you have a group of lights called bob this would be group:bob. It could also be a single bulb ID like id:d3b2f2d97452 (more details here). There are some other settings you probably want to change: Latitude and Longitude (if you don't live in San Francisco for some reason), OnHour and OffHour for the time to switch on before sunrise and off after sunset, DaytimeOffsetMins is the minutes to wait after sunrise before switching off and before sunset for switching on, and then finally default color, brightness and fade time.
In Google Drive create a new apps script project and copy in the following code:
Configure the settings at the top in the code editor. Switch to settings (cog at the bottom of the right hand menu) and make sure the script time zone is correct for your location. Back in the code editor run the function nightSchedule() to check that everything is working as expected. This will print the calculated times and then switch the light on or off. Once you're happy with this, go to the Triggers page (clock icon) and set nightSchedule() to run as often as you like (I use every minute).
The current design of the script will update the bulb(s) every time you run it (so if you manually toggle on during the day the script will turn the light off the next time it runs). It would be possible to store the current state in script settings if you want it to behave differently. You could also add or remove holidays and colors to use to celebrate them. If you have other ideas or enhancements please leave a comment below.
Thanks to LIFX for a well documented and easy to use API. Also to sunrise-sunset.org for making a free API that provides sunrise and sunset times for any location.
For many years I had a similar setup using a Philips Hue and IFTTT. Philips pulled support for their proprietary hub and I wasn't about to buy a new one and the IFTTT triggers didn't provide a lot of control over the exact time to switch a bulb on or off. LIFX plus apps script is a much better solution. And it's at least half way towards the build-it-myself smart home philosophy I swore to a couple of years ago.
More Google Apps Script Projects
- Get an email when your security camera sees something new (Apps Script + Cloud Vision)
- Get an email if your site stops being mobile friendly (no longer available)
- Export Google Fit Daily Steps, Weight and Distance to a Google Sheet
- Email Alerts for new Referers in Google Analytics using Apps Script
- Animation of a year of Global Cloud Cover
- How to backup Google Photos to Google Drive automatically after July 2019 with Apps Script
- Using the Todoist API to set a due date on the Alexa integration to-do list (with Apps Script)
- Automate Google PageSpeed Insights and Core Web Vitals Logging with Apps Script
- Using the Azure Monitor REST API from Google Apps Script
- Monitor page index status with Google Sheets, Apps Script and the Google Search Console API
(Published to the Fediverse as: Control LIFX WiFi light bulbs from Google Apps Script #software #appsscript #drive #philips How to use Google Apps Script to control LIFX WiFi light bulbs and switch lighting on in the morning and evening around sunrise and sunset (plus special color programs for holidays). )
Add Comment
All comments are moderated. Your email address is used to display a Gravatar and optionally for notification of new comments and to sign up for the newsletter.