Created
March 19, 2019 19:06
-
-
Save ehowe/a00e07f7f59086d6c2e40adee42ce484 to your computer and use it in GitHub Desktop.
Nest to Sensibo integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
########## | |
# This is a script I wrote as an AWS lambda function to read sensor data from a Nest and set my Sensibo accordingly | |
# The API clients are written by me as well and are available at | |
# https://github.com/ehowe/sensibo-api and https://github.com/ehowe/nest-api | |
# but can also be installed with `gem install sensibo-api` and `gem install nest-api`. | |
# The lambda function requires 2 environment variables `API_KEY` for the Sensibo API and `ACCESS_TOKEN` for the Nest API. | |
# Those API keys will need to be generated in the appropriate developer consoles. | |
# After creating the Lambda function, you can create a scheduled task in CloudWatch to run the function at whatever interval | |
# you choose. Mine is set at 15 minutes. | |
# The function will send all output to CloudWatch as well, which you can act on if you choose. | |
########## | |
require "sensibo" | |
require "nest" | |
def lambda_handler(event:, context:) | |
nest_client = Nest.new | |
sensibo_client = Sensibo.new | |
nest = nest_client.thermostats.first | |
sensibo = sensibo_client.devices.first | |
nest_mode = nest.hvac_mode | |
nest_target_temperature = nest.target_temperature_f.round | |
old_mode = sensibo.mode | |
old_power_on = sensibo.power_on | |
old_target_temperature = sensibo.target_temperature | |
new_mode = nest_mode == "eco" ? sensibo.mode : nest_mode | |
new_power_on = nest_mode != "eco" | |
new_target_temperature = sensibo.c_to_f(sensibo.f_to_c(nest_target_temperature)) | |
sensibo.mode = new_mode unless new_mode == old_mode | |
sensibo.power_on = new_power_on unless new_power_on == old_power_on | |
sensibo.target_temperature = new_target_temperature unless new_target_temperature == old_target_temperature | |
p "changes: #{sensibo.dirty_attributes}" unless sensibo.dirty_attributes.empty? | |
sensibo.save unless sensibo.dirty_attributes.empty? | |
rescue => e | |
p "Error encountered!" | |
p "error name: #{e.class.name}" | |
p "error message: #{e.message}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment