Skip to content

Instantly share code, notes, and snippets.

@C4illin
Last active February 22, 2021 16:01
Show Gist options
  • Save C4illin/5a706aef121dea63652b1a389c72a0f2 to your computer and use it in GitHub Desktop.
Save C4illin/5a706aef121dea63652b1a389c72a0f2 to your computer and use it in GitHub Desktop.

A AutoHotKey script for controlling Hue lights that works with DiyHUE

Install

  1. Install hueadm
  2. Generate user token according to the hueadm documentation
  3. Install AutoHotKey
  4. Install the linux tools "grep" and "sort" (comes with git for windows)
  5. Run the .ahk script
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
#SingleInstance force
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
global User = "4d65727279204368726973746d617321" ; Look at the hueadm documentation
global Host = "192.168.1.1"
;DON'T CHANGE
global presses = 0
global bri := 0
global hueName
;LIGHT TOGGLE
!+k::lightToggle(1)
!+l::lightToggle(2)
;GROUP TOGGLE
!+o::
Run hueadm.cmd -H %Host% -U %User% group 1 on,,Hide
lightToggleBox("roomname is on")
Return
!+i::
Run hueadm.cmd -H %Host% -U %User% group 1 off,,Hide
lightToggleBox("roomname is off")
Return
;BRIGHTNESS CHANGE
!+p::changeBrightness(1, +30)
!+ö::changeBrightness(1, -30)
!+å::changeBrightness(2, +30)
!+ä::changeBrightness(2, -30)
changeBrightness(lamp, value) {
global User
global Host
global presses
; For persistence
global bri
global hueName
if (presses = 0) {
presses = 1
RunWait, hueadm.cmd -H %Host% -U %User% light 1 | grep -E "bri:|name:" | sort > "C:\temp\hue.txt",,Hide
FileReadLine, hueBri, C:\temp\hue.txt, 1
FileReadLine, hueName, C:\temp\hue.txt, 3
FileDelete, C:\temp\hue.txt
bri := SubStr(hueBri, 10)
} else {
presses = 2
}
SetTimer, KeyBri, -800
bri := bri + value
if (bri < 1) {
bri = 1
} else if (bri > 254) {
bri = 254
}
Run hueadm.cmd -H %Host% -U %User% light %lamp% =%bri%,,Hide
brightnessBox(bri, SubStr(hueName, 6))
}
KeyBri:
presses = 0
return
brightnessBox(bri, name) {
static Brightness
if (presses = 2) {
GuiControl,, Brightness, %bri%
SetTimer,lightToggleClose, -2000
return
}
IfWinExist, lightToggleBox
{
Gui, destroy
}
Gui, +LastFound +ToolWindow -Caption +alwaysontop
Gui, Font, cFFFFFF s10, verdana
Gui, Color, 19161E
Gui, Add, text, Center, %name%
Gui, Add, Progress, vBrightness w200 h20 cWhite Background171315 Range0-254, %bri%
SysGet, screenx, 0
SysGet, screeny, 1
xpos:=screenx-220
ypos:=screeny-102
Gui, Show, NoActivate x%xpos% y%ypos%, lightToggleBox
WinGetPos,,,width,h,lightToggleBox
xpos:=screenx-width-5
Gui, Show, NoActivate x%xpos% y%ypos%, lightToggleBox
SetTimer,lightToggleClose, -2000
}
lightToggle(lamp) {
global User
global Host
RunWait, hueadm.cmd -H %Host% -U %User% light 1 | grep -E " on:|name:" | sort > "C:\temp\hue.txt",,Hide
FileReadLine, hueStatus, C:\temp\hue.txt, 1
FileReadLine, hueName, C:\temp\hue.txt, 3
FileDelete, C:\temp\hue.txt
t1 := SubStr(hueStatus, 9)
if (t1 = "true") {
Run hueadm.cmd -H %Host% -U %User% light %lamp% off,,Hide
lightToggleBox(SubStr(hueName, 6) . " is off")
}
else {
Run hueadm.cmd -H %Host% -U %User% light %lamp% on,,Hide
lightToggleBox(SubStr(hueName, 6) . " is on")
}
}
lightToggleBox(message) {
IfWinExist, lightToggleBox
{
Gui, destroy
}
Gui, +LastFound +ToolWindow -Caption +alwaysontop
Gui, Font, cFFFFFF s10, verdana
Gui, Color, 19161E
Gui, Add, text, Center, %message%
SysGet, screenx, 0
SysGet, screeny, 1
xpos:=screenx-220
ypos:=screeny-75
Gui, Show, NoActivate x%xpos% y%ypos%, lightToggleBox
WinGetPos,,,width,,lightToggleBox
xpos:=screenx-width-5
Gui, Show, NoActivate x%xpos% y%ypos%, lightToggleBox
SetTimer,lightToggleClose, -2000
}
lightToggleClose:
Gui, destroy
Return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment