Evening Light schedule driven by Google Calendar

Open your Home Assistant instance and show the blueprint import dialog with a specific blueprint pre-filled.

I use the Google Calendar integration to control lights around the outside of my home according to a schedule.

Then use the binary sensor created as input into the blueprint automation. The calendar entry defines the earliest time the light can turn on, but also won’t turn on until sunset. That is, the light will turn on after sunset and after the event has started. The light shuts off when the calendar event is over.

blueprint:
  name: Evening Light Calendar
  description: Use a calendar for managing a light schedule.  Lights are turned on according the calendar schedule, and only after dark.
  domain: automation
  input:
    calendar_sensor:
      name: Calendar Sensor
      selector:
        entity:
          domain:
          - calendar
          - binary_sensor
    target_light:
      name: Light
      selector:
        target:
          entity:
            domain: light

variables:
  cal_switch: !input calendar_sensor

trigger:
- platform: state
  entity_id: !input calendar_sensor
- platform: sun
  event: sunset

condition:
- after: sunset
  condition: sun

action:
  service: "{{ iif(is_state(cal_switch, 'on'), 'light.turn_on', 'light.turn_off') }}"
  target: !input target_light
1 Like

Here is an example google_calendars.yaml file pointed at a secondary calendar in my account. The event descriptions have a hash tag that I use to match the calendar entity.

- cal_id: [email protected]
  entities:
  - device_id: backyard_light
    name: Backyard Light
    search: "#Backyard"
    track: true

I’m using a similar search for my google calendar integration, but I’m struggling to get it to work right. To work off of your example, I have events in my calendar with titles #Backyard and others with something like BBQ in Backyard. With the search on #Backyard, I would expect to only have the events with that title, but instead it’s triggering on all events with Backyard in them, which means both sets of events. It’s like it’s ignoring the # in the search…have you had any experience with this?

Ah I have not looked at that. In my case I also use a dedicated additional calendar so maybe I just get lucky since all my hash tags are unique.

You may want to see what happens searching in the Google calendar UI and see if it matches and maybe we should try a different method.

Longer term: I am building a new way to do automations for calendar events in home assistant using Triggers instead of treating the calendar as a binary sensor, and that will pass the full event struct to the automation, giving you ability to do your own string matching rather than relying on the API searching.

Yea, so if I put in a search like #Backyard in the normal Google calendar ui search field, every event with Backyard in the title is returned…so seems like Google ignores the #. I also tested with @ and it also ignores that.

I’m really interested in the triggers instead of a binary sensor…can’t wait to see that when you get to it!

Does this blueprint work? How do I use it, set the search condition, Etc.?

Yes, it works. I’ve just now updated it with a small modification though, and see more below.

The second post above shows an old way to create separate events based on search criteria in google calendar, but that is not needed anymore. Since then, we’ve added support for calendar triggers that work better.

What I currently use is first add a template entity in templates.yaml to create an entity for the particular calendar event I am trying to match:

- trigger:
    - platform: calendar
      event: start
      entity_id: calendar.device_automation_schedules
    - platform: calendar
      event: end
      entity_id: calendar.device_automation_schedules
  binary_sensor:
    - name: Calendar Backyard Light
      state: "{{ iif('#Backyard' in trigger.calendar_event.summary, iif(trigger.event == 'start', 'on', 'off'), states('binary_sensor.calendar_backyard_light')) }}"
      unique_id: 7e6f2b80-f10c-11ec-8c39-066a07ffbaf5
  unique_id: "2975913a-f10c-11ec-9d8a-066a07ffbaf5"

So this gives me an entity called binary_sensor.calendar_backyard_light that turns on only when the specific event has started. It also helps that those sensors can preserve state and are useful for tracking/debugging.