Calendar Integration and Xmas Lights Automation

Can someone walk me through the process on automating a calendar integration for Xmas lights?

-Setup the Local Calendar integration
-Created a calendar called “Xmas Lights”
– Starts Nov 26th and ends Jan 14th
–Time starts at 7pm and ends at 10:30pm (same day)
–Devices are in a scene called “Xmas Lights On”

I was thinking i could do an Action - If / Then / Else but there’s no condition for Calendar integration.

Do I need two automations to turn on & off the Xmas Lights scenes?

Thanks,

A Calendar can be a State Condition ON or OFF.

There’s a service call to turn on a scene (scene.turn_on). There’s no service call to turn off a scene.

You have a scene called “Xmas Lights On”. Do you also have a scene called “Xmas Lights Off”?

Here is what I came up with:

alias: Seasonal - Xmas Lights (Nov - Jan)
description: Seasonal - Xmas Lights (Nov - Jan)
trigger:
  - platform: state
    entity_id:
      - calendar.yearly_calendar
condition: []
action:
  - if:
      - condition: state
        entity_id: calendar.yearly_calendar
        state: "on"
    then:
      - service: scene.turn_on
        target:
          entity_id: scene.xmas_lights_all
        metadata: {}
    else:
      - service: scene.turn_on
        target:
          entity_id: scene.xmas_lights_all_off
        metadata: {}
mode: single

Thoughts? I created another test calendar and it appears to be working.

If you name the two scenes like this:

scene.xmas_lights_all_on
scene.xmas_lights_all_off

then you can reduce the automation’s action to this:

alias: Seasonal - Xmas Lights (Nov - Jan)
description: Seasonal - Xmas Lights (Nov - Jan)
trigger:
  - platform: state
    entity_id:
      - calendar.yearly_calendar
condition: []
action:
  - service: scene.turn_on
    target:
      entity_id: 'scene.xmas_lights_all_{{ trigger.to_state.state }}'
mode: single

This is a blueprint for how I control my xmas tree lights on a switch with a calendar:

This is another way I do this for a calendar that has many device automations on it and you can match the event name to a specific device:

alias: Exterior XMas Light Schedule
description: ""
trigger:
  - platform: calendar
    event: start
    offset: "0:0:0"
    entity_id: calendar.local_automation_schedule
  - platform: calendar
    event: end
    offset: "0:0:0"
    entity_id: calendar.local_automation_schedule
condition:
  - condition: template
    value_template: "{{ 'Exterior XMas' in trigger.calendar_event.summary }}"
action:
  - if:
      - condition: template
        value_template: "{{ trigger.event == 'start' }}"
    then:
      - service: light.turn_on
        data: {}
        target:
          entity_id: light.exterior_switch
    else:
      - service: light.turn_off
        data: {}
        target:
          entity_id: light.exterior_switch
mode: single
1 Like

There are also some examples on the calendar page Calendar - Home Assistant

How do I get the calendar based trigger to work off the sun? E.g. I would like my lights to turn on 30 mins after sunset. Not based on a specific time.

In your automation, use a Sun Trigger with a positive value for its offset option.

Example

alias: example
description: Turn on lights 30 minutes after sunset
trigger:
  - platform: sun
    event: sunset
    offset: '00:30:00'
condition: []
action:
  - service: light turn_on
    target:
      entity_id:
        - light.bedroom
        - light.kitchen

If you envisioned using the Calendar integration for this purpose, you’ll need to explain what role it would play in combination with sunset.

I have a Christmas event in my Holiday calendar which runs from mid November to the first week in January. I want my WLED lights to come on during this time period, but only 30mins after sunset to 2am.

It would be simpler to add a condition, to the example I posted above, that employs a hard-coded time period rather than one derived from a calendar.

The following example turns on the light at 30 minutes after sunset from November 15 to January 6, inclusively. It turns off the light at 02:00 from November 15 to January 7, inclusively. It’s one extra day because the light is always turned off the next day at 02:00.

alias: example
description: Control Christmas Lights 
trigger:
  - id: 'on'
    platform: sun
    event: sunset
    offset: '00:30:00'
  - id: 'off'
    platform: time
    at: '02:00:00'
condition:
  - condition: template
    value_template: >
      {% set start = (11, 15) %}
      {% set end = (1, iif(trigger.id == 'on', 6, 7)) %}
      {% set t = (now().month, now().day) %}
      {{ t >= start or t <= end }}
action:
  - service: 'light.turn_{{ trigger.id }}'
    target:
      entity_id: light.wled

If you don’t want to hard-code the time period as shown above, you’ll need to devise a method of checking the Calendar and determining if it contains an All Day event for activating the WLED light. You can’t simply check the Calendar entity’s attributes.

I suggest creating a Trigger-based Template Binary Sensor (binary_sensor.xmas) that is triggered at the start of every day (00:00), checks if the current day’s calendar contains an All Day WLED event, and then reports on if it does (otherwise off). Then the automation posted above can use a State Condition to check if binary_sensor.xmas is on.

However this approach does not handle the complication created by the 02:00 turn-off time which occurs the day after the last day the light is turned on. In other words, if the Calendar’s Christmas period ends January 6th, the automation will turn on the light, shortly after sunset, on the 6th but not turn it off (at 02:00) on the 7th.

The example with the hard-coded Template Condition is designed to handle this situation.

Thanks, This is what I wanted to know. I really wanted the flexibilty of updating the Calendar itself as needed. I also didn’t really want triggering outside of those dates.

How do you intend to handle the fact that the time period for turning off the light is one day longer than the time period for turning on the light?

Hmm…I see what you’re saying. How can I make the Calendar itself a condition? Intead of the hard coded value template?

It’s not designed to be used like that. It’s meant to be used with a Calendar Trigger.

I see what you mean. Is there a efficient way doing tempate conditions for like 10+ holiday date ranges? Would I have to end up using If/else conditions?

It depends on the nature of the 10 date ranges. If they don’t overlap, your Template Condition can use a dictionary to store them. Finding a match in a dictionary is quicker than traversing a long if-elif-else-endif chain.

How would that look like? They wont overlap for sure as this calendar is specifically for holiday lights. I tried this if-else template condition, which didn’t work even though the template worked in developer tools.

if:
  - condition: template
    value_template: "  value_template: \"{{ is_state_attr('calendar.holidays', 'message', 'Christmas Lights') }}\""
then:
  - service: light.turn_{{ trigger.to_state.state }}
    target:
      entity_id:
        - light.house_wled
        - light.front_porch_light
        - light.front_yard_light

The Template Editor tests Jinja templates. It doesn’t test YAML. Your example contains a YAML error.

The word "value_template` is duplicated. Change it to this:

    value_template: "{{ is_state_attr('calendar.holidays', 'message', 'Christmas Lights') }}"