Nest Automatic Humidity Control


#1

I had a central humidifier installed on my furnace yesterday and wired it into my Nest. I was disappointed to discover that the “smart” Nest only allows for a static minimum target humidity setting, rather than automatically adjusting the target humidity based on outside temperature. It’s necessary to reduce the target humidity as temperature drops to prevent condensation from forming inside your home and potentially damaging things. I was further disappointed to see that Nest removed the ability to set the target humidity via their official API. Using the unofficial Nest API and HASS I was able to do what I wanted fairly easily, and am sharing it here for anyone else who wants to do the same. I wrote my script in PHP using this library - https://github.com/gboudreau/nest-api.

I’m using Weather Underground for my weather sensor: https://home-assistant.io/components/sensor.wunderground. I created a trigger using the outside temperature sensor to call my PHP script.

configuration.yaml changes:

automation:
- alias: "Auto target humidity control on Nest"
  trigger:
    platform: state
    entity_id: sensor.pws_temp_f
  action:
    service: shell_command.update_nest_target_humidity

shell_command:
  update_nest_target_humidity: 'php /home/hass/scripts/update_nest_target_humidity.php {{states.sensor.pws_temp_f.state}} {{states.sensor.pws_relative_humidity.state}}'

and here’s my PHP script:

<?php
require_once('nest.class.php');

// Your Nest username and password.
define('USERNAME', 'YOUR_NEST_USERNAME');
define('PASSWORD', 'YOUR_NEST_PASSWORD');

define("MAX_HUMIDITY", 45);
define("MIN_HUMIDITY", 15);
define("LOG_FILENAME", "/home/hass/humidity.log");

$nest = new Nest();

// get the current inside humidity and target humidity
$nest_info = $nest->getDeviceInfo();
$current_target_humidity = $nest_info->target->humidity;
$inside_hum = $nest_info->current_state->humidity;

//print_r($nest_info);
//print "Current Nest target humidity = $current_target_humidity%, actual humidity = $inside_hum%\n";

if (!isset($argv[1])) {
        throw new Exception("Missing input for outside temperature");
}

$outside_temp = $argv[1];
$outside_hum = $argv[2];

// set target humidity based on outside temperature (in Fahrenheit).  adjust these to your liking
if ($outside_temp >= 40) {
        $target_hum = 45;
} else if ($outside_temp >= 30) {
        $target_hum = 40;
} else if ($outside_temp >= 20) {
        $target_hum = 35;
} else if ($outside_temp >= 10) {
        $target_hum = 30;
} else if ($outside_temp >= 0) {
        $target_hum = 25;
} else if ($outside_temp >= -10) {
        $target_hum = 20;
} else if ($outside_temp >= -20) {
        $target_hum = 15;
}

// safety check
$target_hum = max($target_hum, MIN_HUMIDITY);
$target_hum = min($target_hum, MAX_HUMIDITY);

// only make the call to Nest if we are changing the target humidity from what it is set to now
$should_change = $target_hum != $current_target_humidity;

$log_string = "Maintaining Nest target humidity of $target_hum% for outside temp $outside_temp (current humidity inside = $inside_hum%, outside = $outside_hum%)";

if ($should_change) {
        $log_string = "Setting Nest target humidity to $target_hum% for outside temp $outside_temp (current humidity inside = $inside_hum%, outside = $outside_hum%)";
}

debug_log($log_string);

if ($should_change) {
        $return = $nest->setHumidity($target_hum);
        if ($return != 1) {
                debug_log("ERROR: Nest returned: $return\n");
        }
}

function debug_log($message) {
        $date = date('m-d-Y H:i:s');

        $message .= "\n";
        file_put_contents(LOG_FILENAME, "$date $message", FILE_APPEND);
        print $message;
}

Hopefully this is useful to someone else.


New nest component - loss of functionality?
#2

Thanks for sharing this with the community! I never get tired of seeing creative solutions people come up with using HA.


#3

How were you able to integrate your code with Nest to allow it to automatically controller humidity settings?


#4

The PHP script I posted above is what talks to the Nest and sets the target humidity. Home Assistant calls the PHP script whenever the outdoor temperature changes.


#5

Hi @joe248! Super new to the whole automation world, have had many issues with fluctuating temps causing condensation in my house using the Nest. Wondering if you can help guide me through the process of how this works! Appreciate the help!

Once again sorry newbie with all this…

Andrew


#6

Sure, I can try to help guide you through it. Do you have Home Assistant installed and running yet?

Also, I’m assuming your Nest is set up to control your central humidifier?


#7

When you say “wired” to your nest, you literally had to send a wire to the nest directly and connect? If so, which labeled one?

EDIT: Never mind, I found which one it goes to.


#8

Hi,

I know I am reviving an old thread here but stumbled across this and this seems to be the only way to automatically control the target humidity from nest based on outside temperature. I have been able to run your php script successfully to update the target humidity from my rpi3b+ running on Raspbian Stretch Lite since I’ve installed the php7 packages on my raspberry pi. However, I am unable to execute the php script from Home Assistant as defined in your configuration.yaml, as there seems to be no php integration within hassio.

I would be very interested in knowing how you did manage to call that php script from the shell command as it seems to be the missing piece for me to achieve full automation on the nest humidity.

Thanks!


#9

Sorry, I don’t use hassio and don’t really know anything about it. Are you not able to install PHP on it? What about looking for a Nest unofficial API library in python and re-writing my script to use that?


#10

Couldn’t find a way to install php on it no, and from what I’ve seen people suggest converting php scripts to python. I may have found a NEST python API as you suggested, that may be the way to go for me.

Out of curiosity, what were you running HASS on ? Were you running it on Hassbian ? Either way, thanks for sharing your script, I’m quite happy I may get to achieve automation out of this.

Cheers


#11

No problem; glad someone else got some use out of it. I’m using a PI 3B running the latest Raspbian.


#12

Signed up for an account to ask this question:

I’m trying to get this set up on a Raspberry Pi, but I’m not getting any response back from the Nest as far as I can tell. Does the nest.class.php still work? Do I need to use the same password / account I would use to log into the Nest website, or should I be using the client_id and secret from setting up a developer account? Is there some other authentication trick I’m missing here?

Thanks!


#13

Yes, it’s still working for me. Use the email and password you use to log into your Nest account, not the client_id / secret. There’s nothing else required to authenticate. Did you try running the library’s example script here?


#14

I did - right now I’m trying to run a basic script that does nothing but run getDeviceInfo() and print the results. I get a long string of PHP errors that imply that when it tried to create the new instance of the Nest class it basically failed to do so. (index out of range for index 0, etc.) The line numbers for the errors show that it is getting into that routine in the class, but I haven’t started line-by-line tracebacks yet, because I wanted to verify that Nest hadn’t slammed the door on this API first.


#15

Figured at least the first problem out - the API does NOT work with family member accounts. I had set up a separate account to use with the Pi, but it was getting no data back from nest. When I logged in as the owner, I started getting data.