Showing posts with label scripting. Show all posts
Showing posts with label scripting. Show all posts

Sunday, 7 December 2014

Scripting to create effects with your Hue / LIFX bulbs

This tutorial uses Hue-topia (for Philips Hue bulbs) or LIFXstyle (for LIFX bulbs)

It also uses Applescript. Code is provided below (which you'll be able to modify and develop) and you'll need Applescript Editor which you'll find on your Mac.

This previous article explained how to send a command to switch to a named preset. As from version 1.4 of LIFXstyle and v1.8.1 of Hue-topia it's also possible to read and set the state of individual bulbs or groups (on/off, hue, brightness, whiteness) from a script.

Accessing a bulb / group

To get started, type:

tell application "LIFXStyle"
# or tell application "Hue-topia"
get properties of first lamp
end tell

The result should be something like:

{class:lamp, hue:39, whiteness:225, brightness:225, title:"Living A", identity:"d073d500cc1c", state:true}


You can access a bulb / group by its name or ID. We're using name here (actually called  'title' when scripting). But of course you can easily change this, so to futureproof your script, you might like to use the ID instead (actually called 'identity' when scripting). You can find the identity of a bulb by using the example above.

To access a particular bulb, use something like:

properties of first lamp whose title is "Living A"

(Obviously one of my lamps is called 'Living A' - change that for the name of one of your lamps) 

lamp  is one of your lamps
group  is one of your groups
lamps  gets you a collection containing all of your lamps
groups gets you a collection containing all of your groups

Accessing individual bulbs within a group 

(since HT v1.8.2 and LS v1.4.1)  group contains lamps so:

        set collectionOfLamps to a reference to lamps of (first group whose title is "AllBarSecurity")
        set brightness of first item of collectionOfLamps to 80

Changing the colour of a bulb

This example switches the bulb on and sets the colour to red:

set state of first lamp whose title is "Living A" to 1
set hue of first lamp whose title is "Living A" to 0  # zero is red
set whiteness of first lamp whose title is "Living A" to 0


Note a lamp must be on when you send hue, whiteness or brightness, otherwise the instruction will have no effect. That's just the way the bulbs work. 

Note also that the state being off isn't the same thing as the brightness being 0 (LIFX bulbs fade right down to nothing with the state still set to on. Hue bulbs' minimum brightness is still quite a bit of light.)

whiteness, brightness and hue can be an integer between 0 and 255. state is a boolean so can be true/false or 0/1

Some approximate colour values:

Red: 0
Green: 85
Blue: 170
Yellow: 32
Orange: 20
Purple: 200

Loops and pauses 

All the usual Applescript program flow applies here. The script below will cycle all of your lamps randomly red, green and white. (Perfect for Christmas)

tell application "LIFXStyle"
#or tell application "Hue-topia"
repeat
set n to (number of items in lamps)
set i to (random number from 1 to n)
set whiteness of lamp i to 0
set hue of lamp i to 85 # green 
delay 0.5
set i to (random number from 1 to n)
set whiteness of lamp i to 0
set hue of lamp i to 0 # red 
delay 0.5
set i to (random number from 1 to n)
set whiteness of lamp i to 255 # white 
delay 0.5
#press cmd . to stop
end repeat
end tell


This script cycles a given lamp through the whole colour spectrum:

tell application "LIFXStyle"
# or tell application "Hue-topia"
repeat
repeat with hueValue from 0 to 255
set (hue of first lamp whose title is "Living A") to hueValue
delay 0.1
end repeat
end repeat
end tell

For more documentation see AppleScript Editor's File > Open Dictionary... (choose LIFXstyle or Hue-topia)

If you write a cool script, do share.

Friday, 10 October 2014

Hue-topia is now scriptable

Hue-topia now offers limited scripting support (Hue-topia version 1.7.1 + ). It makes your presets available from other apps or triggered by certain actions.

Below is a brief explanation and then an example of how to control your bulbs remotely by sending yourself an email.

Your presets are available using the command preset:


The name of your preset is case-sensitive. The result will be 1 if the preset was found and triggered, or 0 if there was a problem.

In addition to your named presets, you can use "All on", "All off", "All blink" (the latter could be used to alert you to something like new email coming in).

Control your lights remotely

This example allows you to send an instruction to your lights from anywhere and any device. It sets up Mail to trigger a script when an email is received with a specific subject line.

1. Create a script that says:

tell application "Hue-topia"
preset "All on"

end tell

2. Save the script somewhere.

3. Create a rule in Mail (Preferences > Rules. You will need to add the new script to Mail ( to ~/Library/Application Scripts/com.apple.mail. Or choose “Open in Finder” from the rules window to open the folder so you can copy a script into it.)


4. Create a new script and rule for each preset you want to control by mail.

5. To test, send yourself an email with the subject you've specified in the rule.