Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Turn on the Nest in fan-only mode when it's too hot in the living room.
#!/bin/bash
API_KEY='Authorization: Bearer API_KEY_RETRIEVED_USING_PIN_CODE'
API_URL='https://developer-api.nest.com'
TS_ID='curl_/devices_to_get_this_hash'
TS="devices/thermostats/${TS_ID}"
MIN_TEMP=70
THRESHOLD=2
api_curl() {
curl -L -H "${API_KEY}" "$@"
}
get() {
api_curl -X GET "${API_URL}/$1"
}
put() {
api_curl -X PUT -d "$2" "${API_URL}/$1"
}
give_up() {
echo "$@"
exit 1
}
current=( $(get "$TS" | jq -r '. | "\(.is_online) \(.fan_timer_active) \(.hvac_state) \(.ambient_temperature_f) \(.target_temperature_f) \(.device_id)"') )
#current=( $(cat nest_api.thermo | jq -r '. | "\(.is_online) \(.fan_timer_active) \(.hvac_state) \(.ambient_temperature_f) \(.target_temperature_f) \(.device_id)"') )
if [[ $TS_ID != "${current[$((${#current[*]}-1))]}" ]]; then
give_up "API output did not contain a valid device_id"
fi
if [[ ${current[0]} != "true" ]]; then
give_up "Thermostat is not online"
fi
if [[ ${current[1]} != "false" ]]; then
give_up "Fan is not stopped"
fi
if [[ ${current[2]} != "off" ]]; then
give_up "Heater is not off"
fi
if [[ ${current[4]} -lt $MIN_TEMP ]]; then
give_up "Target temperature ${current[5]} is below $MIN_TEMP F"
fi
if [[ ${current[4]} -ge ${current[3]} ]]; then
give_up "Current temperature ${current[3]} F is not more than target temperature ${current[4]} F"
fi
if (( ${current[3]} - ${current[4]} < $THRESHOLD )); then
give_up "Current temperature ${current[3]} F is not $THRESHOLD F more than target temperature ${current[4]} F"
fi
echo "Turning on fan, current ${current[3]} F versus target ${current[4]} F"
put "$TS" '{"fan_timer_active":true}'
@floatingatoll

This comment has been minimized.

Copy link
Owner Author

@floatingatoll floatingatoll commented Jan 8, 2016

I don't have an AC, but I have a plain heater, and it has a fan-only wire, so I ran that to the Nest. Unfortunately, the Nest doesn't currently understand that I want it to run the fan when the temperature exceeds a threshold, to redistribute that heat throughout the house, so this script does that.

To use this, you need to sign up for a Nest Developer account. They're free for individuals and as long as you only use this on your own thermostat, you don't have to do any sort of approval process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment