Phillips HUE light, mail detection and voice control

Hello everyone.

This is a very simple script to turn on your lights, just replace the IP adress and the api code (username)

global BridgeAddress
global apiKey
set BridgeAddress to "192.168.1.6"
set apiKey to "newdeveloper"
global turnOn

set turnOn to the quoted form of "{\"on\": true,\"hue\": 0, \"sat\": 0,\"bri\": 254,\"transitiontime\": 0}"
do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/4/state/"
do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/5/state/"

set WHITE1 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & 255 & ",\"transitiontime\": 0}"
set WHITE1 to the quoted form of WHITE1



do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/4/state/"
do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/5/state/"

To make a turn off script just replace the on:true by false.

The funny thing is that i used the vocal recognition software to trigger those. It doesnt work every time ( i guess it’s due to my accent as a non english speaker) but it works quite well in my living room :wink:

here’s a demo :

http://www.youtube.com/watch?v=xIkCIXYLsBo

To play the song add:
(You have to install playsound first.)

set soundfile to (POSIX file ("/Users/djbennyj/Documents/ziik/Daft Punk/Discovery (2001)/Daft Punk - One more time.mp3/")) as Unicode text
tell application "Play Sound"
	play soundfile
end tell

The last script is a one i use to tell me if any mail arrive in my inbox (it was not made by me, i just modified it, but thought it was cool to have it here, as i didnt find a lot of hue scripts on macscripter).
Its triggerd by mail.app, and it flashes the lights. I
If i get a ffacebook notification it flashes blue (the script below), and if the sender is one in my work contact list it flashes red. The test is made by mail.app, the scripts just store the lights state, flashes the lights, then restore the lights to their previous state.

Here’s the one that flashes blue :

global BridgeAddress
global apiKey
set BridgeAddress to "192.168.1.6"
set apiKey to "newdeveloper"

global turnOn
global turnOff
set turnOn to the quoted form of "{\"on\": true,\"hue\": 0, \"sat\": 0,\"bri\": 254,\"transitiontime\": 0}"
set turnOff to the quoted form of "{\"on\": false,\"hue\": 0, \"sat\": 0,\"transitiontime\": 0}"

run evaluateState


script flasher
	
	set Lamp1State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/"
	set Lamp2State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/"
	
	set the_String to Lamp1State
	try
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":", ","}
		-- do script steps here
		set these_items to the text items of the_String
		set AppleScript's text item delimiters to oldDelims
	on error
		set AppleScript's text item delimiters to oldDelims
	end try
	
	set Lamp1power to (item 3 of these_items)
	set Lamp1bri to (item 5 of these_items)
	set Lamp1hue to (item 7 of these_items)
	set Lamp1sat to (item 9 of these_items)
	set Lamp1ct to (item 14 of these_items)
	set Lamp1x to (item 11 of these_items)
	set Lamp1y to (item 12 of these_items)
	
	set the_String to Lamp2State
	try
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":", ","}
		set these_items to the text items of the_String
		set AppleScript's text item delimiters to oldDelims
	on error
		set AppleScript's text item delimiters to oldDelims
	end try
	
	set Lamp2power to (item 3 of these_items)
	set Lamp2bri to (item 5 of these_items)
	set Lamp2hue to (item 7 of these_items)
	set Lamp2sat to (item 9 of these_items)
	set Lamp2ct to (item 14 of these_items)
	set Lamp2x to (item 11 of these_items)
	set Lamp2y to (item 12 of these_items)
	
	
	set BLUE1 to "{\"hue\": " & 46920 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
	set BLUE1 to the quoted form of BLUE1
	set BLUE2 to "{\"hue\": " & 25000 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
	set BLUE2 to the quoted form of BLUE2
	
	set RED1 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
	set RED1 to the quoted form of RED1
	set RED2 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
	set RED2 to the quoted form of RED2
	
	set WHITE1 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
	set WHITE1 to the quoted form of WHITE1
	set WHITE2 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
	set WHITE2 to the quoted form of WHITE2
	
	
	delay 0.2
	
	repeat 10 times
		delay 0.2
		delay 0.1
		do shell script "curl --request PUT --data " & BLUE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
		do shell script "curl --request PUT --data " & BLUE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
		delay 0.1
		do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
		do shell script "curl --request PUT --data " & WHITE2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
		delay 0.1
		do shell script "curl --request PUT --data " & BLUE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
		do shell script "curl --request PUT --data " & BLUE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
	end repeat
	
	set lamp1Restore to "{\"on\": true,\"bri\": " & Lamp1bri & ",\"hue\": " & Lamp1hue & ",\"xy\": " & Lamp1x & "," & Lamp1y & ",\"sat\": " & Lamp1sat & ",\"ct\": " & Lamp1ct & ",\"transitiontime\": 0}"
	set lamp1Restore to the quoted form of lamp1Restore
	
	do shell script "curl --request PUT --data " & lamp1Restore & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
	
	
	set lamp2Restore to "{\"on\": true,\"bri\": " & Lamp2bri & ",\"hue\": " & Lamp2hue & ",\"xy\": " & Lamp2x & "," & Lamp2y & ",\"sat\": " & Lamp2sat & ",\"ct\": " & Lamp2ct & ",\"transitiontime\": 0}"
	set lamp2Restore to the quoted form of lamp2Restore
	
	do shell script "curl --request PUT --data " & lamp2Restore & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
end script


script evaluateState
	
	set Lamp1State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/"
	set Lamp2State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/"
	
	set the_String to Lamp1State
	try
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":", ","}
		-- do script steps here
		set these_items to the text items of the_String
		set AppleScript's text item delimiters to oldDelims
	on error
		set AppleScript's text item delimiters to oldDelims
	end try
	set Lamp1power to (item 3 of these_items)
	set Lamp1bri to (item 5 of these_items)
	set Lamp1hue to (item 7 of these_items)
	set Lamp1sat to (item 9 of these_items)
	set Lamp1ct to (item 14 of these_items)
	set Lamp1x to (item 11 of these_items)
	set Lamp1y to (item 12 of these_items)
	
	set the_String to Lamp2State
	try
		set oldDelims to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {":", ","}
		set these_items to the text items of the_String
		set AppleScript's text item delimiters to oldDelims
	on error
		set AppleScript's text item delimiters to oldDelims
	end try
	set Lamp2power to (item 3 of these_items)
	set Lamp2bri to (item 5 of these_items)
	set Lamp2hue to (item 7 of these_items)
	set Lamp2sat to (item 9 of these_items)
	set Lamp2ct to (item 14 of these_items)
	set Lamp2x to (item 11 of these_items)
	set Lamp2y to (item 12 of these_items)
	
	if Lamp1power = "true" and Lamp2power = "true" then
		run flasher
	else
		do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
		do shell script "curl --request PUT --data " & turnOn & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
		delay 1
		
		set Lamp1State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/"
		set Lamp2State to do shell script "curl --request GET http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/"
		
		set the_String to Lamp1State
		try
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {":", ","}
			-- do script steps here
			set these_items to the text items of the_String
			set AppleScript's text item delimiters to oldDelims
		on error
			set AppleScript's text item delimiters to oldDelims
		end try
		
		set Lamp1power to (item 3 of these_items)
		set Lamp1bri to (item 5 of these_items)
		set Lamp1hue to (item 7 of these_items)
		set Lamp1sat to (item 9 of these_items)
		set Lamp1ct to (item 14 of these_items)
		set Lamp1x to (item 11 of these_items)
		set Lamp1y to (item 12 of these_items)
		
		set the_String to Lamp2State
		try
			set oldDelims to AppleScript's text item delimiters
			set AppleScript's text item delimiters to {":", ","}
			set these_items to the text items of the_String
			set AppleScript's text item delimiters to oldDelims
		on error
			set AppleScript's text item delimiters to oldDelims
		end try
		
		set Lamp2power to (item 3 of these_items)
		set Lamp2bri to (item 5 of these_items)
		set Lamp2hue to (item 7 of these_items)
		set Lamp2sat to (item 9 of these_items)
		set Lamp2ct to (item 14 of these_items)
		set Lamp2x to (item 11 of these_items)
		set Lamp2y to (item 12 of these_items)
		
		
		set BLUE1 to "{\"hue\": " & 25000 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
		set BLUE1 to the quoted form of BLUE1
		set BLUE2 to "{\"hue\": " & 25000 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
		set BLUE2 to the quoted form of BLUE2
		
		set RED1 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
		set RED1 to the quoted form of RED1
		set RED2 to "{\"hue\": " & 0 & ", \"sat\": 254,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
		set RED2 to the quoted form of RED2
		
		set WHITE1 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp1bri & ",\"transitiontime\": 0}"
		set WHITE1 to the quoted form of WHITE1
		set WHITE2 to "{\"hue\": " & 0 & ", \"sat\": 0,\"bri\": " & Lamp2bri & ",\"transitiontime\": 0}"
		set WHITE2 to the quoted form of WHITE2
		
		
		delay 0.2
		
		repeat 2 times
			delay 0.2
			#do shell script "curl --request PUT --data " & RED1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
			do shell script "curl --request PUT --data " & BLUE2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
			delay 0.2
			do shell script "curl --request PUT --data " & WHITE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
			do shell script "curl --request PUT --data " & WHITE2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
			delay 0.2
			do shell script "curl --request PUT --data " & BLUE1 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
			#do shell script "curl --request PUT --data " & RED2 & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
		end repeat
		
		do shell script "curl --request PUT --data " & turnOff & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/3/state/"
		delay 0.5
		do shell script "curl --request PUT --data " & turnOff & " http://" & BridgeAddress & "/api/" & apiKey & "/lights/2/state/"
	end if
	
	
end script


have fun :wink: