Using Hue motion sensor temperature with Geektool and Keyboard Maestro

Using Hue motion sensor temperature with Geektool and Keyboard Maestro

[lead dropcap="yes"]One part of our smart home setup is our Philips Hue lighting system. Over the years I've replaced many of our old light bulbs with Hue smart bulbs in various fixtures, which allows us not only to control the lights with automation and schedules, but also with our voices using Alexa and Siri. We can also group control of them through compatible switches without having to rewire what's in the walls.[/lead]

Philips also makes a motion sensor, which is great for rooms that don't get a lot of traffic during the day, meaning that the lights go off when no one's using them or where people often forget to turn them off. I have one of these sensors in my office and another in the pantry/laundry room. What's nice is that these sensors also have thermometers in them, which means I can track the temperature in the rooms. Unfortunately, to find out out what the temp is, you have to open either the Home app or the Hue app to find out. Unless you do a little home programming.

I wanted to have the current ambient temperature of both rooms to be constantly updated and displayed on my desktop and, also, to get notifications if the temperature gets too cold in the winter or too hot in the summer and I might want to turn on the heater or the air conditioner to adjust the temperature.1

The first step is to get access to your Hue's API key. This is a unique userid that gives you access to your Hue bridge and thus should be kept private and secure. Hue offers a 3-step getting started process on their site for learning the basics of your the Hue API (application programming interface). After that, you'll want to register for a free developer account on the Hue site as well.

The next step is to use the CLIP API Debugger tool to find out about your system. This is a web page found at https://[YOUR_BRIDGE_IP_ADDRESS]/debug/clip.html. You'll find your bridge IP address in the previous step or in your wifi router's interface. Once you're on that page, in the URL space enter api/[YOUR_API_KEY]/ and click on GET. In "command response:", you will get a listing of every item in your Hue system and their various properties.

Look for your motion sensor, which will be in under something like "15":{. The number of the temperature sensor part of the device is typically the one before the motion sensor, in this case "14".

So, to get the temperature of the sensor in the office, I need to grab the data using a programming language. In this case I used AppleScript, as that's what I'm familiar with. I also used the great Geektool for putting it on my desktop. (Because the Hue API sends the information in JSON format, you'll need the free "JSON Helper for AppleScript" from the Mac App Store.)
Here's the AppleScript:

on round_truncate(this_number, decimal_places)
if decimal_places is 0 then
set this_number to this_number + 0.5
return number_to_text(this_number div 1)
end if

set the rounding_value to "5"
repeat decimal_places times
set the rounding_value to "0" & the rounding_value
end repeat
set the rounding_value to ("." & the rounding_value) as number

set this_number to this_number + rounding_value

set the mod_value to "1"
repeat decimal_places - 1 times
set the mod_value to "0" & the mod_value
end repeat
set the mod_value to ("." & the mod_value) as number

set second_part to (this_number mod 1) div the mod_value
if the length of (the second_part as text) is less than the decimal_places then
repeat decimal_places - (the length of (the second_part as text)) times
set second_part to ("0" & second_part) as string
end repeat
end if

set first_part to this_number div 1
set first_part to number_to_string(first_part)
set this_number to (first_part & "." & second_part)

return this_number
end round_truncate

on number_to_string(this_number)
set this_number to this_number as string
if this_number contains "E+" then
set x to the offset of "." in this_number
set y to the offset of "+" in this_number
set z to the offset of "E" in this_number
set the decimal_adjust to characters (y - (length of this_number)) thru ¬
-1 of this_number as string as number
if x is not 0 then
set the first_part to characters 1 thru (x - 1) of this_number as string
else
set the first_part to ""
end if
set the second_part to characters (x + 1) thru (z - 1) of this_number as string
set the converted_number to the first_part
repeat with i from 1 to the decimal_adjust
try
set the converted_number to ¬
the converted_number & character i of the second_part
on error
set the converted_number to the converted_number & "0"
end try
end repeat
return the converted_number
else
return this_number
end if
end number_to_string
tell application "JSON Helper"
set result to (fetch JSON from "http://[YOUR_BRIDGE_URL]/api/[YOUR_API_KEY]")
tell state of result
set officeTemp to its temperature
set officeTempF to (officeTemp / 100 * 9 / 5 + 32)
end tell
end tell
round_truncate(officeTempF, 2)

To explain the above, the sensor gives the temperature as a four-digit number, like 1753, which is 17.53 degrees Celsius. The script gets the temperature from Hue, converts it to Fahrenheit, and rounds it down to two decimal points. (If you just want to round down to a whole number change the final line to round_truncate(officeTempF,0). You notice the script begins with two functions that do the rounding and truncating.

Test the script in Script Editor and the save it in your Documents folder as something like officeTemp.scpt. Then open up GeekTool and create a shell widget on your desktop. (I also created widgets for the "Office Temp:" label and the "°F" label at the end.) In the widget for the temperature, set the refresh to 30 seconds, and in "Command:" put osascript ~/Documents/[SCRIPT_NAME].scpt. And now you will have your sensor's temperature on your desk updated every 30 seconds.

Setting up the Notifications

The second part I built will notify me if I need to be prompted to turn on the heat or AC.2 For this I need something that will run a script every so often and can pop up a notification and for that Keyboard Maestro is the right tool for me.

I created a new macro and set the trigger to be "Every 30 minutes between 8:30am and 5:30pm every day," because that's usually when I'm in my office. Under actions, it starts with two different Applescripts.

Because I need the macro to distinguish between summer and winter, I need it to find out the current outdoor temperature for my location3. For that I use the Dark Sky API. Once again, you'll need to sign up for a free account4. That will give you the API key that you'll use in the first AppleScript and once again it will be unique to you and you'll want to keep it confidential. You'll also need to know your latitude and longitude, to three or four decimal points.5

Select "Execute AppleScript" as your first action and paste in this script, substituting your API and location data.

tell application "JSON Helper"
    set weatherdata to fetch JSON from "https://api.darksky.net/forecast/[YOUR_DARKSKY_API_KEY]/42.[...],-71.[...]"
end tell

set currentTemp to temperature of currently of weatherdata as string
set currentTemp to round currentTemp 

Have the action save the results to variable "currentOutsideTemp". Then add another "Execute Applescript" action and paste in this script, which you'll recognize from Geektool widget:

tell application "JSON Helper"
    set result to (fetch JSON from "http://[BRIDGE_IP_ADDRESS]/api/[HUE_API_KEY]/sensors/18/")
    tell state of result
        set officeTemp to its temperature
        set officeTempF to (officeTemp / 100 * 9 / 5 + 32)
    end tell
end tell

Be sure to put your actual temperature sensor's number in the URL after "sensors" where mine says 18. Have the action save the results to variable "officeTemp". You're now going to select an "If, Then, Else" action. Under "If" the triggering condition is "all of the following are true" and put the following two conditions: "If the variable currentOutsideTemp is less than 40" (or whatever constitutes winter cold weather for you) and "If the variable officeTemp is less than 66" (or whatever constitutes unacceptably cold conditions in the room).

Under "Then" have it execute the following Notification action: "Title: It's getting cold in here" with "Message: The outside temp is %Variable%currentOutsideTemp%°F and the office temp is %Variable%officeTemp%°F. Turn on the heater?"

Under "Else", put another If-Then-Else action with the "If" triggering condition as "all of the following are true:" and the following two conditions: "If the variable currentOutsideTemp is greater than 80" or whatever constitutes summer hot weather for you) and "If the variable officeTemp is greater than 74" (or whatever constitutes unacceptably hot conditions in the room). The "Then" condition here is, you guessed it, a Notification action like the one above, but worded to tell you how hot it is inside and outside and suggest turning on the fan or AC. (You can leave the "Else" action blank.)

And that's it. Now, every 30 minutes during the workday, Keyboard Maestro will check inside and outside temps and alert you if you should consider changing things.

You don't have to stop here. There are all kinds of ways that knowing indoor and outdoor temperatures can help you with automations. If I had automatic blinds, I could raise or lower them depending on the temperature and time of day or if I put a fan on a smart plug, I could turn it on automatically when it gets too hot.6 Perhaps, you could set the temperature sensor as a secondary fire alarm, alerting you if temperatures get fire-hot. Or if your attic hits a certain temperature. And so on. If you come up with something interesting, please come back and let me know.

  1. It's a quirk of our home's HVAC that the office is on the far end of the house from the heat exchanger and the thermostat and thus is always hotter or cooler than the rest of the house.
  2. Won't I notice being hot or cold, you ask? Sometimes I get so into my work that I don't notice I'm cold or hot until it's very cold or very hot.
  3. Philips now makes an outdoor motion sensor, which I don't have and I'm not sure if it gives temperature like the indoor ones do.
  4. It's free for up to 1,000 API calls per day, so you need to be careful how often you hit this. That's why I have the triggers set to every 30 minutes during the business day.
  5. There a bunch of sites that can give you this information. I used whatsmygps.com.
  6. I would not, under any circumstances, put a space heater on a smart outlet or program it to come on or off unattended. That would be very dangerous.

Image Credit

  • shiveringman: Storyblocks | Copyright by owner. Used with permission.
Share:FacebookX

Archives

Categories