Toggle outside lights based on calendar event location = home and sunset

I would like to trigger my front outside lights to turn on when I have guests coming over and it’s going to be dark either when they arrive or when they leave.

I was hoping to use the integration to Google Calendar and basically toggle this off the location of an event on the calendar. If the location is set to my home address, then let HA assume that means guests are coming over to my place.

I know how to write an automation to turn lights on based on sunset and I know how to write on based on turning lights on when a calendar even starts.

What I can’t figure out is the overlap between the two and permutations involved. E.g., if they arrive while the sun is up but leave when it is dark. Or if it is dark for an hour before they arrive, how do I turn the lights on 15 minutes before they get here?

I guess what I can’t figure out is how to have HA check if there’s an event at my home sometime this evening at sunset, or to check if the sun will set during an event at my home.

This is as far as I got:

trigger:
  - platform: calendar
    event: start
    offset: '-0:15:0'
    entity_id: calendar.our_family_block_both
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: climate.main_floor_thermostat
        attribute: preset_mode
        state:
          - Home
          - Sleep
      - condition: template
        value_template: '{{ ''123 Somewhere St'' in trigger.calendar_event.location }}'
action:
  - choose:
      - conditions:
          - condition: sun
            after: sunset
          - condition: state
            entity_id: calendar.our_family_block_both
            attribute: location
            state: 123 Somewhere St
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.outside_lights_normal_on
            metadata: {}
mode: single

You can write an automation with 2 triggers and drive your actions, depending on what triggered them.

That way you can construct an automation that triggers both at sunset (or when the sun is x below the horizon or any other trigger to indicate it’s going dark outside) and 15mins before your guests arrive and when the event is at its end (or any specific extended time thereafter).

Then you can put more specific conditions into your actions. In other words:
if triggered by sunset and during your event → choose 1
If triggered by the start of your event and post sunset → choose 2
if triggered by the end of your event and post sunset → choose 3

Untested draft below:
(I’m not familiar with the calendar integration, so not sure how to write the template to get the start time of your event minus 15mins)

trigger:
  - platform: calendar
    event: start
    offset: '-0:15:0'
    entity_id: calendar.our_family_block_both
    id: event_start
  - platform: sun
    event: sunset
    offset: 0
    id: sunset
condition:
  - condition: and
    conditions:
      - condition: state
        entity_id: climate.main_floor_thermostat
        attribute: preset_mode
        state:
          - Home
          - Sleep
      - condition: state
        entity_id: calendar.our_family_block_both
        attribute: location
        state: 123 Somewhere St
      - condition: sun
        after: sunset
action:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - event_start
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.outside_lights_normal_on
            metadata: {}
      - conditions:
          - condition: trigger
            id:
              - sunset
          - condition: template
            value_template: "{{ template to determine start of event -15mins }}"
        sequence:
          - service: scene.turn_on
            target:
              entity_id: scene.outside_lights_normal_on
            metadata: {}
mode: single
1 Like