Why does this Applescript often fail the first time, but always work after that?
You're right, I didn't test it well enough. The error was shown when System Events was not already running. As another workaround, I added launch application "System Events" and delay 0.2 to the start:

launch application "System Events"
delay 0.2
ignoring application responses
    tell application "System Events" to tell process "QuickHue"
        click menu bar item 1 of menu bar 1
    end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "QuickHue"
    tell menu bar item 1 of menu bar 1
        click menu item 2 of menu 1
    end tell
end tell


If it still doesn't work, try increasing the delays.