Turning Nest thermostat ON (heating mode) and OFF using Google Calendar events, with error checking

Hi all, after strolling the documentation and forums for a couple of days I have finally finished the automation I wanted. Sharing the code here to benefit others looking for something similar. We have a vacation home with floor heating, and I wanted to turn the Nest thermostat ON (heating mode, following the heating schedule) and OFF (no heating, not following the heating schedule) using a Google Calendar that contains the reservations for the vacation home. The reservations are full-day reservations, ending on the last full day where the renters are there (in which case they will leave the following morning). The script below follows these requirements:

  • Nest thermostat turns ON when it finds a Google Calendar event (reservation) with the word “Nest” anywhere (in the title or in the description)
  • Nest thermostat turns OFF 12 hours after the event ends (so that it still heats on the morning after the last reservation)
  • Nest does not turn OFF when there is a new event at that time
  • Turning ON and OFF is error-checked and repeatedly tried every 30 seconds, stopping after 60 tries (half an hour)
  • Notifications are sent by e-mail for all actions (or failed actions)

Of course you need to set up Nest integration, Google Calendar integration and e-mail, see the documentation for relevant manuals, and replace relevant names and device_id. Let me know if you think the code could be improved in some way.

alias: Nest_calendar
description: Turn nest on and off when event starts and ends, with error checking
trigger:
  - platform: calendar
    event: start
    entity_id: calendar.guest_reservations
  - platform: calendar
    event: end
    entity_id: calendar.guest_reservations
    offset: "12:00:00"
condition:
  - condition: or
    conditions:
      - condition: template
        value_template: "{{ 'Nest' in trigger.calendar_event.summary }}"
      - condition: template
        value_template: "{{ 'Nest' in trigger.calendar_event.description }}"
action:
  - if:
      - "{{ trigger.event == 'start' }}"
    then:
      - alias: Repeat the sequence until the Nest is ON or 60 tries have passed
        repeat:
          sequence:
            - device_id: xxxxxxxxxxxxxxxxxx
              domain: climate
              entity_id: climate.living_room
              type: set_hvac_mode
              hvac_mode: heat
            - delay:
                seconds: 30
          until: "{{ is_state('climate.living_room', 'heat') or repeat.index > 60 }}"
      - if: "{{ is_state('climate.living_room', 'heat') }}"
        then:
          - service: notify.email_me
            data:
              title: Nest has been turned ON
              message: Nest is ON. Will turn Nest OFF again when reservation finishes.
        else:
          - service: notify.email_me
            data:
              title: FAILED to turn Nest ON
              message: Tried 60 times but failed, giving up now :-(
    else:
      - if: "{{ not is_state('calendar.guest_reservations', 'on') }}"
        then:
          - alias: Repeat the sequence until the Nest is OFF or 60 tries have passed
            repeat:
              sequence:
                - device_id: xxxxxxxxxxxxxxxxxx
                  domain: climate
                  entity_id: climate.living_room
                  type: set_hvac_mode
                  hvac_mode: "off"
                - delay:
                    seconds: 30
              until: "{{ is_state('climate.living_room', 'off') or repeat.index > 60 }}"
          - if: "{{ is_state('climate.living_room', 'off') }}"
            then:
              - service: notify.email_me
                data:
                  title: Nest has been turned OFF
                  message: Nest is OFF. Bye.
            else:
              - service: notify.email_me
                data:
                  title: FAILED to turn Nest OFF
                  message: Tried 60 times but failed, giving up now :-(
        else:
          - service: notify.email_me
            data:
              title: Keeping Nest ON
              message: >-
                End of reservation was triggered, but we are in a new
                reservation so keeping Nest ON
mode: queued

Hope this is helpful to others, cheers!

1 Like

This is ideal, thanks for sharing. I use a Nest thermostat at home and am frustrated sometimes by not being able to simply turn the heating on for an hour when I feel cold.

My intention is to have an automation which keeps the heating on for 45 minutes, then turns it off.

I had imagined doing this by temporarily increasing the target temperature to something high, then 45 minutes later reverting the target temperature back to what it was before.

I’ll have a play with the script you’ve shared and see how i get on!

Hi James, to be sure, my script toggles between the Nest mode:
Nest ON (hvac_mode: heat) which means that it follows the regular heating schedule/program. If the temperature is already at (or above) the target temperature, the Nest will not actually start heating. Heating always depends on the target temperature.
Nest OFF (hvac_mode: “off”) which means it is off and no longer follows the regular heating schedule/program, it simply will not heat (unless it drops below the safety temperature that is specified in the Nest settings).
It sounds like what you want to achieve is to turn on the heating sometimes even when the target temperature has already been reached? In that case I would indeed recommend to temporarily increase the target temperature, and go back to the original target temperature after a set amount of time. This can of course also be achieved through an automation, just look at the documentation for the Nest integration.

thanks, yes - i’ve realised that too now :slight_smile:

i’ve had a look at the automation options for the device, and there are two actions, changing HVAC mode and changing preset mode.

i can’t see how to create an automation action that allows me to specify a target temperature.

do you know where i need to look?

thanks!

^sorted - called the Climate action to sort this out :slight_smile: